forked from birdwyx/phpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path131_go_time_basic_test.phpt
157 lines (135 loc) · 3.31 KB
/
131_go_time_basic_test.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
--TEST--
Go time basic test
--FILE--
<?php
use \Go\Chan;
use \Go\Time;
use \Go\Scheduler;
function subtc($seq){
echo "SUB-TC: #$seq\n";
}
go_debug(0);
subtc(1);
echo Time::NANOSECOND . PHP_EOL;
echo Time::MICROSECOND . PHP_EOL;
echo Time::MILLISECOND . PHP_EOL;
echo Time::SECOND . PHP_EOL;
echo Time::MINUTE . PHP_EOL;
echo Time::HOUR . PHP_EOL;
subtc(2);
echo "verify sleep of 1 second : outside of go routine: ";
$t = microtime(true);
Time::sleep(Time::SECOND);
$t1 = microtime(true);
if ( $t1 - $t > 1.1 || $t1 - $t < 0.9 ){
echo "should sleep 1 second but slept "
. ( $t1-$t ) . " seconds\n";
}else{
echo "pass\n";
}
subtc(3);
go(function (){ //go 1
$chan = Time::After(1*Time::SECOND); //go 2
go(function () use($chan){ //go 3
$time0 = time(); $mtime0 = microtime(true);
while( true ){
$v = $chan->tryPop() ;
if( !empty($v) ) break;
Time::sleep(20 * Time::MILLISECOND); // = usleep(20 * 1000);
if( microtime(true)-$mtime0 > 1.05 ) {
echo "timer should expire at 1 second but did not expire until "
. ( microtime(true)-$mtime0 ) . " seconds\n";
return;
}
}
if( abs(microtime(true) - $v) > 0.05 ){
echo "timer should expire at ". microtime(true) . " but expired at " . $v . PHP_EOL;
return;
}
if( microtime(true) - $mtime0 > 1.05 ){
echo "timer should expire at ". $mtime0 . "+1 second but expired at "
. microtime(true) . PHP_EOL;
return;
}
echo "verify Time::After : 1 second : pass\n";
});
});
Scheduler::join();
subtc(4);
go(function (){ //go 1
$c = 2000;
for($i=0;$i<$c; $i++){
$ch[$i] = Time::After($i*Time::MILLISECOND);
}
for($i=0;$i<$c; $i++){
if( empty($ch[$i]) ){
echo "some timers were failed to start\n";
return;
}
}
go( function() use($ch, $c){
$count = 0;
$time0 = time();
while($count<$c){
for($i=0; $i<$c; $i++){
$p = $ch[$i]->TryPop();
if(!empty($p)){
$count ++;
//echo "ch $i returns $p\n";
}
}
Time::sleep(10 * Time::MILLISECOND); // = usleep(10*1000);
if( time()-$time0 > ($c+1000) / 1000 ) {
echo "timer should expire at ". ($c/1000) .
" seconds but expired at " . (time()-$time0) .
" seconds\n";
return;
}
}
echo "verify start of 2000 timers : pass\n";
});
});
Scheduler::join();
subtc(5);
go(function (){ //go 1
$ch = Time::tick(100*Time::MILLISECOND);
$done = new Chan(1);
go( function() use($ch, $done){
select(
['case', $ch, function($v) use($done){
static $t0 = 0;
$t1 = microtime(true);
if($t0){
if( $t1-$t0 > 0.12 || $t1 - $t0 < 0.08 ){
echo "timer interval expected to be 0.1 seconds, ". ($t1-$t0) ." in actual\n";
}
}
$t0 = $t1;
//echo microtime(true) . ":". $v . PHP_EOL;
static $i = 0;
if($i++ >= 10) {
$done->close();
}
}]
)->loop($done);
echo "verify Timer::tick(): pass\n";
});
});
Scheduler::join();
?>
--EXPECT--
SUB-TC: #1
1
1000
1000000
1000000000
60000000000
3600000000000
SUB-TC: #2
verify sleep of 1 second : outside of go routine: pass
SUB-TC: #3
verify Time::After : 1 second : pass
SUB-TC: #4
verify start of 2000 timers : pass
SUB-TC: #5
verify Timer::tick(): pass