ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FunctionsTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\Event\Loop;
4 
6 
7  function setUp() {
8 
9  // Always creating a fresh loop object.
10  instance(new Loop());
11 
12  }
13 
14  function tearDown() {
15 
16  // Removing the global loop object.
17  instance(null);
18 
19  }
20 
21  function testNextTick() {
22 
23  $check = 0;
24  nextTick(function() use (&$check) {
25 
26  $check++;
27 
28  });
29 
30  run();
31 
32  $this->assertEquals(1, $check);
33 
34  }
35 
36  function testTimeout() {
37 
38  $check = 0;
39  setTimeout(function() use (&$check) {
40 
41  $check++;
42 
43  }, 0.02);
44 
45  run();
46 
47  $this->assertEquals(1, $check);
48 
49  }
50 
51  function testTimeoutOrder() {
52 
53  $check = [];
54  setTimeout(function() use (&$check) {
55 
56  $check[] = 'a';
57 
58  }, 0.2);
59  setTimeout(function() use (&$check) {
60 
61  $check[] = 'b';
62 
63  }, 0.1);
64  setTimeout(function() use (&$check) {
65 
66  $check[] = 'c';
67 
68  }, 0.3);
69 
70  run();
71 
72  $this->assertEquals(['b', 'a', 'c'], $check);
73 
74  }
75 
76  function testSetInterval() {
77 
78  $check = 0;
79  $intervalId = null;
80  $intervalId = setInterval(function() use (&$check, &$intervalId) {
81 
82  $check++;
83  if ($check > 5) {
84  clearInterval($intervalId);
85  }
86 
87  }, 0.02);
88 
89  run();
90  $this->assertEquals(6, $check);
91 
92  }
93 
94  function testAddWriteStream() {
95 
96  $h = fopen('php://temp', 'r+');
97  addWriteStream($h, function() use ($h) {
98 
99  fwrite($h, 'hello world');
101 
102  });
103  run();
104  rewind($h);
105  $this->assertEquals('hello world', stream_get_contents($h));
106 
107  }
108 
109  function testAddReadStream() {
110 
111  $h = fopen('php://temp', 'r+');
112  fwrite($h, 'hello world');
113  rewind($h);
114 
115  $result = null;
116 
117  addReadStream($h, function() use ($h, &$result) {
118 
119  $result = fgets($h);
121 
122  });
123  run();
124  $this->assertEquals('hello world', $result);
125 
126  }
127 
128  function testStop() {
129 
130  $check = 0;
131  setTimeout(function() use (&$check) {
132  $check++;
133  }, 200);
134 
135  nextTick(function() {
136  stop();
137  });
138  run();
139 
140  $this->assertEquals(0, $check);
141 
142  }
143 
144  function testTick() {
145 
146  $check = 0;
147  setTimeout(function() use (&$check) {
148  $check++;
149  }, 1);
150 
151  nextTick(function() use (&$check) {
152  $check++;
153  });
154  tick();
155 
156  $this->assertEquals(1, $check);
157 
158  }
159 
160 }
addWriteStream($stream, callable $cb)
Adds a write stream.
Definition: functions.php:91
$result
$h
removeWriteStream($stream)
Stop watching a stream for writes.
Definition: functions.php:115
stop()
Stops a running eventloop.
Definition: functions.php:162
setTimeout(callable $cb, $timeout)
Executes a function after x seconds.
Definition: functions.php:12
run()
Runs the loop.
Definition: functions.php:130
clearInterval($intervalId)
Stops a running internval.
Definition: functions.php:40
removeReadStream($stream)
Stop watching a stream for reads.
Definition: functions.php:103
addReadStream($stream, callable $cb)
Adds a read stream.
Definition: functions.php:72
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
Definition: functions.php:52
instance(Loop $newLoop=null)
Retrieves or sets the global Loop object.
Definition: functions.php:173
setInterval(callable $cb, $timeout)
Executes a function every x seconds.
Definition: functions.php:28
tick($block=false)
Executes all pending events.
Definition: functions.php:151
A simple eventloop implementation.
Definition: Loop.php:18