1 | // |
---|---|
2 | // TaskTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "TaskTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Task.h" |
15 | #include "Poco/Thread.h" |
16 | #include "Poco/Event.h" |
17 | #include "Poco/AutoPtr.h" |
18 | |
19 | |
20 | using Poco::Task; |
21 | using Poco::Thread; |
22 | using Poco::Event; |
23 | using Poco::AutoPtr; |
24 | |
25 | |
26 | namespace |
27 | { |
28 | class TestTask: public Task |
29 | { |
30 | public: |
31 | TestTask(): Task("TestTask") |
32 | { |
33 | } |
34 | |
35 | void runTask() |
36 | { |
37 | _event.wait(); |
38 | if (sleep(10)) |
39 | return; |
40 | setProgress(0.5); |
41 | _event.wait(); |
42 | if (isCancelled()) |
43 | return; |
44 | setProgress(1.0); |
45 | _event.wait(); |
46 | } |
47 | |
48 | void cont() |
49 | { |
50 | _event.set(); |
51 | } |
52 | |
53 | private: |
54 | Event _event; |
55 | }; |
56 | } |
57 | |
58 | |
59 | TaskTest::TaskTest(const std::string& rName): CppUnit::TestCase(rName) |
60 | { |
61 | } |
62 | |
63 | |
64 | TaskTest::~TaskTest() |
65 | { |
66 | } |
67 | |
68 | |
69 | void TaskTest::testFinish() |
70 | { |
71 | AutoPtr<TestTask> pTT = new TestTask; |
72 | assertTrue (pTT->state() == Task::TASK_IDLE); |
73 | Thread thr; |
74 | thr.start(*pTT); |
75 | assertTrue (pTT->progress() == 0); |
76 | pTT->cont(); |
77 | while (pTT->progress() != 0.5) Thread::sleep(50); |
78 | assertTrue (pTT->state() == Task::TASK_RUNNING); |
79 | pTT->cont(); |
80 | while (pTT->progress() != 1.0) Thread::sleep(50); |
81 | pTT->cont(); |
82 | thr.join(); |
83 | assertTrue (pTT->state() == Task::TASK_FINISHED); |
84 | } |
85 | |
86 | |
87 | void TaskTest::testCancel1() |
88 | { |
89 | AutoPtr<TestTask> pTT = new TestTask; |
90 | assertTrue (pTT->state() == Task::TASK_IDLE); |
91 | Thread thr; |
92 | thr.start(*pTT); |
93 | assertTrue (pTT->progress() == 0); |
94 | pTT->cont(); |
95 | while (pTT->progress() != 0.5) Thread::sleep(50); |
96 | assertTrue (pTT->state() == Task::TASK_RUNNING); |
97 | pTT->cancel(); |
98 | assertTrue (pTT->state() == Task::TASK_CANCELLING); |
99 | pTT->cont(); |
100 | thr.join(); |
101 | assertTrue (pTT->state() == Task::TASK_FINISHED); |
102 | } |
103 | |
104 | |
105 | void TaskTest::testCancel2() |
106 | { |
107 | AutoPtr<TestTask> pTT = new TestTask; |
108 | assertTrue (pTT->state() == Task::TASK_IDLE); |
109 | Thread thr; |
110 | thr.start(*pTT); |
111 | assertTrue (pTT->progress() == 0); |
112 | pTT->cancel(); |
113 | assertTrue (pTT->state() == Task::TASK_CANCELLING); |
114 | pTT->cont(); |
115 | thr.join(); |
116 | assertTrue (pTT->state() == Task::TASK_FINISHED); |
117 | } |
118 | |
119 | |
120 | void TaskTest::setUp() |
121 | { |
122 | } |
123 | |
124 | |
125 | void TaskTest::tearDown() |
126 | { |
127 | } |
128 | |
129 | |
130 | CppUnit::Test* TaskTest::suite() |
131 | { |
132 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TaskTest"); |
133 | |
134 | CppUnit_addTest(pSuite, TaskTest, testFinish); |
135 | CppUnit_addTest(pSuite, TaskTest, testCancel1); |
136 | CppUnit_addTest(pSuite, TaskTest, testCancel2); |
137 | |
138 | return pSuite; |
139 | } |
140 |