This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
forked from zeliard/Dispatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DispatcherTest.cpp
100 lines (74 loc) · 1.9 KB
/
DispatcherTest.cpp
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
#include "stdafx.h"
#include "JobDispatcher.h"
enum TestEnvironment
{
TEST_OBJECT_COUNT = 10,
TEST_WORKER_THREAD = 4,
};
class TestObject : public AsyncExecutable
{
public:
TestObject() : mTestCount(0)
{}
void TestFunc0()
{
++mTestCount;
}
void TestFunc1(int b)
{
mTestCount += b;
}
void TestFunc2(double a, int b)
{
mTestCount += b;
if (a < 50.0)
DoAsync(&TestObject::TestFunc1, std::move(b));
}
void TestFuncForTimer(int b)
{
if (rand() % 2 == 0)
DoAsyncAfter(1000, &TestObject::TestFuncForTimer, std::move(-b));
}
int GetTestCount() { return mTestCount; }
private:
int mTestCount;
};
std::vector<std::shared_ptr<TestObject>> gTestObjects;
class TestWorkerThread : public Runnable
{
public:
~TestWorkerThread()
{
printf("tt dtor");
}
virtual bool Run()
{
/// TEST
uint32_t after = rand() % 2000;
if (after > 1000)
{
gTestObjects[rand() % TEST_OBJECT_COUNT]->DoAsync(&TestObject::TestFunc0);
gTestObjects[rand() % TEST_OBJECT_COUNT]->DoAsync(&TestObject::TestFunc2, double(rand() % 100), 2);
gTestObjects[rand() % TEST_OBJECT_COUNT]->DoAsync(&TestObject::TestFunc1, 1);
gTestObjects[rand() % TEST_OBJECT_COUNT]->DoAsyncAfter(after, &TestObject::TestFuncForTimer, (int)after);
}
/// exit condition
if (gTestObjects[rand() % TEST_OBJECT_COUNT]->GetTestCount() > 5000)
{
std::cout << "thread " << std::this_thread::get_id() << " end by force\n";
return false;
}
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
gTestObjects.push_back(std::make_shared<TestObject>());
JobDispatcher<TestWorkerThread> workerService(TEST_WORKER_THREAD);
workerService.RunWorkerThreads();
for (auto& t : gTestObjects)
std::cout << "TestCount: " << t->GetTestCount() << std::endl;
getchar();
return 0;
}