3 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "util/string.h"
24 #include "util/serialize.h"
26 class FakeServer : public Server
30 FakeServer() : Server("fakeworld", SubgameSpec("fakespec", "fakespec"), true,
31 Address(), true, nullptr)
37 void SendChatMessage(session_t peer_id, const ChatMessage &message)
43 class TestServerShutdownState : public TestBase
46 TestServerShutdownState() { TestManager::registerTestModule(this); }
47 const char *getName() { return "TestServerShutdownState"; }
49 void runTests(IGameDef *gamedef);
57 static TestServerShutdownState g_test_instance;
59 void TestServerShutdownState::runTests(IGameDef *gamedef)
67 void TestServerShutdownState::testInit()
69 Server::ShutdownState ss;
70 UASSERT(!ss.is_requested);
71 UASSERT(!ss.should_reconnect);
72 UASSERT(ss.message.empty());
73 UASSERT(ss.m_timer == 0.0f);
76 void TestServerShutdownState::testReset()
78 Server::ShutdownState ss;
80 UASSERT(!ss.is_requested);
81 UASSERT(!ss.should_reconnect);
82 UASSERT(ss.message.empty());
83 UASSERT(ss.m_timer == 0.0f);
86 void TestServerShutdownState::testTrigger()
88 Server::ShutdownState ss;
89 ss.trigger(3.0f, "testtrigger", true);
90 UASSERT(!ss.is_requested);
91 UASSERT(ss.should_reconnect);
92 UASSERT(ss.message == "testtrigger");
93 UASSERT(ss.m_timer == 3.0f);
96 void TestServerShutdownState::testTick()
98 std::unique_ptr<FakeServer> fakeServer(new FakeServer());
99 Server::ShutdownState ss;
100 ss.trigger(28.0f, "testtrigger", true);
101 ss.tick(0.0f, fakeServer.get());
103 // Tick with no time should not change anything
104 UASSERT(!ss.is_requested);
105 UASSERT(ss.should_reconnect);
106 UASSERT(ss.message == "testtrigger");
107 UASSERT(ss.m_timer == 28.0f);
110 ss.tick(2.0f, fakeServer.get());
111 UASSERT(!ss.is_requested);
112 UASSERT(ss.should_reconnect);
113 UASSERT(ss.message == "testtrigger");
114 UASSERT(ss.m_timer == 26.0f);
116 // Tick remaining seconds + additional expire
117 ss.tick(26.1f, fakeServer.get());
118 UASSERT(ss.is_requested);
119 UASSERT(ss.should_reconnect);
120 UASSERT(ss.message == "testtrigger");
121 UASSERT(ss.m_timer == 0.0f);