Commits

Ruslan Osmanov  committed 1d04e11

Fix: segfault in EvPeriodic::set() when reschedule callback is not provided
Add: tests for EvTimer and EvWatcher::keepalive()

  • Participants
  • Parent commits e6a79aa

Comments (0)

Files changed (3)

 	
 	/* Free fci and fcc within periodic_ptr, since they will be overwritten anyways */
 
-	if (ZEND_FCI_INITIALIZED(*periodic_ptr->fci)) {
+	if (periodic_ptr->fci && ZEND_FCI_INITIALIZED(*periodic_ptr->fci)) {
 		PHP_EV_FREE_FCALL_INFO(periodic_ptr->fci, periodic_ptr->fcc);
 	}
 

File tests/05_timer.phpt

+--TEST--
+Check for EvTimer functionality
+--FILE--
+<?php 
+error_reporting(0);
+
+$fudge = 0.02;
+$id = 1;
+
+$loop = EvLoop::default_loop();
+$base = $loop->now();
+$prev = $loop->now();
+
+for ($i = 1; $i <= 5; ++$i) {
+	$t = $i * $i * 1.735435336;
+	$t -= (int) $t;
+
+	$timer = new EvTimer($t, 0, $loop, function ($w, $r)
+		use (&$id, &$prev, $base, $loop, $i, $t, $fudge) {
+			$now = $loop->now();
+
+			$loop->verify();
+
+			echo $now + $fudge >= $prev ? "" : "not ", "ok ", ++$id,
+				" # t0 $i $now + $fudge >= $prev\n";
+			echo $now + $fudge >= $base + $t ? "" : "not ", "ok ", ++$id,
+				" # t1 $now + $fudge >= $base + $t\n";
+
+			if (! ($id % 3)) {
+				$t *= 0.0625;
+				$w->set($t, 0);
+				$w->start();
+				$t = $now + $t - $base;
+			}
+
+			$prev = $now;
+		});
+	$timer->start();
+
+	$t = $i * $i * 1.375475771;
+	$t -= (int) $t;
+
+	$periodic = new EvPeriodic($base + $t, 0, NULL, $loop, function ($w, $r)
+		use (&$id, &$prev, $base, $loop, $i, $t) {
+			$now = $loop->now();
+
+			$loop->verify();
+
+			echo $now >= $prev ? "" : "not ", "ok ", ++$id,
+				" # p0 $i $now >= $prev\n";
+			echo $now >= $base + $t ? "" : "not ", "ok ", ++$id,
+				" # p1 $i $now >= $base + $t\n";
+
+			if (! ($id % 3)) {
+   				$t *= 1.0625;
+         		$w->set($base + $t, 0);
+         		$w->start();
+			}
+
+			$prev = $now;
+		});
+	$periodic->start();
+}
+
+echo "ok 1\n";
+$loop->run();
+echo "ok 32\n";
+?>
+--EXPECTF--
+ok 1
+ok 2 %s
+ok 3 %s
+ok 4 %s
+ok 5 %s
+ok 6 %s
+ok 7 %s
+ok 8 %s
+ok 9 %s
+ok 10 %s
+ok 11 %s
+ok 12 %s
+ok 13 %s
+ok 14 %s
+ok 15 %s
+ok 16 %s
+ok 17 %s
+ok 18 %s
+ok 19 %s
+ok 20 %s
+ok 21 %s
+ok 22 %s
+ok 23 %s
+ok 24 %s
+ok 25 %s
+ok 26 %s
+ok 27 %s
+ok 28 %s
+ok 29 %s
+ok 30 %s
+ok 31 %s
+ok 32

File tests/06_keepalive.phpt

+--TEST--
+Check for EvWatcher::keepalive() functionality
+--FILE--
+<?php 
+error_reporting(0);
+
+$loop = EvLoop::default_loop();
+
+$timer = new EvTimer(1, 0.3, $loop, function ($w, $r) {
+	echo "ok 7\n";
+	$w->stop();
+});
+
+$timer->keepalive(1);
+
+echo "ok 1\n";
+$loop->run();
+echo "ok 2\n";
+
+$timer->start();
+
+$timer->keepalive(0);
+
+$timer->again();
+$timer->stop();
+$timer->start();
+
+$timer2 = new EvTimer(-1, 0, $loop, function ($w, $r) {
+	echo "ok 4\n";
+});
+$timer2->keepalive(0);
+
+echo "ok 3\n";
+$loop->run();
+echo "ok 5\n";
+
+$timer->keepalive(1);
+
+echo "ok 6\n";
+$loop->run();
+echo "ok 8\n";
+?>
+--EXPECT--
+ok 1
+ok 2
+ok 3
+ok 5
+ok 6
+ok 7
+ok 8