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 vxm\async\Event; |
11
|
|
|
use Yii; |
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
use vxm\async\Async; |
15
|
|
|
use vxm\async\ErrorEvent; |
16
|
|
|
use vxm\async\SuccessEvent; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AsyncTest |
20
|
|
|
* |
21
|
|
|
* @author Vuong Minh <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class AsyncTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testAsync() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$time = time(); |
30
|
|
|
|
31
|
|
|
Yii::$app->async(function () { |
32
|
|
|
sleep(10); |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
$this->assertTrue((time() - $time) < 10); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testAwait() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$time = time(); |
41
|
|
|
|
42
|
|
|
Yii::$app->async(function () { |
43
|
|
|
sleep(10); |
44
|
|
|
})->await(); |
45
|
|
|
|
46
|
|
|
$this->assertTrue((time() - $time) >= 10); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSuccessEvent() |
50
|
|
|
{ |
51
|
|
|
Yii::$app->async->on(Async::EVENT_SUCCESS, function (SuccessEvent $event) { |
52
|
|
|
|
53
|
|
|
$this->assertEquals(123, $event->output); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
Yii::$app->async(function () { |
57
|
|
|
|
58
|
|
|
return 123; |
59
|
|
|
}, [ |
60
|
|
|
'success' => function ($result) { |
61
|
|
|
|
62
|
|
|
$this->assertEquals(123, $result); |
63
|
|
|
} |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testErrorEvent() |
68
|
|
|
{ |
69
|
|
|
Yii::$app->async->on(Async::EVENT_ERROR, function (ErrorEvent $event) { |
70
|
|
|
|
71
|
|
|
$this->assertEquals(Exception::class, get_class($event->throwable)); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
Yii::$app->async(function () { |
75
|
|
|
|
76
|
|
|
throw new Exception('Error'); |
77
|
|
|
}, [ |
78
|
|
|
'error' => function (Exception $exception) { |
79
|
|
|
|
80
|
|
|
$this->assertEquals(Exception::class, get_class($exception)); |
81
|
|
|
} |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testTimeoutEvent() |
86
|
|
|
{ |
87
|
|
|
Yii::$app->async->on(Async::EVENT_ERROR, function (Event $event) { |
|
|
|
|
88
|
|
|
|
89
|
|
|
echo 'global passed'; |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
Yii::$app->async(function () { |
93
|
|
|
|
94
|
|
|
sleep(30); |
95
|
|
|
}, [ |
96
|
|
|
'timeout' => function () { |
97
|
|
|
|
98
|
|
|
echo 'custom passed'; |
99
|
|
|
} |
100
|
|
|
])->await(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.