20
20
*/
21
21
class StopwatchEventTest extends \PHPUnit_Framework_TestCase
22
22
{
23
+ const DELTA = 20 ;
24
+
23
25
public function testGetOrigin ()
24
26
{
25
27
$ event = new StopwatchEvent (12 );
@@ -66,20 +68,18 @@ public function testDuration()
66
68
{
67
69
$ event = new StopwatchEvent (microtime (true ) * 1000 );
68
70
$ event ->start ();
69
- usleep (20000 );
71
+ usleep (200000 );
70
72
$ event ->stop ();
71
- $ total = $ event ->getDuration ();
72
- $ this ->assertTrue ($ total >= 11 && $ total <= 29 , $ total .' should be 20 (between 11 and 29) ' );
73
+ $ this ->assertEquals (200 , $ event ->getDuration (), null , self ::DELTA );
73
74
74
75
$ event = new StopwatchEvent (microtime (true ) * 1000 );
75
76
$ event ->start ();
76
- usleep (10000 );
77
+ usleep (100000 );
77
78
$ event ->stop ();
78
79
$ event ->start ();
79
- usleep (10000 );
80
+ usleep (100000 );
80
81
$ event ->stop ();
81
- $ total = $ event ->getDuration ();
82
- $ this ->assertTrue ($ total >= 11 && $ total <= 29 , $ total .' should be 20 (between 11 and 29) ' );
82
+ $ this ->assertEquals (200 , $ event ->getDuration (), null , self ::DELTA );
83
83
}
84
84
85
85
/**
@@ -96,12 +96,11 @@ public function testEnsureStopped()
96
96
// this also test overlap between two periods
97
97
$ event = new StopwatchEvent (microtime (true ) * 1000 );
98
98
$ event ->start ();
99
- usleep (10000 );
99
+ usleep (100000 );
100
100
$ event ->start ();
101
- usleep (10000 );
101
+ usleep (100000 );
102
102
$ event ->ensureStopped ();
103
- $ total = $ event ->getDuration ();
104
- $ this ->assertTrue ($ total >= 21 && $ total <= 39 , $ total .' should be 30 (between 21 and 39) ' );
103
+ $ this ->assertEquals (300 , $ event ->getDuration (), null , self ::DELTA );
105
104
}
106
105
107
106
public function testStartTime ()
@@ -116,10 +115,9 @@ public function testStartTime()
116
115
117
116
$ event = new StopwatchEvent (microtime (true ) * 1000 );
118
117
$ event ->start ();
119
- usleep (10000 );
118
+ usleep (100000 );
120
119
$ event ->stop ();
121
- $ start = $ event ->getStartTime ();
122
- $ this ->assertTrue ($ start >= 0 && $ start <= 20 );
120
+ $ this ->assertEquals (0 , $ event ->getStartTime (), null , self ::DELTA );
123
121
}
124
122
125
123
public function testEndTime ()
@@ -133,13 +131,12 @@ public function testEndTime()
133
131
134
132
$ event = new StopwatchEvent (microtime (true ) * 1000 );
135
133
$ event ->start ();
136
- usleep (10000 );
134
+ usleep (100000 );
137
135
$ event ->stop ();
138
136
$ event ->start ();
139
- usleep (10000 );
137
+ usleep (100000 );
140
138
$ event ->stop ();
141
- $ end = $ event ->getEndTime ();
142
- $ this ->assertTrue ($ end >= 11 && $ end <= 29 , $ end .' should be 20 (between 11 and 29) ' );
139
+ $ this ->assertEquals (200 , $ event ->getEndTime (), null , self ::DELTA );
143
140
}
144
141
145
142
/**
0 commit comments