@@ -6,57 +6,57 @@ |
||
6 | 6 | |
7 | 7 | class Listener implements ListenerInterface |
8 | 8 | { |
9 | - /** @var string $eventName */ |
|
10 | - private $eventName; |
|
11 | - |
|
12 | - /** @var callable $callback */ |
|
13 | - private $callback; |
|
14 | - |
|
15 | - /** |
|
16 | - * Listener constructor. |
|
17 | - * @param string $eventName |
|
18 | - * @param callable|null $callback |
|
19 | - */ |
|
20 | - public function __construct(string $eventName, callable $callback = null) |
|
21 | - { |
|
22 | - $this->eventName = $eventName; |
|
23 | - $this->callback = $callback; |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * @inheritDoc |
|
28 | - */ |
|
29 | - public function getEventName(): string |
|
30 | - { |
|
31 | - return $this->eventName; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @inheritDoc |
|
36 | - */ |
|
37 | - public function setEventName(string $eventName): self |
|
38 | - { |
|
39 | - $this->eventName = $eventName; |
|
40 | - |
|
41 | - return $this; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @inheritDoc |
|
46 | - */ |
|
47 | - public function setCallback(callable $callback): ListenerInterface |
|
48 | - { |
|
49 | - $this->callback = $callback; |
|
50 | - |
|
51 | - return $this; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @inheritDoc |
|
56 | - */ |
|
57 | - public function notify(EventInterface $event) |
|
58 | - { |
|
59 | - ($this->callback)($event); |
|
60 | - } |
|
9 | + /** @var string $eventName */ |
|
10 | + private $eventName; |
|
11 | + |
|
12 | + /** @var callable $callback */ |
|
13 | + private $callback; |
|
14 | + |
|
15 | + /** |
|
16 | + * Listener constructor. |
|
17 | + * @param string $eventName |
|
18 | + * @param callable|null $callback |
|
19 | + */ |
|
20 | + public function __construct(string $eventName, callable $callback = null) |
|
21 | + { |
|
22 | + $this->eventName = $eventName; |
|
23 | + $this->callback = $callback; |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * @inheritDoc |
|
28 | + */ |
|
29 | + public function getEventName(): string |
|
30 | + { |
|
31 | + return $this->eventName; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @inheritDoc |
|
36 | + */ |
|
37 | + public function setEventName(string $eventName): self |
|
38 | + { |
|
39 | + $this->eventName = $eventName; |
|
40 | + |
|
41 | + return $this; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @inheritDoc |
|
46 | + */ |
|
47 | + public function setCallback(callable $callback): ListenerInterface |
|
48 | + { |
|
49 | + $this->callback = $callback; |
|
50 | + |
|
51 | + return $this; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @inheritDoc |
|
56 | + */ |
|
57 | + public function notify(EventInterface $event) |
|
58 | + { |
|
59 | + ($this->callback)($event); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
@@ -7,270 +7,270 @@ |
||
7 | 7 | |
8 | 8 | class ThreadPool extends AbstractThreadPoolMediator |
9 | 9 | { |
10 | - // 0.2s |
|
11 | - private const SLEEP_TIME_MS = 50000; |
|
12 | - |
|
13 | - /** @var Thread[] $childs */ |
|
14 | - private $threads; |
|
15 | - |
|
16 | - /** @var Thread[] $toRunThreads */ |
|
17 | - private $toRunThreads; |
|
18 | - |
|
19 | - /** @var Thread[] $runningChilds */ |
|
20 | - private $runningThreads; |
|
21 | - |
|
22 | - /** @var bool $isRunning */ |
|
23 | - private $isRunning; |
|
24 | - |
|
25 | - /** @var int $maxRunningThreadNb */ |
|
26 | - private $maxRunningThreadNb; |
|
27 | - |
|
28 | - /** |
|
29 | - * ThreadPool constructor. |
|
30 | - */ |
|
31 | - public function __construct() |
|
32 | - { |
|
33 | - parent::__construct(); |
|
34 | - $this->threads = []; |
|
35 | - $this->runningThreads = []; |
|
36 | - $this->toRunThreads = []; |
|
37 | - $this->isRunning = false; |
|
38 | - $this->maxRunningThreadNb = 0; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * |
|
43 | - */ |
|
44 | - public function __destruct() |
|
45 | - { |
|
46 | - pcntl_waitpid(-1, $status, WNOHANG); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return Thread[] |
|
51 | - */ |
|
52 | - public function getThreads(): array |
|
53 | - { |
|
54 | - return $this->threads; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param Thread[] $threads |
|
59 | - * @return ThreadPool |
|
60 | - */ |
|
61 | - public function setThreads(array $threads): self |
|
62 | - { |
|
63 | - $this->threads = $threads; |
|
64 | - |
|
65 | - return $this; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param Thread $thread |
|
70 | - * @return ThreadPool |
|
71 | - */ |
|
72 | - public function addThread(Thread $thread): self |
|
73 | - { |
|
74 | - $this->threads[] = $thread; |
|
75 | - |
|
76 | - return $this; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @return int |
|
81 | - */ |
|
82 | - public function getMaxRunningThreadNb(): int |
|
83 | - { |
|
84 | - return $this->maxRunningThreadNb; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param int $maxRunningThreadNb |
|
89 | - * @return ThreadPool |
|
90 | - */ |
|
91 | - public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
92 | - { |
|
93 | - $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
94 | - |
|
95 | - return $this; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return Thread[] |
|
100 | - */ |
|
101 | - public function getToRunThreads(): array |
|
102 | - { |
|
103 | - return $this->toRunThreads; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return Thread[] |
|
108 | - */ |
|
109 | - public function getRunningThreads(): array |
|
110 | - { |
|
111 | - return $this->runningThreads; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @throws ThreadException |
|
116 | - */ |
|
117 | - public function run() |
|
118 | - { |
|
119 | - $this->checkEnv(); |
|
120 | - $this->initRun(); |
|
121 | - |
|
122 | - while ($this->isRunningThreads()) { |
|
123 | - $this->waitOnThreads(); |
|
124 | - } |
|
125 | - |
|
126 | - $this->resetRun(); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return bool |
|
131 | - * @throws ThreadException |
|
132 | - */ |
|
133 | - private function isRunningThreads(): bool |
|
134 | - { |
|
135 | - if (count($this->toRunThreads) > 0) { |
|
136 | - while (count($this->runningThreads) < $this->maxRunningThreadNb && count($this->toRunThreads) > 0) { |
|
137 | - $this->createThreadProcess(array_shift($this->toRunThreads)); |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - return count($this->runningThreads) > 0; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * can't test some part of it this since we can't unit-test in web and we're never in a child |
|
146 | - * process when pid 0 when unit-testing since the coverage is done by the parent thread |
|
147 | - * @param Thread $thread |
|
148 | - * @throws ThreadException |
|
149 | - */ |
|
150 | - private function createThreadProcess(Thread $thread) |
|
151 | - { |
|
152 | - $pid = pcntl_fork(); |
|
153 | - |
|
154 | - switch ($pid) { |
|
155 | - case -1: //error forking |
|
156 | - // @codeCoverageIgnoreStart |
|
157 | - throw new ThreadException('Error while trying to fork. Check your server installation'); |
|
158 | - // @codeCoverageIgnoreEnd |
|
159 | - case 0: // child |
|
160 | - // @codeCoverageIgnoreStart |
|
161 | - $this->processThread($thread); |
|
162 | - break; |
|
163 | - // @codeCoverageIgnoreEnd |
|
164 | - default: //parent |
|
165 | - $thread->setPid($pid); |
|
166 | - $this->runningThreads[] = $thread; |
|
167 | - $this->notify(Event::POOL_NEW_THREAD, $thread); |
|
168 | - $this->startRunStatus(); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * |
|
174 | - */ |
|
175 | - private function waitOnThreads() |
|
176 | - { |
|
177 | - $this->notify(Event::POOL_PRE_WAIT_TICK); |
|
178 | - foreach ($this->runningThreads as $k => $thread) { |
|
179 | - |
|
180 | - $res = pcntl_waitpid($thread->getPid(), $status, WNOHANG); |
|
181 | - $this->notify(Event::POOL_WAIT_TICK_PID); |
|
182 | - |
|
183 | - if ($res === -1 || $res > 0) { |
|
184 | - $this->notify(Event::POOL_WAIT_TICK_PID_REMOVED, $thread); |
|
185 | - unset($this->runningThreads[$k]); |
|
186 | - } |
|
187 | - |
|
188 | - } |
|
189 | - $this->notify(Event::POOL_POST_WAIT_TICK); |
|
190 | - |
|
191 | - usleep(self::SLEEP_TIME_MS); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the |
|
196 | - * unit-test coverage which is only done in the main process |
|
197 | - * @param Thread $thread |
|
198 | - * @throws ThreadException |
|
199 | - */ |
|
200 | - private function processThread(Thread $thread) |
|
201 | - { |
|
202 | - $this->notify(Event::THREAD_PRE_PROCESS, $thread); |
|
203 | - $response = $thread->run($thread->getProcessName()); |
|
204 | - $this->notify(Event::THREAD_POST_PROCESS, $thread); |
|
205 | - |
|
206 | - switch ($response) { |
|
207 | - case Thread::EXIT_STATUS_SUCCESS: |
|
208 | - $this->notify(Event::THREAD_EXIT_SUCCESS, $thread); |
|
209 | - break; |
|
210 | - case Thread::EXIT_STATUS_ERROR: |
|
211 | - $this->notify(Event::THREAD_EXIT_ERROR, $thread); |
|
212 | - break; |
|
213 | - default: |
|
214 | - $this->notify(Event::THREAD_EXIT_UNKNOWN, $thread); |
|
215 | - } |
|
216 | - |
|
217 | - exit($response); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Can't test the exception is not in cli since php-unit is only run in cli environment |
|
222 | - * @throws ThreadException |
|
223 | - */ |
|
224 | - private function checkEnv() |
|
225 | - { |
|
226 | - if (false === $this->isCli()) { |
|
227 | - // @codeCoverageIgnoreStart |
|
228 | - throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli'); |
|
229 | - // @codeCoverageIgnoreEnd |
|
230 | - } |
|
231 | - if (0 === count($this->threads)) { |
|
232 | - throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool'); |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * |
|
238 | - */ |
|
239 | - private function initRun() |
|
240 | - { |
|
241 | - $this->resetRun(); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return bool |
|
246 | - */ |
|
247 | - private function isCli(): bool |
|
248 | - { |
|
249 | - return PHP_SAPI === 'cli'; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * |
|
254 | - */ |
|
255 | - private function startRunStatus() |
|
256 | - { |
|
257 | - if (false === $this->isRunning) { |
|
258 | - $this->notify(Event::POOL_RUN_START); |
|
259 | - $this->isRunning = true; |
|
260 | - } |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * |
|
265 | - */ |
|
266 | - private function resetRun() |
|
267 | - { |
|
268 | - if (true === $this->isRunning) { |
|
269 | - $this->notify(Event::POOL_RUN_STOP); |
|
270 | - } |
|
271 | - $this->isRunning = false; |
|
272 | - $this->toRunThreads = $this->threads; |
|
273 | - $this->runningThreads = []; |
|
274 | - } |
|
10 | + // 0.2s |
|
11 | + private const SLEEP_TIME_MS = 50000; |
|
12 | + |
|
13 | + /** @var Thread[] $childs */ |
|
14 | + private $threads; |
|
15 | + |
|
16 | + /** @var Thread[] $toRunThreads */ |
|
17 | + private $toRunThreads; |
|
18 | + |
|
19 | + /** @var Thread[] $runningChilds */ |
|
20 | + private $runningThreads; |
|
21 | + |
|
22 | + /** @var bool $isRunning */ |
|
23 | + private $isRunning; |
|
24 | + |
|
25 | + /** @var int $maxRunningThreadNb */ |
|
26 | + private $maxRunningThreadNb; |
|
27 | + |
|
28 | + /** |
|
29 | + * ThreadPool constructor. |
|
30 | + */ |
|
31 | + public function __construct() |
|
32 | + { |
|
33 | + parent::__construct(); |
|
34 | + $this->threads = []; |
|
35 | + $this->runningThreads = []; |
|
36 | + $this->toRunThreads = []; |
|
37 | + $this->isRunning = false; |
|
38 | + $this->maxRunningThreadNb = 0; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * |
|
43 | + */ |
|
44 | + public function __destruct() |
|
45 | + { |
|
46 | + pcntl_waitpid(-1, $status, WNOHANG); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return Thread[] |
|
51 | + */ |
|
52 | + public function getThreads(): array |
|
53 | + { |
|
54 | + return $this->threads; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param Thread[] $threads |
|
59 | + * @return ThreadPool |
|
60 | + */ |
|
61 | + public function setThreads(array $threads): self |
|
62 | + { |
|
63 | + $this->threads = $threads; |
|
64 | + |
|
65 | + return $this; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param Thread $thread |
|
70 | + * @return ThreadPool |
|
71 | + */ |
|
72 | + public function addThread(Thread $thread): self |
|
73 | + { |
|
74 | + $this->threads[] = $thread; |
|
75 | + |
|
76 | + return $this; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @return int |
|
81 | + */ |
|
82 | + public function getMaxRunningThreadNb(): int |
|
83 | + { |
|
84 | + return $this->maxRunningThreadNb; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param int $maxRunningThreadNb |
|
89 | + * @return ThreadPool |
|
90 | + */ |
|
91 | + public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
92 | + { |
|
93 | + $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
94 | + |
|
95 | + return $this; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return Thread[] |
|
100 | + */ |
|
101 | + public function getToRunThreads(): array |
|
102 | + { |
|
103 | + return $this->toRunThreads; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return Thread[] |
|
108 | + */ |
|
109 | + public function getRunningThreads(): array |
|
110 | + { |
|
111 | + return $this->runningThreads; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @throws ThreadException |
|
116 | + */ |
|
117 | + public function run() |
|
118 | + { |
|
119 | + $this->checkEnv(); |
|
120 | + $this->initRun(); |
|
121 | + |
|
122 | + while ($this->isRunningThreads()) { |
|
123 | + $this->waitOnThreads(); |
|
124 | + } |
|
125 | + |
|
126 | + $this->resetRun(); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return bool |
|
131 | + * @throws ThreadException |
|
132 | + */ |
|
133 | + private function isRunningThreads(): bool |
|
134 | + { |
|
135 | + if (count($this->toRunThreads) > 0) { |
|
136 | + while (count($this->runningThreads) < $this->maxRunningThreadNb && count($this->toRunThreads) > 0) { |
|
137 | + $this->createThreadProcess(array_shift($this->toRunThreads)); |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + return count($this->runningThreads) > 0; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * can't test some part of it this since we can't unit-test in web and we're never in a child |
|
146 | + * process when pid 0 when unit-testing since the coverage is done by the parent thread |
|
147 | + * @param Thread $thread |
|
148 | + * @throws ThreadException |
|
149 | + */ |
|
150 | + private function createThreadProcess(Thread $thread) |
|
151 | + { |
|
152 | + $pid = pcntl_fork(); |
|
153 | + |
|
154 | + switch ($pid) { |
|
155 | + case -1: //error forking |
|
156 | + // @codeCoverageIgnoreStart |
|
157 | + throw new ThreadException('Error while trying to fork. Check your server installation'); |
|
158 | + // @codeCoverageIgnoreEnd |
|
159 | + case 0: // child |
|
160 | + // @codeCoverageIgnoreStart |
|
161 | + $this->processThread($thread); |
|
162 | + break; |
|
163 | + // @codeCoverageIgnoreEnd |
|
164 | + default: //parent |
|
165 | + $thread->setPid($pid); |
|
166 | + $this->runningThreads[] = $thread; |
|
167 | + $this->notify(Event::POOL_NEW_THREAD, $thread); |
|
168 | + $this->startRunStatus(); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * |
|
174 | + */ |
|
175 | + private function waitOnThreads() |
|
176 | + { |
|
177 | + $this->notify(Event::POOL_PRE_WAIT_TICK); |
|
178 | + foreach ($this->runningThreads as $k => $thread) { |
|
179 | + |
|
180 | + $res = pcntl_waitpid($thread->getPid(), $status, WNOHANG); |
|
181 | + $this->notify(Event::POOL_WAIT_TICK_PID); |
|
182 | + |
|
183 | + if ($res === -1 || $res > 0) { |
|
184 | + $this->notify(Event::POOL_WAIT_TICK_PID_REMOVED, $thread); |
|
185 | + unset($this->runningThreads[$k]); |
|
186 | + } |
|
187 | + |
|
188 | + } |
|
189 | + $this->notify(Event::POOL_POST_WAIT_TICK); |
|
190 | + |
|
191 | + usleep(self::SLEEP_TIME_MS); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the |
|
196 | + * unit-test coverage which is only done in the main process |
|
197 | + * @param Thread $thread |
|
198 | + * @throws ThreadException |
|
199 | + */ |
|
200 | + private function processThread(Thread $thread) |
|
201 | + { |
|
202 | + $this->notify(Event::THREAD_PRE_PROCESS, $thread); |
|
203 | + $response = $thread->run($thread->getProcessName()); |
|
204 | + $this->notify(Event::THREAD_POST_PROCESS, $thread); |
|
205 | + |
|
206 | + switch ($response) { |
|
207 | + case Thread::EXIT_STATUS_SUCCESS: |
|
208 | + $this->notify(Event::THREAD_EXIT_SUCCESS, $thread); |
|
209 | + break; |
|
210 | + case Thread::EXIT_STATUS_ERROR: |
|
211 | + $this->notify(Event::THREAD_EXIT_ERROR, $thread); |
|
212 | + break; |
|
213 | + default: |
|
214 | + $this->notify(Event::THREAD_EXIT_UNKNOWN, $thread); |
|
215 | + } |
|
216 | + |
|
217 | + exit($response); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Can't test the exception is not in cli since php-unit is only run in cli environment |
|
222 | + * @throws ThreadException |
|
223 | + */ |
|
224 | + private function checkEnv() |
|
225 | + { |
|
226 | + if (false === $this->isCli()) { |
|
227 | + // @codeCoverageIgnoreStart |
|
228 | + throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli'); |
|
229 | + // @codeCoverageIgnoreEnd |
|
230 | + } |
|
231 | + if (0 === count($this->threads)) { |
|
232 | + throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool'); |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * |
|
238 | + */ |
|
239 | + private function initRun() |
|
240 | + { |
|
241 | + $this->resetRun(); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return bool |
|
246 | + */ |
|
247 | + private function isCli(): bool |
|
248 | + { |
|
249 | + return PHP_SAPI === 'cli'; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * |
|
254 | + */ |
|
255 | + private function startRunStatus() |
|
256 | + { |
|
257 | + if (false === $this->isRunning) { |
|
258 | + $this->notify(Event::POOL_RUN_START); |
|
259 | + $this->isRunning = true; |
|
260 | + } |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * |
|
265 | + */ |
|
266 | + private function resetRun() |
|
267 | + { |
|
268 | + if (true === $this->isRunning) { |
|
269 | + $this->notify(Event::POOL_RUN_STOP); |
|
270 | + } |
|
271 | + $this->isRunning = false; |
|
272 | + $this->toRunThreads = $this->threads; |
|
273 | + $this->runningThreads = []; |
|
274 | + } |
|
275 | 275 | |
276 | 276 | } |
@@ -10,23 +10,23 @@ |
||
10 | 10 | class EventFactory |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @param string $eventName |
|
15 | - * @param ThreadPool|AbstractThreadPoolMediator $pool |
|
16 | - * @param Thread|null $thread |
|
17 | - * @return Event |
|
18 | - */ |
|
19 | - public static function create(string $eventName, AbstractThreadPoolMediator $pool, ?Thread $thread = null): Event |
|
20 | - { |
|
21 | - $event = new Event($eventName); |
|
22 | - $event->setThreadNb(count($pool->getThreads())); |
|
23 | - $event->setThreadDoneNb(count($pool->getThreads()) - count($pool->getToRunThreads())); |
|
24 | - $event->setMaxRunningThreadNb($pool->getMaxRunningThreadNb()); |
|
25 | - $event->setThreadLeftNb(count($pool->getToRunThreads())); |
|
26 | - $event->setRunningThreadNb(count($pool->getRunningThreads())); |
|
27 | - $event->setThread($thread); |
|
13 | + /** |
|
14 | + * @param string $eventName |
|
15 | + * @param ThreadPool|AbstractThreadPoolMediator $pool |
|
16 | + * @param Thread|null $thread |
|
17 | + * @return Event |
|
18 | + */ |
|
19 | + public static function create(string $eventName, AbstractThreadPoolMediator $pool, ?Thread $thread = null): Event |
|
20 | + { |
|
21 | + $event = new Event($eventName); |
|
22 | + $event->setThreadNb(count($pool->getThreads())); |
|
23 | + $event->setThreadDoneNb(count($pool->getThreads()) - count($pool->getToRunThreads())); |
|
24 | + $event->setMaxRunningThreadNb($pool->getMaxRunningThreadNb()); |
|
25 | + $event->setThreadLeftNb(count($pool->getToRunThreads())); |
|
26 | + $event->setRunningThreadNb(count($pool->getRunningThreads())); |
|
27 | + $event->setThread($thread); |
|
28 | 28 | |
29 | - return $event; |
|
30 | - } |
|
29 | + return $event; |
|
30 | + } |
|
31 | 31 | |
32 | 32 | } |
@@ -7,18 +7,18 @@ |
||
7 | 7 | class ThreadFactory |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * @param $processName |
|
12 | - * @param callable $function |
|
13 | - * @return Thread |
|
14 | - */ |
|
15 | - public static function create(string $processName, callable $function): Thread |
|
16 | - { |
|
17 | - $thread = new Thread(); |
|
18 | - $thread->setCallback($function); |
|
19 | - $thread->setProcessName($processName); |
|
10 | + /** |
|
11 | + * @param $processName |
|
12 | + * @param callable $function |
|
13 | + * @return Thread |
|
14 | + */ |
|
15 | + public static function create(string $processName, callable $function): Thread |
|
16 | + { |
|
17 | + $thread = new Thread(); |
|
18 | + $thread->setCallback($function); |
|
19 | + $thread->setProcessName($processName); |
|
20 | 20 | |
21 | - return $thread; |
|
22 | - } |
|
21 | + return $thread; |
|
22 | + } |
|
23 | 23 | |
24 | 24 | } |
@@ -7,186 +7,186 @@ |
||
7 | 7 | |
8 | 8 | class Event implements EventInterface |
9 | 9 | { |
10 | - const POOL_RUN_START = 'run_start'; |
|
11 | - const POOL_RUN_STOP = 'run_stop'; |
|
12 | - const POOL_NEW_THREAD = 'new_thread'; |
|
13 | - const POOL_PRE_WAIT_TICK = 'pre_wait_tick'; |
|
14 | - const POOL_POST_WAIT_TICK = 'post_wait_tick'; |
|
15 | - const POOL_WAIT_TICK_PID = 'wait_tick_pid'; |
|
16 | - const POOL_WAIT_TICK_PID_REMOVED = 'wait_tick_pid_removed'; |
|
17 | - |
|
18 | - const THREAD_PRE_PROCESS = 'pre_process'; |
|
19 | - const THREAD_POST_PROCESS = 'post_process'; |
|
20 | - const THREAD_EXIT_SUCCESS = 'exist_success'; |
|
21 | - const THREAD_EXIT_ERROR = 'exit_error'; |
|
22 | - const THREAD_EXIT_UNKNOWN = 'exit_unknown'; |
|
23 | - |
|
24 | - /** @var string $eventName */ |
|
25 | - private $eventName; |
|
26 | - |
|
27 | - /** @var int $threadNb */ |
|
28 | - private $threadNb; |
|
29 | - |
|
30 | - /** @var int $runningThreadNb */ |
|
31 | - private $runningThreadNb; |
|
32 | - |
|
33 | - /** @var int $threadDoneNb */ |
|
34 | - private $threadDoneNb; |
|
35 | - |
|
36 | - /** @var int $threadLeftNb */ |
|
37 | - private $threadLeftNb; |
|
38 | - |
|
39 | - /** @var int $maxRunningThreadNb */ |
|
40 | - private $maxRunningThreadNb; |
|
41 | - |
|
42 | - /** @var Thread $thread */ |
|
43 | - private $thread; |
|
44 | - |
|
45 | - /** |
|
46 | - * ThreadPoolEvent constructor. |
|
47 | - * @param string $eventName |
|
48 | - */ |
|
49 | - public function __construct($eventName) |
|
50 | - { |
|
51 | - $this->eventName = $eventName; |
|
52 | - $this->threadNb = 0; |
|
53 | - $this->runningThreadNb = 0; |
|
54 | - $this->threadDoneNb = 0; |
|
55 | - $this->threadLeftNb = 0; |
|
56 | - $this->maxRunningThreadNb = 0; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public function getEventName(): string |
|
63 | - { |
|
64 | - return $this->eventName; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @param string $eventName |
|
69 | - * @return Event |
|
70 | - */ |
|
71 | - public function setEventName(string $eventName): self |
|
72 | - { |
|
73 | - $this->eventName = $eventName; |
|
74 | - |
|
75 | - return $this; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @return int |
|
80 | - */ |
|
81 | - public function getThreadNb(): int |
|
82 | - { |
|
83 | - return $this->threadNb; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @param int $threadNb |
|
88 | - * @return Event |
|
89 | - */ |
|
90 | - public function setThreadNb(int $threadNb): self |
|
91 | - { |
|
92 | - $this->threadNb = $threadNb; |
|
93 | - |
|
94 | - return $this; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @return int |
|
99 | - */ |
|
100 | - public function getThreadDoneNb(): int |
|
101 | - { |
|
102 | - return $this->threadDoneNb; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param int $threadDoneNb |
|
107 | - * @return Event |
|
108 | - */ |
|
109 | - public function setThreadDoneNb(int $threadDoneNb): self |
|
110 | - { |
|
111 | - $this->threadDoneNb = $threadDoneNb; |
|
112 | - |
|
113 | - return $this; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @return int |
|
118 | - */ |
|
119 | - public function getThreadLeftNb(): int |
|
120 | - { |
|
121 | - return $this->threadLeftNb; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param int $threadLeftNb |
|
126 | - * @return Event |
|
127 | - */ |
|
128 | - public function setThreadLeftNb(int $threadLeftNb): self |
|
129 | - { |
|
130 | - $this->threadLeftNb = $threadLeftNb; |
|
131 | - |
|
132 | - return $this; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @return int |
|
137 | - */ |
|
138 | - public function getMaxRunningThreadNb(): int |
|
139 | - { |
|
140 | - return $this->maxRunningThreadNb; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @param int $maxRunningThreadNb |
|
145 | - * @return Event |
|
146 | - */ |
|
147 | - public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
148 | - { |
|
149 | - $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
150 | - |
|
151 | - return $this; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @return int |
|
156 | - */ |
|
157 | - public function getRunningThreadNb(): int |
|
158 | - { |
|
159 | - return $this->runningThreadNb; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param int $runningThreadNb |
|
164 | - * @return Event |
|
165 | - */ |
|
166 | - public function setRunningThreadNb(int $runningThreadNb): self |
|
167 | - { |
|
168 | - $this->runningThreadNb = $runningThreadNb; |
|
169 | - |
|
170 | - return $this; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @return Thread |
|
175 | - */ |
|
176 | - public function getThread(): ?Thread |
|
177 | - { |
|
178 | - return $this->thread; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @param Thread|null $thread |
|
183 | - * @return $this |
|
184 | - */ |
|
185 | - public function setThread(Thread $thread = null): self |
|
186 | - { |
|
187 | - $this->thread = $thread; |
|
188 | - |
|
189 | - return $this; |
|
190 | - } |
|
10 | + const POOL_RUN_START = 'run_start'; |
|
11 | + const POOL_RUN_STOP = 'run_stop'; |
|
12 | + const POOL_NEW_THREAD = 'new_thread'; |
|
13 | + const POOL_PRE_WAIT_TICK = 'pre_wait_tick'; |
|
14 | + const POOL_POST_WAIT_TICK = 'post_wait_tick'; |
|
15 | + const POOL_WAIT_TICK_PID = 'wait_tick_pid'; |
|
16 | + const POOL_WAIT_TICK_PID_REMOVED = 'wait_tick_pid_removed'; |
|
17 | + |
|
18 | + const THREAD_PRE_PROCESS = 'pre_process'; |
|
19 | + const THREAD_POST_PROCESS = 'post_process'; |
|
20 | + const THREAD_EXIT_SUCCESS = 'exist_success'; |
|
21 | + const THREAD_EXIT_ERROR = 'exit_error'; |
|
22 | + const THREAD_EXIT_UNKNOWN = 'exit_unknown'; |
|
23 | + |
|
24 | + /** @var string $eventName */ |
|
25 | + private $eventName; |
|
26 | + |
|
27 | + /** @var int $threadNb */ |
|
28 | + private $threadNb; |
|
29 | + |
|
30 | + /** @var int $runningThreadNb */ |
|
31 | + private $runningThreadNb; |
|
32 | + |
|
33 | + /** @var int $threadDoneNb */ |
|
34 | + private $threadDoneNb; |
|
35 | + |
|
36 | + /** @var int $threadLeftNb */ |
|
37 | + private $threadLeftNb; |
|
38 | + |
|
39 | + /** @var int $maxRunningThreadNb */ |
|
40 | + private $maxRunningThreadNb; |
|
41 | + |
|
42 | + /** @var Thread $thread */ |
|
43 | + private $thread; |
|
44 | + |
|
45 | + /** |
|
46 | + * ThreadPoolEvent constructor. |
|
47 | + * @param string $eventName |
|
48 | + */ |
|
49 | + public function __construct($eventName) |
|
50 | + { |
|
51 | + $this->eventName = $eventName; |
|
52 | + $this->threadNb = 0; |
|
53 | + $this->runningThreadNb = 0; |
|
54 | + $this->threadDoneNb = 0; |
|
55 | + $this->threadLeftNb = 0; |
|
56 | + $this->maxRunningThreadNb = 0; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public function getEventName(): string |
|
63 | + { |
|
64 | + return $this->eventName; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @param string $eventName |
|
69 | + * @return Event |
|
70 | + */ |
|
71 | + public function setEventName(string $eventName): self |
|
72 | + { |
|
73 | + $this->eventName = $eventName; |
|
74 | + |
|
75 | + return $this; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @return int |
|
80 | + */ |
|
81 | + public function getThreadNb(): int |
|
82 | + { |
|
83 | + return $this->threadNb; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @param int $threadNb |
|
88 | + * @return Event |
|
89 | + */ |
|
90 | + public function setThreadNb(int $threadNb): self |
|
91 | + { |
|
92 | + $this->threadNb = $threadNb; |
|
93 | + |
|
94 | + return $this; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @return int |
|
99 | + */ |
|
100 | + public function getThreadDoneNb(): int |
|
101 | + { |
|
102 | + return $this->threadDoneNb; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param int $threadDoneNb |
|
107 | + * @return Event |
|
108 | + */ |
|
109 | + public function setThreadDoneNb(int $threadDoneNb): self |
|
110 | + { |
|
111 | + $this->threadDoneNb = $threadDoneNb; |
|
112 | + |
|
113 | + return $this; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @return int |
|
118 | + */ |
|
119 | + public function getThreadLeftNb(): int |
|
120 | + { |
|
121 | + return $this->threadLeftNb; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param int $threadLeftNb |
|
126 | + * @return Event |
|
127 | + */ |
|
128 | + public function setThreadLeftNb(int $threadLeftNb): self |
|
129 | + { |
|
130 | + $this->threadLeftNb = $threadLeftNb; |
|
131 | + |
|
132 | + return $this; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @return int |
|
137 | + */ |
|
138 | + public function getMaxRunningThreadNb(): int |
|
139 | + { |
|
140 | + return $this->maxRunningThreadNb; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @param int $maxRunningThreadNb |
|
145 | + * @return Event |
|
146 | + */ |
|
147 | + public function setMaxRunningThreadNb(int $maxRunningThreadNb): self |
|
148 | + { |
|
149 | + $this->maxRunningThreadNb = $maxRunningThreadNb; |
|
150 | + |
|
151 | + return $this; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @return int |
|
156 | + */ |
|
157 | + public function getRunningThreadNb(): int |
|
158 | + { |
|
159 | + return $this->runningThreadNb; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param int $runningThreadNb |
|
164 | + * @return Event |
|
165 | + */ |
|
166 | + public function setRunningThreadNb(int $runningThreadNb): self |
|
167 | + { |
|
168 | + $this->runningThreadNb = $runningThreadNb; |
|
169 | + |
|
170 | + return $this; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @return Thread |
|
175 | + */ |
|
176 | + public function getThread(): ?Thread |
|
177 | + { |
|
178 | + return $this->thread; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @param Thread|null $thread |
|
183 | + * @return $this |
|
184 | + */ |
|
185 | + public function setThread(Thread $thread = null): self |
|
186 | + { |
|
187 | + $this->thread = $thread; |
|
188 | + |
|
189 | + return $this; |
|
190 | + } |
|
191 | 191 | |
192 | 192 | } |
@@ -13,26 +13,26 @@ |
||
13 | 13 | */ |
14 | 14 | class ListenerTest extends TestCase |
15 | 15 | { |
16 | - /** @var Listener */ |
|
17 | - private $listener; |
|
16 | + /** @var Listener */ |
|
17 | + private $listener; |
|
18 | 18 | |
19 | - /** @var callable */ |
|
20 | - private $callback; |
|
19 | + /** @var callable */ |
|
20 | + private $callback; |
|
21 | 21 | |
22 | - public function setUp() |
|
23 | - { |
|
24 | - $this->callback = function(){ |
|
25 | - }; |
|
26 | - $this->listener = new Listener(Event::POOL_NEW_THREAD, $this->callback); |
|
27 | - } |
|
22 | + public function setUp() |
|
23 | + { |
|
24 | + $this->callback = function(){ |
|
25 | + }; |
|
26 | + $this->listener = new Listener(Event::POOL_NEW_THREAD, $this->callback); |
|
27 | + } |
|
28 | 28 | |
29 | - public function test_listener() |
|
30 | - { |
|
31 | - $this->assertSame(Event::POOL_NEW_THREAD, $this->listener->getEventName()); |
|
32 | - $this->assertSame($this->listener, $this->listener->setEventName(Event::POOL_POST_WAIT_TICK)); |
|
33 | - $this->assertSame(Event::POOL_POST_WAIT_TICK, $this->listener->getEventName()); |
|
34 | - $this->assertSame($this->listener, $this->listener->setCallback($this->callback)); |
|
35 | - $this->listener->notify(new Event('test')); |
|
36 | - } |
|
29 | + public function test_listener() |
|
30 | + { |
|
31 | + $this->assertSame(Event::POOL_NEW_THREAD, $this->listener->getEventName()); |
|
32 | + $this->assertSame($this->listener, $this->listener->setEventName(Event::POOL_POST_WAIT_TICK)); |
|
33 | + $this->assertSame(Event::POOL_POST_WAIT_TICK, $this->listener->getEventName()); |
|
34 | + $this->assertSame($this->listener, $this->listener->setCallback($this->callback)); |
|
35 | + $this->listener->notify(new Event('test')); |
|
36 | + } |
|
37 | 37 | |
38 | 38 | } |
@@ -14,25 +14,25 @@ |
||
14 | 14 | */ |
15 | 15 | class MediatorTest extends TestCase |
16 | 16 | { |
17 | - /** @var Mediator */ |
|
18 | - private $mediator; |
|
17 | + /** @var Mediator */ |
|
18 | + private $mediator; |
|
19 | 19 | |
20 | - public function setUp() |
|
21 | - { |
|
22 | - $this->mediator = new Mediator(); |
|
23 | - } |
|
20 | + public function setUp() |
|
21 | + { |
|
22 | + $this->mediator = new Mediator(); |
|
23 | + } |
|
24 | 24 | |
25 | - public function test_listeners() |
|
26 | - { |
|
27 | - $listener = new Listener(Event::POOL_NEW_THREAD, function() { |
|
28 | - }); |
|
29 | - $this->assertSame([], $this->mediator->getListeners()); |
|
30 | - $this->assertSame($this->mediator, $this->mediator->addListener($listener)); |
|
31 | - $this->assertSame([Event::POOL_NEW_THREAD => [$listener]], $this->mediator->getListeners()); |
|
32 | - $this->assertSame($this->mediator, $this->mediator->removeListener(new Listener('fake', function(){ |
|
33 | - }))); |
|
34 | - $this->mediator->notify(Event::POOL_NEW_THREAD, new Event('unit-event')); |
|
35 | - $this->assertSame($this->mediator, $this->mediator->removeListener($listener)); |
|
36 | - } |
|
25 | + public function test_listeners() |
|
26 | + { |
|
27 | + $listener = new Listener(Event::POOL_NEW_THREAD, function() { |
|
28 | + }); |
|
29 | + $this->assertSame([], $this->mediator->getListeners()); |
|
30 | + $this->assertSame($this->mediator, $this->mediator->addListener($listener)); |
|
31 | + $this->assertSame([Event::POOL_NEW_THREAD => [$listener]], $this->mediator->getListeners()); |
|
32 | + $this->assertSame($this->mediator, $this->mediator->removeListener(new Listener('fake', function(){ |
|
33 | + }))); |
|
34 | + $this->mediator->notify(Event::POOL_NEW_THREAD, new Event('unit-event')); |
|
35 | + $this->assertSame($this->mediator, $this->mediator->removeListener($listener)); |
|
36 | + } |
|
37 | 37 | |
38 | 38 | } |
@@ -17,96 +17,96 @@ |
||
17 | 17 | */ |
18 | 18 | class ThreadPoolTest extends TestCase |
19 | 19 | { |
20 | - /** @var ThreadPool */ |
|
21 | - private $threadPool; |
|
22 | - |
|
23 | - protected function setUp() |
|
24 | - { |
|
25 | - $this->threadPool = new ThreadPool(); |
|
26 | - } |
|
27 | - |
|
28 | - public function test_constructor_destructor() |
|
29 | - { |
|
30 | - $this->assertAttributeEquals([], 'threads', $this->threadPool); |
|
31 | - $this->assertAttributeEquals([], 'runningThreads', $this->threadPool); |
|
32 | - $this->assertAttributeEquals([], 'toRunThreads', $this->threadPool); |
|
33 | - $this->assertAttributeEquals(false, 'isRunning', $this->threadPool); |
|
34 | - $this->assertAttributeEquals(0, 'maxRunningThreadNb', $this->threadPool); |
|
35 | - $this->assertSame(Mediator::class, get_class($this->threadPool->getMediator())); |
|
36 | - |
|
37 | - unset($this->threadPool); |
|
38 | - } |
|
39 | - |
|
40 | - public function test_getThreads() |
|
41 | - { |
|
42 | - $threads = [new Thread()]; |
|
43 | - $this->assertSame($this->threadPool, $this->threadPool->setThreads($threads)); |
|
44 | - $this->assertSame($threads, $this->threadPool->getThreads()); |
|
45 | - } |
|
46 | - |
|
47 | - public function test_addThread() |
|
48 | - { |
|
49 | - $thread = new Thread(); |
|
50 | - $this->assertSame($this->threadPool, $this->threadPool->addThread($thread)); |
|
51 | - $this->assertContains($thread, $this->threadPool->getThreads()); |
|
52 | - } |
|
53 | - |
|
54 | - public function test_getMaxRunningThreadNb() |
|
55 | - { |
|
56 | - $this->assertSame(0, $this->threadPool->getMaxRunningThreadNb()); |
|
57 | - } |
|
58 | - |
|
59 | - public function test_setMaxRunningThreadNb() |
|
60 | - { |
|
61 | - $this->assertSame($this->threadPool, $this->threadPool->setMaxRunningThreadNb(10)); |
|
62 | - $this->assertSame(10, $this->threadPool->getMaxRunningThreadNb()); |
|
63 | - } |
|
64 | - |
|
65 | - public function test_getToRunThreads() |
|
66 | - { |
|
67 | - $this->assertSame([], $this->threadPool->getToRunThreads()); |
|
68 | - } |
|
69 | - |
|
70 | - public function test_getRunningThreads() |
|
71 | - { |
|
72 | - $this->assertSame([], $this->threadPool->getRunningThreads()); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @throws \Wonderland\Thread\Exception\ThreadException |
|
77 | - */ |
|
78 | - public function test_run() |
|
79 | - { |
|
80 | - $thread = new Thread(); |
|
81 | - $thread->setCallback(function (){ return 0; |
|
82 | - |
|
83 | - }); |
|
84 | - $thread->setProcessName('unitTest'); |
|
85 | - $threads = []; |
|
86 | - $listener = new Listener(Event::POOL_NEW_THREAD, function (){ |
|
87 | - }); |
|
88 | - |
|
89 | - $this->threadPool->setMaxRunningThreadNb(10); |
|
90 | - $this->assertSame($this->threadPool, $this->threadPool->addListener($listener)); |
|
91 | - for ($i = 0; $i < 20; |
|
20 | + /** @var ThreadPool */ |
|
21 | + private $threadPool; |
|
22 | + |
|
23 | + protected function setUp() |
|
24 | + { |
|
25 | + $this->threadPool = new ThreadPool(); |
|
26 | + } |
|
27 | + |
|
28 | + public function test_constructor_destructor() |
|
29 | + { |
|
30 | + $this->assertAttributeEquals([], 'threads', $this->threadPool); |
|
31 | + $this->assertAttributeEquals([], 'runningThreads', $this->threadPool); |
|
32 | + $this->assertAttributeEquals([], 'toRunThreads', $this->threadPool); |
|
33 | + $this->assertAttributeEquals(false, 'isRunning', $this->threadPool); |
|
34 | + $this->assertAttributeEquals(0, 'maxRunningThreadNb', $this->threadPool); |
|
35 | + $this->assertSame(Mediator::class, get_class($this->threadPool->getMediator())); |
|
36 | + |
|
37 | + unset($this->threadPool); |
|
38 | + } |
|
39 | + |
|
40 | + public function test_getThreads() |
|
41 | + { |
|
42 | + $threads = [new Thread()]; |
|
43 | + $this->assertSame($this->threadPool, $this->threadPool->setThreads($threads)); |
|
44 | + $this->assertSame($threads, $this->threadPool->getThreads()); |
|
45 | + } |
|
46 | + |
|
47 | + public function test_addThread() |
|
48 | + { |
|
49 | + $thread = new Thread(); |
|
50 | + $this->assertSame($this->threadPool, $this->threadPool->addThread($thread)); |
|
51 | + $this->assertContains($thread, $this->threadPool->getThreads()); |
|
52 | + } |
|
53 | + |
|
54 | + public function test_getMaxRunningThreadNb() |
|
55 | + { |
|
56 | + $this->assertSame(0, $this->threadPool->getMaxRunningThreadNb()); |
|
57 | + } |
|
58 | + |
|
59 | + public function test_setMaxRunningThreadNb() |
|
60 | + { |
|
61 | + $this->assertSame($this->threadPool, $this->threadPool->setMaxRunningThreadNb(10)); |
|
62 | + $this->assertSame(10, $this->threadPool->getMaxRunningThreadNb()); |
|
63 | + } |
|
64 | + |
|
65 | + public function test_getToRunThreads() |
|
66 | + { |
|
67 | + $this->assertSame([], $this->threadPool->getToRunThreads()); |
|
68 | + } |
|
69 | + |
|
70 | + public function test_getRunningThreads() |
|
71 | + { |
|
72 | + $this->assertSame([], $this->threadPool->getRunningThreads()); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @throws \Wonderland\Thread\Exception\ThreadException |
|
77 | + */ |
|
78 | + public function test_run() |
|
79 | + { |
|
80 | + $thread = new Thread(); |
|
81 | + $thread->setCallback(function (){ return 0; |
|
82 | + |
|
83 | + }); |
|
84 | + $thread->setProcessName('unitTest'); |
|
85 | + $threads = []; |
|
86 | + $listener = new Listener(Event::POOL_NEW_THREAD, function (){ |
|
87 | + }); |
|
88 | + |
|
89 | + $this->threadPool->setMaxRunningThreadNb(10); |
|
90 | + $this->assertSame($this->threadPool, $this->threadPool->addListener($listener)); |
|
91 | + for ($i = 0; $i < 20; |
|
92 | 92 | $i++) { $threads[] = clone $thread; |
93 | - } |
|
94 | - $this->assertSame($this->threadPool, $this->threadPool->addThread($thread)); |
|
95 | - $this->assertSame($this->threadPool, $this->threadPool->setThreads($threads)); |
|
93 | + } |
|
94 | + $this->assertSame($this->threadPool, $this->threadPool->addThread($thread)); |
|
95 | + $this->assertSame($this->threadPool, $this->threadPool->setThreads($threads)); |
|
96 | 96 | |
97 | - $this->threadPool->run(); |
|
97 | + $this->threadPool->run(); |
|
98 | 98 | |
99 | - $this->assertSame($this->threadPool, $this->threadPool->removeListener($listener)); |
|
100 | - } |
|
99 | + $this->assertSame($this->threadPool, $this->threadPool->removeListener($listener)); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * @throws ThreadException |
|
104 | - */ |
|
105 | - public function test_run_exception() |
|
106 | - { |
|
107 | - $this->expectException(ThreadException::class); |
|
108 | - $this->threadPool->run(); |
|
102 | + /** |
|
103 | + * @throws ThreadException |
|
104 | + */ |
|
105 | + public function test_run_exception() |
|
106 | + { |
|
107 | + $this->expectException(ThreadException::class); |
|
108 | + $this->threadPool->run(); |
|
109 | 109 | |
110 | - } |
|
110 | + } |
|
111 | 111 | |
112 | 112 | } |
@@ -14,16 +14,16 @@ |
||
14 | 14 | class ThreadFactoryTest extends TestCase |
15 | 15 | { |
16 | 16 | |
17 | - public function test_create() |
|
18 | - { |
|
19 | - $callback = function(){ |
|
20 | - }; |
|
21 | - $thread = new Thread(); |
|
22 | - $thread->setProcessName('unit-test'); |
|
23 | - $thread->setCallback($callback); |
|
17 | + public function test_create() |
|
18 | + { |
|
19 | + $callback = function(){ |
|
20 | + }; |
|
21 | + $thread = new Thread(); |
|
22 | + $thread->setProcessName('unit-test'); |
|
23 | + $thread->setCallback($callback); |
|
24 | 24 | |
25 | - $this->assertSame($thread->getCallback(), ThreadFactory::create('unit-test', $callback)->getCallback()); |
|
26 | - $this->assertSame($thread->getProcessName(), ThreadFactory::create('unit-test', $callback)->getProcessName()); |
|
27 | - } |
|
25 | + $this->assertSame($thread->getCallback(), ThreadFactory::create('unit-test', $callback)->getCallback()); |
|
26 | + $this->assertSame($thread->getProcessName(), ThreadFactory::create('unit-test', $callback)->getProcessName()); |
|
27 | + } |
|
28 | 28 | |
29 | 29 | } |