|
6 | 6 | use Amp\Iterator;
|
7 | 7 | use Amp\Loop;
|
8 | 8 | use Amp\Producer;
|
| 9 | +use Amp\Promise; |
| 10 | +use ScriptFUSION\Async\Throttle\DualThrottle; |
9 | 11 | use ScriptFUSION\Porter\Collection\AsyncRecordCollection;
|
10 | 12 | use ScriptFUSION\Porter\Collection\CountableAsyncPorterRecords;
|
11 | 13 | use ScriptFUSION\Porter\Collection\CountableAsyncProviderRecords;
|
|
22 | 24 | use ScriptFUSION\Porter\Transform\AsyncTransformer;
|
23 | 25 | use ScriptFUSION\Porter\Transform\FilterTransformer;
|
24 | 26 | use ScriptFUSIONTest\MockFactory;
|
| 27 | +use function Amp\call; |
25 | 28 |
|
26 | 29 | /**
|
27 | 30 | * @see Porter
|
@@ -200,20 +203,44 @@ public function testPorterAwareAsyncTransformer(): void
|
200 | 203 | }
|
201 | 204 |
|
202 | 205 | /**
|
203 |
| - * Tests that the throttle is invoked during fetch operations. |
| 206 | + * Tests that a working throttle implementation is invoked during fetch operations. |
204 | 207 | */
|
205 | 208 | public function testThrottle(): \Generator
|
206 | 209 | {
|
207 |
| - $throttle = $this->specification->getThrottle(); |
208 |
| - $throttle->setMaxPerSecond(1); |
209 |
| - |
210 |
| - $start = microtime(true); |
| 210 | + $this->specification->setThrottle($throttle = new DualThrottle); |
| 211 | + $throttle->setMaxConcurrency(1); |
211 | 212 |
|
212 | 213 | $records = $this->porter->importAsync($this->specification);
|
213 | 214 | self::assertTrue($throttle->isThrottling());
|
214 | 215 |
|
215 | 216 | yield $records->advance();
|
216 | 217 | self::assertFalse($throttle->isThrottling());
|
217 |
| - self::assertGreaterThan(1, microtime(true) - $start); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Tests that a working throttle implementation can be called from multiple fibers queueing excess objects. |
| 222 | + */ |
| 223 | + public function testThrottleConcurrentFibers(): \Generator |
| 224 | + { |
| 225 | + $this->specification->setThrottle($throttle = new DualThrottle); |
| 226 | + $throttle->setMaxPerSecond(1); |
| 227 | + |
| 228 | + $import = function (): Promise { |
| 229 | + return call( |
| 230 | + function (): \Generator { |
| 231 | + $records = $this->porter->importAsync($this->specification); |
| 232 | + |
| 233 | + while (yield $records->advance()) { |
| 234 | + // Do nothing. |
| 235 | + } |
| 236 | + } |
| 237 | + ); |
| 238 | + }; |
| 239 | + |
| 240 | + $start = microtime(true); |
| 241 | + |
| 242 | + yield [$import(), $import(), $import()]; |
| 243 | + |
| 244 | + self::assertGreaterThan(3, microtime(true) - $start); |
218 | 245 | }
|
219 | 246 | }
|
0 commit comments