File: SmokeTestGame.cpp

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (254 lines) | stat: -rw-r--r-- 9,087 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <boost/test/unit_test.hpp>

#include "ClientAppFixture.h"
#include "Empire/Empire.h"
#include "universe/Planet.h"
#include "universe/Ship.h"
#include "universe/ShipDesign.h"
#include "util/Directories.h"
#include "util/Order.h"
#include "util/Process.h"
#include "util/SitRepEntry.h"

#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/string_generator.hpp>

#ifdef FREEORION_MACOSX
#include <stdlib.h>
#endif

BOOST_FIXTURE_TEST_SUITE(SmokeTestGame, ClientAppFixture)

/**
 * - Do start a server with as a host mock player on localhost.
 * - Expect successfully connection to localhost server.
 * - Do launch singleplayer game with `FO_TEST_GAME_AIS` AIs (by default 2).
 * - Expect singleplayer game is correctly lauched.
 * - Do make empty turns until turn number reach `FO_TEST_GAME_TURNS` or mock player will be
 *   eliminated or someone will win.
 * - Expect turns're processing without error.
 * - Do save game after each turn if `FO_TEST_GAME_SAVE` was set.
 * - Expect saving is processing correctly.
 * - Do shut down server.
 * - Expect that server and AIs were shut down.
 */

