5d079857dee3b436918a79625f498c9a832d6894
[m6w6/atick] / tests / lib / atick / TickerTest.php
1 <?php
2
3 namespace atick;
4
5 include __DIR__."/../../setup.inc";
6
7 class TickerTest extends \PHPUnit_Framework_TestCase {
8
9 /**
10 * @var Ticker
11 */
12 protected $ticker;
13
14 protected function setUp() {
15 $this->ticker = new Ticker;
16 }
17
18 public function testRegister() {
19 $this->ticker->register();
20 $this->ticker->unregister();
21 $this->ticker->register();
22 $this->ticker->unregister();
23 }
24
25 public function testTicks() {
26 $file = fopen(__FILE__, "r");
27 stream_set_blocking($file, false);
28 $read = 0;
29
30 declare(ticks=1);
31 $this->ticker->register();
32 $this->ticker->read($file, function ($file) use (&$read) {
33 do {
34 $data = fread($file, 4096);
35 $read += strlen($data);
36 } while (strlen($data));
37 return feof($file);
38 });
39
40 $dummy = "This test is do tiny, ";
41 $dummy.= "we don't have to do much.";
42
43 $this->assertEquals(filesize(__FILE__), $read);
44 }
45
46 public function testBasic() {
47 $r = $w = false;
48 $this->assertCount(0, $this->ticker);
49
50 $file = fopen(__FILE__, "r");
51 stream_set_blocking($file, false);
52
53 $this->ticker->read($file, function ($file) use (&$r) {
54 return $r;
55 });
56 $this->assertCount(1, $this->ticker);
57 $this->ticker->write($file, function ($file) use (&$w) {
58 return $w;
59 });
60 $this->assertCount(2, $this->ticker);
61
62 $this->assertSame(2, $this->ticker->wait());
63 $r = true;
64 $this->assertSame(1, $this->ticker->wait());
65 $w = true;
66 $this->assertSame(0, $this->ticker->wait());
67 }
68
69 }