]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_threading.cpp
Load client mods into memory before execution.
[minetest.git] / src / unittest / test_threading.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21
22 #include <atomic>
23 #include "threading/semaphore.h"
24 #include "threading/thread.h"
25
26
27 class TestThreading : public TestBase {
28 public:
29         TestThreading() { TestManager::registerTestModule(this); }
30         const char *getName() { return "TestThreading"; }
31         void runTests(IGameDef *gamedef);
32
33         void testStartStopWait();
34         void testThreadKill();
35         void testAtomicSemaphoreThread();
36 };
37
38 static TestThreading g_test_instance;
39
40 void TestThreading::runTests(IGameDef *gamedef)
41 {
42         TEST(testStartStopWait);
43         TEST(testThreadKill);
44         TEST(testAtomicSemaphoreThread);
45 }
46
47 class SimpleTestThread : public Thread {
48 public:
49         SimpleTestThread(unsigned int interval) :
50                 Thread("SimpleTest"),
51                 m_interval(interval)
52         {
53         }
54
55 private:
56         void *run()
57         {
58                 void *retval = this;
59
60                 if (isCurrentThread() == false)
61                         retval = (void *)0xBAD;
62
63                 while (!stopRequested())
64                         sleep_ms(m_interval);
65
66                 return retval;
67         }
68
69         unsigned int m_interval;
70 };
71
72 void TestThreading::testStartStopWait()
73 {
74         void *thread_retval;
75         SimpleTestThread *thread = new SimpleTestThread(25);
76
77         // Try this a couple times, since a Thread should be reusable after waiting
78         for (size_t i = 0; i != 5; i++) {
79                 // Can't wait() on a joined, stopped thread
80                 UASSERT(thread->wait() == false);
81
82                 // start() should work the first time, but not the second.
83                 UASSERT(thread->start() == true);
84                 UASSERT(thread->start() == false);
85
86                 UASSERT(thread->isRunning() == true);
87                 UASSERT(thread->isCurrentThread() == false);
88
89                 // Let it loop a few times...
90                 sleep_ms(70);
91
92                 // It's still running, so the return value shouldn't be available to us.
93                 UASSERT(thread->getReturnValue(&thread_retval) == false);
94
95                 // stop() should always succeed
96                 UASSERT(thread->stop() == true);
97
98                 // wait() only needs to wait the first time - the other two are no-ops.
99                 UASSERT(thread->wait() == true);
100                 UASSERT(thread->wait() == false);
101                 UASSERT(thread->wait() == false);
102
103                 // Now that the thread is stopped, we should be able to get the
104                 // return value, and it should be the object itself.
105                 thread_retval = NULL;
106                 UASSERT(thread->getReturnValue(&thread_retval) == true);
107                 UASSERT(thread_retval == thread);
108         }
109
110         delete thread;
111 }
112
113
114 void TestThreading::testThreadKill()
115 {
116         SimpleTestThread *thread = new SimpleTestThread(300);
117
118         UASSERT(thread->start() == true);
119
120         // kill()ing is quite violent, so let's make sure our victim is sleeping
121         // before we do this... so we don't corrupt the rest of the program's state
122         sleep_ms(100);
123         UASSERT(thread->kill() == true);
124
125         // The state of the thread object should be reset if all went well
126         UASSERT(thread->isRunning() == false);
127         UASSERT(thread->start() == true);
128         UASSERT(thread->stop() == true);
129         UASSERT(thread->wait() == true);
130
131         // kill() after already waiting should fail.
132         UASSERT(thread->kill() == false);
133
134         delete thread;
135 }
136
137
138 class AtomicTestThread : public Thread {
139 public:
140         AtomicTestThread(std::atomic<u32> &v, Semaphore &trigger) :
141                 Thread("AtomicTest"),
142                 val(v),
143                 trigger(trigger)
144         {
145         }
146
147 private:
148         void *run()
149         {
150                 trigger.wait();
151                 for (u32 i = 0; i < 0x10000; ++i)
152                         ++val;
153                 return NULL;
154         }
155
156         std::atomic<u32> &val;
157         Semaphore &trigger;
158 };
159
160
161 void TestThreading::testAtomicSemaphoreThread()
162 {
163         std::atomic<u32> val;
164         val = 0;
165         Semaphore trigger;
166         static const u8 num_threads = 4;
167
168         AtomicTestThread *threads[num_threads];
169         for (auto &thread : threads) {
170                 thread = new AtomicTestThread(val, trigger);
171                 UASSERT(thread->start());
172         }
173
174         trigger.post(num_threads);
175
176         for (AtomicTestThread *thread : threads) {
177                 thread->wait();
178                 delete thread;
179         }
180
181         UASSERT(val == num_threads * 0x10000);
182 }
183