ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
LoopTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\Event\Loop;
4 
6 
7  function testNextTick() {
8 
9  $loop = new Loop();
10  $check = 0;
11  $loop->nextTick(function() use (&$check) {
12 
13  $check++;
14 
15  });
16 
17  $loop->run();
18 
19  $this->assertEquals(1, $check);
20 
21  }
22 
23  function testTimeout() {
24 
25  $loop = new Loop();
26  $check = 0;
27  $loop->setTimeout(function() use (&$check) {
28 
29  $check++;
30 
31  }, 0.02);
32 
33  $loop->run();
34 
35  $this->assertEquals(1, $check);
36 
37  }
38 
39  function testTimeoutOrder() {
40 
41  $loop = new Loop();
42  $check = [];
43  $loop->setTimeout(function() use (&$check) {
44 
45  $check[] = 'a';
46 
47  }, 0.2);
48  $loop->setTimeout(function() use (&$check) {
49 
50  $check[] = 'b';
51 
52  }, 0.1);
53  $loop->setTimeout(function() use (&$check) {
54 
55  $check[] = 'c';
56 
57  }, 0.3);
58 
59  $loop->run();
60 
61  $this->assertEquals(['b', 'a', 'c'], $check);
62 
63  }
64 
65  function testSetInterval() {
66 
67  $loop = new Loop();
68  $check = 0;
69  $intervalId = null;
70  $intervalId = $loop->setInterval(function() use (&$check, &$intervalId, $loop) {
71 
72  $check++;
73  if ($check > 5) {
74  $loop->clearInterval($intervalId);
75  }
76 
77  }, 0.02);
78 
79  $loop->run();
80  $this->assertEquals(6, $check);
81 
82  }
83 
84  function testAddWriteStream() {
85 
86  $h = fopen('php://temp', 'r+');
87  $loop = new Loop();
88  $loop->addWriteStream($h, function() use ($h, $loop) {
89 
90  fwrite($h, 'hello world');
91  $loop->removeWriteStream($h);
92 
93  });
94  $loop->run();
95  rewind($h);
96  $this->assertEquals('hello world', stream_get_contents($h));
97 
98  }
99 
100  function testAddReadStream() {
101 
102  $h = fopen('php://temp', 'r+');
103  fwrite($h, 'hello world');
104  rewind($h);
105 
106  $loop = new Loop();
107 
108  $result = null;
109 
110  $loop->addReadStream($h, function() use ($h, $loop, &$result) {
111 
112  $result = fgets($h);
113  $loop->removeReadStream($h);
114 
115  });
116  $loop->run();
117  $this->assertEquals('hello world', $result);
118 
119  }
120 
121  function testStop() {
122 
123  $check = 0;
124  $loop = new Loop();
125  $loop->setTimeout(function() use (&$check) {
126  $check++;
127  }, 200);
128 
129  $loop->nextTick(function() use ($loop) {
130  $loop->stop();
131  });
132  $loop->run();
133 
134  $this->assertEquals(0, $check);
135 
136  }
137 
138  function testTick() {
139 
140  $check = 0;
141  $loop = new Loop();
142  $loop->setTimeout(function() use (&$check) {
143  $check++;
144  }, 1);
145 
146  $loop->nextTick(function() use ($loop, &$check) {
147  $check++;
148  });
149  $loop->tick();
150 
151  $this->assertEquals(1, $check);
152 
153  }
154 
159  function testNextTickStacking() {
160 
161  $loop = new Loop();
162  $check = 0;
163  $loop->nextTick(function() use (&$check, $loop) {
164 
165  $loop->nextTick(function() use (&$check) {
166 
167  $check++;
168 
169  });
170  $check++;
171 
172  });
173 
174  $loop->run();
175 
176  $this->assertEquals(2, $check);
177 
178  }
179 
180 }
$result
$h
testNextTickStacking()
Here we add a new nextTick function as we&#39;re in the middle of a current nextTick. ...
Definition: LoopTest.php:159
A simple eventloop implementation.
Definition: Loop.php:18