44
55use Amp \Future ;
66use Amp \Parallel \Context \StatusError ;
7+ use Amp \Parallel \Test \Worker \Fixtures \TestTask ;
78use Amp \Parallel \Worker \ContextWorkerFactory ;
89use Amp \Parallel \Worker \ContextWorkerPool ;
10+ use Amp \Parallel \Worker \Execution ;
911use Amp \Parallel \Worker \Task ;
1012use Amp \Parallel \Worker \Worker ;
1113use Amp \Parallel \Worker \WorkerPool ;
@@ -81,25 +83,25 @@ public function testBusyPool(): void
8183 return new Fixtures \TestTask ($ value );
8284 }, $ values );
8385
84- $ promises = \array_map (function (Task $ task ) use ($ pool ): Future {
86+ $ futures = \array_map (function (Task $ task ) use ($ pool ): Future {
8587 return $ pool ->submit ($ task )->getFuture ();
8688 }, $ tasks );
8789
88- self ::assertEquals ($ values , Future \await ($ promises ));
90+ self ::assertEquals ($ values , Future \await ($ futures ));
8991
90- $ promises = \array_map (function (Task $ task ) use ($ pool ): Future {
92+ $ futures = \array_map (function (Task $ task ) use ($ pool ): Future {
9193 return $ pool ->submit ($ task )->getFuture ();
9294 }, $ tasks );
9395
94- self ::assertEquals ($ values , Future \await ($ promises ));
96+ self ::assertEquals ($ values , Future \await ($ futures ));
9597
9698 $ pool ->shutdown ();
9799 }
98100
99101 public function testCreatePoolShouldThrowError (): void
100102 {
101103 $ this ->expectException (\Error::class);
102- $ this ->expectExceptionMessage ('Maximum size must be a non-negative integer ' );
104+ $ this ->expectExceptionMessage ('Maximum size must be a positive integer ' );
103105
104106 $ this ->createPool (-1 );
105107 }
@@ -112,28 +114,60 @@ public function testCleanGarbageCollection(): void
112114
113115 $ values = \range (1 , 50 );
114116
115- $ promises = \array_map (static function (int $ value ) use ($ pool ): Future {
117+ $ futures = \array_map (static function (int $ value ) use ($ pool ): Future {
116118 return $ pool ->submit (new Fixtures \TestTask ($ value ))->getFuture ();
117119 }, $ values );
118120
119- self ::assertEquals ($ values , Future \await ($ promises ));
121+ self ::assertEquals ($ values , Future \await ($ futures ));
120122 }
121123 }
122124
125+ /**
126+ * @see https://github.com/amphp/parallel/issues/66
127+ */
123128 public function testPooledKill (): void
124129 {
125130 $ this ->setTimeout (10 );
131+ \set_error_handler (static function (int $ errno , string $ errstr ) use (&$ error ): void {
132+ $ error = $ errstr ;
133+ });
134+
135+ try {
136+ $ pool = $ this ->createPool (1 );
137+ $ worker1 = $ pool ->getWorker ();
138+ $ worker1 ->kill ();
139+ self ::assertFalse ($ worker1 ->isRunning ());
140+
141+ unset($ worker1 ); // Destroying the worker will trigger the pool to recognize it has been killed.
142+
143+ $ worker2 = $ pool ->getWorker ();
144+ self ::assertTrue ($ worker2 ->isRunning ());
145+
146+ self ::assertStringContainsString ('Worker in pool crashed ' , $ error );
147+ } finally {
148+ \restore_error_handler ();
149+ }
150+ }
151+
152+ /**
153+ * @see https://github.com/amphp/parallel/issues/177
154+ */
155+ public function testWaitingForAvailableWorker (): void
156+ {
157+ $ count = 4 ;
158+ $ delay = 0.1 ;
159+
160+ $ this ->setMinimumRuntime ($ delay * $ count );
161+ $ this ->setTimeout ($ delay * $ count + $ delay );
126162
127- // See https://github.com/amphp/parallel/issues/66
128163 $ pool = $ this ->createPool (1 );
129- $ worker1 = $ pool ->getWorker ();
130- $ worker1 ->kill ();
131- self ::assertFalse ($ worker1 ->isRunning ());
132164
133- unset($ worker1 ); // Destroying the worker will trigger the pool to recognize it has been killed.
165+ $ executions = [];
166+ for ($ i = 0 ; $ i < $ count ; $ i ++) {
167+ $ executions [] = $ pool ->submit (new TestTask ($ i , $ delay ));
168+ }
134169
135- $ worker2 = $ pool ->getWorker ();
136- self ::assertTrue ($ worker2 ->isRunning ());
170+ Future \await (\array_map (fn (Execution $ e ) => $ e ->getFuture (), $ executions ));
137171 }
138172
139173 protected function createWorker (?string $ autoloadPath = null ): Worker
0 commit comments