Пример #1
0
 function testAbstractWatcher()
 {
     $logger = $this->getMock('Psr\\Log\\LoggerInterface');
     $stub = $this->getMockForAbstractClass(AbstractWatcher::class, [function () {
     }]);
     $this->assertFalse($stub->isFinished());
     $this->assertTrue($stub->isActive());
     $stub->stop();
     $this->assertFalse($stub->isFinished());
     $this->assertFalse($stub->isActive());
     $stub->start();
     $this->assertFalse($stub->isFinished());
     $this->assertTrue($stub->isActive());
     $stub->finished();
     $this->assertTrue($stub->isFinished());
     $this->assertFalse($stub->isActive());
     $this->assertSame(spl_object_hash($stub), $stub->getHash());
     $this->assertNull($stub->getLogger());
     $loop = new Loop();
     $loop->setLogger($logger);
     $stub->setLoop($loop);
     $this->assertSame($loop, $stub->getLoop());
     $this->assertSame($logger, $stub->getLogger());
 }
Пример #2
0
 public function testStoppedWatcher()
 {
     $bool = false;
     $called = 0;
     $loop = new Loop();
     $loop->setMinDuration(100000)->add((new CallbackWatcher(function () {
     }))->finished())->add((new CallbackWatcher(function (CallbackWatcher $w) use(&$bool, &$called) {
         if ($called === 2) {
             $bool = true;
             $w->stop();
         } else {
             $called++;
         }
     }))->stop())->addInterval(0.2, function (IntervalWatcher $w) {
         if ($w->getLoop()->getTicks() % 2 === 0) {
             $stopped = $w->getLoop()->grep(function (WatcherInterface $w) {
                 return $w->isStopped();
             });
             $stopped[0]->start();
             $w->stop();
         }
     });
     $loop->run(100);
     $this->assertTrue($bool);
     $this->assertSame(2, $called);
     $called = 0;
     $bool = false;
     foreach ($loop->getWatcher() as $watcher) {
         if ($watcher instanceof CallbackWatcher) {
             $watcher->start();
         }
     }
     $loop->run(100);
     $this->assertTrue($bool);
     $this->assertSame(2, $called);
 }
Пример #3
0
 function testLoopRun()
 {
     $loop = new Loop();
     $loop->add(new CallbackWatcher(function () {
     }));
     $loop->run(2);
     $this->assertSame($loop->getTicks(), 2);
     $c = 0;
     $loop->filter(function () {
         return false;
     });
     $loop->setMinDuration(100);
     $loop->add(new CallbackWatcher(function ($w) use(&$c) {
         usleep(101);
         $w->getLoop()->stop();
         $c++;
     }));
     $this->assertSame(0, $c);
     $loop->run();
     $this->assertSame(1, $c);
     $a = 0;
     $b = 0;
     $loop->reset();
     $loop->setMinDuration(100);
     $loop->add(new CallbackWatcher(function ($w) use(&$a) {
         if ($w->getLoop()->getTicks() === 3) {
             $w->stop();
         } else {
             $a++;
         }
     }));
     $loop->add(new CallbackWatcher(function ($w) use(&$b) {
         if ($w->getLoop()->getTicks() === 5) {
             $w->finished();
         } else {
             $b++;
         }
     }));
     $loop->run();
     $this->assertSame(3, $a);
     $this->assertSame(5, $b);
 }