1<?php 2/* 3 * 4 * Copyright 2015 gRPC authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 */ 19class TimevalTest extends PHPUnit_Framework_TestCase 20{ 21 public function setUp() 22 { 23 } 24 25 public function tearDown() 26 { 27 unset($this->time); 28 } 29 30 public function testConstructorWithInt() 31 { 32 $this->time = new Grpc\Timeval(1234); 33 $this->assertNotNull($this->time); 34 $this->assertSame('Grpc\Timeval', get_class($this->time)); 35 } 36 37 public function testConstructorWithNegative() 38 { 39 $this->time = new Grpc\Timeval(-123); 40 $this->assertNotNull($this->time); 41 $this->assertSame('Grpc\Timeval', get_class($this->time)); 42 } 43 44 public function testConstructorWithZero() 45 { 46 $this->time = new Grpc\Timeval(0); 47 $this->assertNotNull($this->time); 48 $this->assertSame('Grpc\Timeval', get_class($this->time)); 49 } 50 51 public function testConstructorWithOct() 52 { 53 $this->time = new Grpc\Timeval(0123); 54 $this->assertNotNull($this->time); 55 $this->assertSame('Grpc\Timeval', get_class($this->time)); 56 } 57 58 public function testConstructorWithHex() 59 { 60 $this->time = new Grpc\Timeval(0x1A); 61 $this->assertNotNull($this->time); 62 $this->assertSame('Grpc\Timeval', get_class($this->time)); 63 } 64 65 public function testConstructorWithFloat() 66 { 67 $this->time = new Grpc\Timeval(123.456); 68 $this->assertNotNull($this->time); 69 $this->assertSame('Grpc\Timeval', get_class($this->time)); 70 } 71 72 public function testCompareSame() 73 { 74 $zero = Grpc\Timeval::zero(); 75 $this->assertSame(0, Grpc\Timeval::compare($zero, $zero)); 76 } 77 78 public function testPastIsLessThanZero() 79 { 80 $zero = Grpc\Timeval::zero(); 81 $past = Grpc\Timeval::infPast(); 82 $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero)); 83 $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past)); 84 } 85 86 public function testFutureIsGreaterThanZero() 87 { 88 $zero = Grpc\Timeval::zero(); 89 $future = Grpc\Timeval::infFuture(); 90 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future)); 91 $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero)); 92 } 93 94 /** 95 * @depends testFutureIsGreaterThanZero 96 */ 97 public function testNowIsBetweenZeroAndFuture() 98 { 99 $zero = Grpc\Timeval::zero(); 100 $future = Grpc\Timeval::infFuture(); 101 $now = Grpc\Timeval::now(); 102 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); 103 $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); 104 } 105 106 public function testNowAndAdd() 107 { 108 $now = Grpc\Timeval::now(); 109 $this->assertNotNull($now); 110 $delta = new Grpc\Timeval(1000); 111 $deadline = $now->add($delta); 112 $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now)); 113 } 114 115 public function testNowAndSubtract() 116 { 117 $now = Grpc\Timeval::now(); 118 $delta = new Grpc\Timeval(1000); 119 $deadline = $now->subtract($delta); 120 $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now)); 121 } 122 123 public function testAddAndSubtract() 124 { 125 $now = Grpc\Timeval::now(); 126 $delta = new Grpc\Timeval(1000); 127 $deadline = $now->add($delta); 128 $back_to_now = $deadline->subtract($delta); 129 $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); 130 } 131 132 public function testSimilar() 133 { 134 $a = Grpc\Timeval::now(); 135 $delta = new Grpc\Timeval(1000); 136 $b = $a->add($delta); 137 $thresh = new Grpc\Timeval(1100); 138 $this->assertTrue(Grpc\Timeval::similar($a, $b, $thresh)); 139 $thresh = new Grpc\Timeval(900); 140 $this->assertFalse(Grpc\Timeval::similar($a, $b, $thresh)); 141 } 142 143 public function testSleepUntil() 144 { 145 $curr_microtime = microtime(true); 146 $now = Grpc\Timeval::now(); 147 $delta = new Grpc\Timeval(1000); 148 $deadline = $now->add($delta); 149 $deadline->sleepUntil(); 150 $done_microtime = microtime(true); 151 $this->assertTrue(($done_microtime - $curr_microtime) > 0.0009); 152 } 153 154 /** 155 * @expectedException InvalidArgumentException 156 */ 157 public function testConstructorInvalidParam() 158 { 159 $delta = new Grpc\Timeval('abc'); 160 } 161 162 /** 163 * @expectedException InvalidArgumentException 164 */ 165 public function testAddInvalidParam() 166 { 167 $a = Grpc\Timeval::now(); 168 $a->add(1000); 169 } 170 171 /** 172 * @expectedException InvalidArgumentException 173 */ 174 public function testSubtractInvalidParam() 175 { 176 $a = Grpc\Timeval::now(); 177 $a->subtract(1000); 178 } 179 180 /** 181 * @expectedException InvalidArgumentException 182 */ 183 public function testCompareInvalidParam() 184 { 185 $a = Grpc\Timeval::compare(1000, 1100); 186 } 187 188 /** 189 * @expectedException InvalidArgumentException 190 */ 191 public function testSimilarInvalidParam() 192 { 193 $a = Grpc\Timeval::similar(1000, 1100, 1200); 194 $this->assertNull($delta); 195 } 196} 197