|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/vuongxuongminh/yii2-async |
|
4
|
|
|
* @copyright Copyright (c) 2019 Vuong Xuong Minh |
|
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace vxm\test\unit\async; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
|
|
14
|
|
|
use vxm\async\Async; |
|
15
|
|
|
use vxm\async\event\Event; |
|
16
|
|
|
use vxm\async\event\ErrorEvent; |
|
17
|
|
|
use vxm\async\event\SuccessEvent; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AsyncTest |
|
21
|
|
|
* |
|
22
|
|
|
* @author Vuong Minh <[email protected]> |
|
23
|
|
|
* @since 1.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class AsyncTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
View Code Duplication |
public function testAsync() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->stopwatch->start('test'); |
|
31
|
|
|
|
|
32
|
|
|
Yii::$app->async->run(function () { |
|
33
|
|
|
sleep(3); |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertLessThan(3000, $this->stopwatch->stop('test')->getDuration()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
View Code Duplication |
public function testWait() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->stopwatch->start('test'); |
|
42
|
|
|
|
|
43
|
|
|
Yii::$app->async->run(function () { |
|
44
|
|
|
sleep(3); |
|
45
|
|
|
})->wait(); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertGreaterThan(3000, $this->stopwatch->stop('test')->getDuration()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testSuccessEvent() |
|
51
|
|
|
{ |
|
52
|
|
|
$result = 0; |
|
53
|
|
|
|
|
54
|
|
|
Yii::$app->async->on(Async::EVENT_SUCCESS, function (SuccessEvent $event) use (&$result) { |
|
55
|
|
|
$result += $event->output; |
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
Yii::$app->async->run(function () { |
|
59
|
|
|
|
|
60
|
|
|
return 5; |
|
61
|
|
|
}, [ |
|
62
|
|
|
'success' => function ($output) use (&$result) { |
|
63
|
|
|
$result += $output; |
|
64
|
|
|
} |
|
65
|
|
|
])->wait(); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals(10, $result); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testErrorEvent() |
|
71
|
|
|
{ |
|
72
|
|
|
/** @var Exception[] $exceptions */ |
|
73
|
|
|
$exceptions = []; |
|
74
|
|
|
Yii::$app->async->on(Async::EVENT_ERROR, function (ErrorEvent $event) use (&$exceptions) { |
|
75
|
|
|
|
|
76
|
|
|
$exceptions[] = $event->throwable; |
|
77
|
|
|
}); |
|
78
|
|
|
|
|
79
|
|
|
Yii::$app->async->run(function () { |
|
80
|
|
|
|
|
81
|
|
|
throw new Exception('Error'); |
|
82
|
|
|
}, [ |
|
83
|
|
|
'error' => function (Exception $exception) use (&$exceptions) { |
|
84
|
|
|
|
|
85
|
|
|
$exceptions[] = $exception; |
|
86
|
|
|
} |
|
87
|
|
|
])->wait(); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($exceptions as $exception) { |
|
90
|
|
|
|
|
91
|
|
|
$this->assertEquals(Exception::class, get_class($exception)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testTimeoutEvent() |
|
97
|
|
|
{ |
|
98
|
|
|
$counter = 0; |
|
99
|
|
|
|
|
100
|
|
|
Yii::$app->async->on(Async::EVENT_TIMEOUT, function (Event $event) use (&$counter) { |
|
|
|
|
|
|
101
|
|
|
$counter++; |
|
102
|
|
|
}); |
|
103
|
|
|
|
|
104
|
|
|
Yii::$app->async->run(function () { |
|
105
|
|
|
|
|
106
|
|
|
sleep(6); |
|
107
|
|
|
}, [ |
|
108
|
|
|
'timeout' => function () use (&$counter) { |
|
109
|
|
|
$counter++; |
|
110
|
|
|
} |
|
111
|
|
|
])->wait(); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertEquals(2, $counter); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.