BOOST_AUTO_TEST_CASE(host_server) {

    BOOST_REQUIRE(!PingLocalHostServer());

    std::string SERVER_CLIENT_EXE = GetOptionsDB().Get<std::string>("misc.server-local-binary.path");

    BOOST_TEST_MESSAGE(SERVER_CLIENT_EXE);

#ifdef FREEORION_MACOSX
    // On OSX set environment variable DYLD_LIBRARY_PATH to python framework folder
    // bundled with app, so the dynamic linker uses the bundled python library.
    // Otherwise the dynamic linker will look for a correct python lib in system
    // paths, and if it can't find it, throw an error and terminate!
    // Setting environment variable here, spawned child processes will inherit it.
    setenv("DYLD_LIBRARY_PATH", GetPythonHome().string().c_str(), 1);
#endif

    std::vector<std::string> args{
        "\"" + SERVER_CLIENT_EXE + "\"",
        "--singleplayer",
        "--testing",
        "--log-level", "info",
        "--resource.path", GetOptionsDB().Get<std::string>("resource.path")
    };

#ifdef FREEORION_LINUX
    // Dirty hack to output log to console.
    args.push_back("--log-file");
    args.push_back("/proc/self/fd/1");
#endif

    Process server = Process(SERVER_CLIENT_EXE, args);

    BOOST_REQUIRE(ConnectToLocalHostServer());

    BOOST_TEST_MESSAGE("Connected to server");

    boost::posix_time::ptime start_time = boost::posix_time::microsec_clock::local_time();
    BOOST_REQUIRE(ProcessMessages(start_time, MAX_WAITING_SEC));

    BOOST_TEST_MESSAGE("First messages processed. Starting game...");

    unsigned int num_AIs = 2;
    int num_turns = 5;
    bool save_game = true;

    const char *env_num_AIs = std::getenv("FO_TEST_GAME_AIS");
    if (env_num_AIs) {
        try {
            num_AIs = boost::lexical_cast<unsigned int>(env_num_AIs);
        } catch (...) {
            // ignore
        }
    }

    const char *env_num_turns = std::getenv("FO_TEST_GAME_TURNS");
    if (env_num_turns) {
        try {
            num_turns = boost::lexical_cast<int>(env_num_turns);
        } catch (...) {
            // ignore
        }
    }

    const char *env_save_game = std::getenv("FO_TEST_GAME_SAVE");
    if (env_save_game) {
        try {
            save_game = boost::lexical_cast<int>(env_save_game) != 0;
        } catch (...) {
            // ignore
        }
    }

    HostSPGame(num_AIs);

    BOOST_TEST_MESSAGE("Waiting game to start...");

    start_time = boost::posix_time::microsec_clock::local_time();
    while (!m_game_started) {
        BOOST_REQUIRE(ProcessMessages(start_time, MAX_WAITING_SEC));
    }

    BOOST_REQUIRE_EQUAL(m_ai_empires.size(), num_AIs);

    BOOST_TEST_MESSAGE("Game started. Waiting AI for turns...");

    start_time = boost::posix_time::microsec_clock::local_time();
    while (!m_ai_waiting.empty()) {
        BOOST_REQUIRE(ProcessMessages(start_time, MAX_WAITING_SEC));
    }

    SaveGameUIData ui_data;

    int troop_design_id = INVALID_DESIGN_ID;

    while (m_current_turn <= num_turns) {
        SendPartialOrders();

        StartTurn(ui_data);

        m_turn_done = false;
        m_ai_waiting = m_ai_empires;

        BOOST_TEST_MESSAGE("Turn done. Waiting server for update... " << m_current_turn);
        start_time = boost::posix_time::microsec_clock::local_time();
        while (!m_turn_done) {
            BOOST_REQUIRE(ProcessMessages(start_time, MAX_WAITING_SEC));
        }

        BOOST_TEST_MESSAGE("Turn updated " << m_current_turn);

        // output sitreps
        auto my_empire = m_empires.GetEmpire(m_empire_id);
        BOOST_REQUIRE(my_empire != nullptr);
        for (const auto& sitrep : my_empire->SitReps()) {
            if (sitrep.GetTurn() == m_current_turn) {
                BOOST_TEST_MESSAGE("Sitrep: " << sitrep.Dump());
            }
        }

        const auto is_owned = [this](const UniverseObject* obj) { return obj->OwnedBy(m_empire_id); };

        if (m_current_turn == 2) {
            // check home planet meters
            const Planet* home_planet = nullptr;

            for (const auto* planet : Objects().findRaw<Planet>(is_owned)) {
                BOOST_REQUIRE_LT(0.0, planet->GetMeter(MeterType::METER_POPULATION)->Current());
                BOOST_TEST_MESSAGE("Population: " << planet->GetMeter(MeterType::METER_POPULATION)->Current());
                BOOST_REQUIRE_LT(0.0, planet->GetMeter(MeterType::METER_INDUSTRY)->Current());
                BOOST_TEST_MESSAGE("Industry: " << planet->GetMeter(MeterType::METER_INDUSTRY)->Current());
                BOOST_REQUIRE_LT(0.0, planet->GetMeter(MeterType::METER_RESEARCH)->Current());
                BOOST_TEST_MESSAGE("Research: " << planet->GetMeter(MeterType::METER_RESEARCH)->Current());
                home_planet = planet;
            }
            BOOST_REQUIRE(home_planet != nullptr);

            // enqueue Troop Ship
            ScriptingContext context;

            static const boost::uuids::uuid troop_ship_uuid =
                boost::uuids::string_generator{}("08a58b080929496d84fcfaa91424ca02");

            for (const auto* design : GetPredefinedShipDesignManager().GetOrderedShipDesigns()) {
                BOOST_TEST_MESSAGE("Predefined ship design " << design->Name() << " " << design->UUID());
                if (design->UUID() == troop_ship_uuid) {
                    BOOST_REQUIRE_EQUAL(troop_design_id, INVALID_DESIGN_ID);
                    troop_design_id = my_empire->AddShipDesign(*design, context.ContextUniverse());
                    BOOST_REQUIRE_NE(troop_design_id, INVALID_DESIGN_ID);
                    BOOST_TEST_MESSAGE("Found predefined troop ship design " << troop_design_id << " " << design->Name() << " " << design->UUID());
                }
            }

            BOOST_REQUIRE_NE(troop_design_id, INVALID_DESIGN_ID);

            BOOST_REQUIRE(my_empire->ProducibleItem(BuildType::BT_SHIP, troop_design_id, home_planet->ID(), context));

            m_orders.IssueOrder(std::make_shared<ProductionQueueOrder>(ProductionQueueOrder::ProdQueueOrderAction::PLACE_IN_QUEUE,
                m_empire_id,
                ProductionQueue::ProductionItem(BuildType::BT_SHIP, troop_design_id, context.ContextUniverse()),
                1,
                home_planet->ID()),
                context);
        }

        if (m_current_turn > 2) {
            if (my_empire->GetProductionQueue().empty()) {
                for (const auto* ship : Objects().findRaw<Ship>(is_owned)) {
                    if (ship->DesignID() == troop_design_id) {
                        ScriptingContext context;
                        BOOST_TEST_MESSAGE("Found troop ship with troops " << ship->TroopCapacity(context.ContextUniverse()));
                        BOOST_REQUIRE_GT(ship->TroopCapacity(context.ContextUniverse()), 0.0f);
                    }
                }
            }
        }

        if (my_empire->Eliminated()) {
            BOOST_TEST_MESSAGE("Test player empire was eliminated.");
            break;
        }
        bool have_winner = false;
        for (auto& empire : m_empires) {
            if (empire.second->Won()) {
                have_winner = true;
                break;
            }
        }
        if (have_winner) {
            BOOST_TEST_MESSAGE("Someone wins game.");
            break;
        }

        BOOST_TEST_MESSAGE("Waiting AI for turns...");
        start_time = boost::posix_time::microsec_clock::local_time();
        while (!m_ai_waiting.empty()) {
            BOOST_REQUIRE(ProcessMessages(start_time, MAX_WAITING_SEC));
        }

        if (save_game) {
            BOOST_TEST_MESSAGE("Save game");

            SaveGame();
            start_time = boost::posix_time::microsec_clock::local_time();
            while (!m_save_completed) {
                BOOST_REQUIRE(ProcessMessages(start_time, MAX_WAITING_SEC));
            }
        }
    }

    BOOST_TEST_MESSAGE("Terminating server...");

    BOOST_WARN(server.Terminate());

    BOOST_TEST_MESSAGE("Server terminated");
}

BOOST_AUTO_TEST_SUITE_END()