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(): void 22 { 23 } 24 25 public function tearDown(): void 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 $timeFromInt = new Grpc\Timeval(123); 71 $this->assertSame(0, Grpc\Timeval::compare($this->time, $timeFromInt)); 72 } 73 74 public function testConstructorWithBigInt() 75 { 76 $this->time = new Grpc\Timeval(7200000000); // > 2^32 77 $this->assertNotNull($this->time); 78 $this->assertSame('Grpc\Timeval', get_class($this->time)); 79 $halfHour = new Grpc\Timeval(1800000000); // < 2^31 80 $hour = $halfHour->add($halfHour); 81 $twoHour = $hour->add($hour); 82 $this->assertSame(0, Grpc\Timeval::compare($this->time, $twoHour)); 83 } 84 85 public function testAddAndSubtractWithBigInt() 86 { 87 $time = new Grpc\Timeval(7200000000); 88 $delta = new Grpc\Timeval(7200000000); 89 $delta2 = new Grpc\Timeval(7200000000*2); 90 $time2 = $time->add($delta2); 91 $time2 = $time2->subtract($delta); 92 $time2 = $time2->subtract($delta); 93 $this->assertSame(0, Grpc\Timeval::compare($time, $time2)); 94 } 95 96 public function testCompareSame() 97 { 98 $zero = Grpc\Timeval::zero(); 99 $this->assertSame(0, Grpc\Timeval::compare($zero, $zero)); 100 } 101 102 public function testPastIsLessThanZero() 103 { 104 $zero = Grpc\Timeval::zero(); 105 $past = Grpc\Timeval::infPast(); 106 $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero)); 107 $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past)); 108 } 109 110 public function testFutureIsGreaterThanZero() 111 { 112 $zero = Grpc\Timeval::zero(); 113 $future = Grpc\Timeval::infFuture(); 114 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future)); 115 $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero)); 116 } 117 118 /** 119 * @depends testFutureIsGreaterThanZero 120 */ 121 public function testNowIsBetweenZeroAndFuture() 122 { 123 $zero = Grpc\Timeval::zero(); 124 $future = Grpc\Timeval::infFuture(); 125 $now = Grpc\Timeval::now(); 126 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); 127 $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); 128 } 129 130 public function testNowAndAdd() 131 { 132 $now = Grpc\Timeval::now(); 133 $this->assertNotNull($now); 134 $delta = new Grpc\Timeval(1000); 135 $deadline = $now->add($delta); 136 $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now)); 137 } 138 139 public function testNowAndSubtract() 140 { 141 $now = Grpc\Timeval::now(); 142 $delta = new Grpc\Timeval(1000); 143 $deadline = $now->subtract($delta); 144 $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now)); 145 } 146 147 public function testAddAndSubtract() 148 { 149 $now = Grpc\Timeval::now(); 150 $delta = new Grpc\Timeval(1000); 151 $deadline = $now->add($delta); 152 $back_to_now = $deadline->subtract($delta); 153 $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); 154 } 155 156 public function testAddAndSubtractBigInt() 157 { 158 $now = Grpc\Timeval::now(); 159 $delta = new Grpc\Timeval(7200000000); 160 $deadline = $now->add($delta); 161 $back_to_now = $deadline->subtract($delta); 162 $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); 163 } 164 165 166 public function testSimilar() 167 { 168 $a = Grpc\Timeval::now(); 169 $delta = new Grpc\Timeval(1000); 170 $b = $a->add($delta); 171 $thresh = new Grpc\Timeval(1100); 172 $this->assertTrue(Grpc\Timeval::similar($a, $b, $thresh)); 173 $thresh = new Grpc\Timeval(900); 174 $this->assertFalse(Grpc\Timeval::similar($a, $b, $thresh)); 175 } 176 177 public function testSleepUntil() 178 { 179 $curr_microtime = microtime(true); 180 $now = Grpc\Timeval::now(); 181 $delta = new Grpc\Timeval(1000); 182 $deadline = $now->add($delta); 183 $deadline->sleepUntil(); 184 $done_microtime = microtime(true); 185 $this->assertTrue(($done_microtime - $curr_microtime) > 0.0009); 186 } 187 188 public function testConstructorInvalidParam() 189 { 190 $this->expectException(\InvalidArgumentException::class); 191 $delta = new Grpc\Timeval('abc'); 192 } 193 194 public function testAddInvalidParam() 195 { 196 $this->expectException(\InvalidArgumentException::class); 197 $a = Grpc\Timeval::now(); 198 $a->add(1000); 199 } 200 201 public function testSubtractInvalidParam() 202 { 203 $this->expectException(\InvalidArgumentException::class); 204 $a = Grpc\Timeval::now(); 205 $a->subtract(1000); 206 } 207 208 public function testCompareInvalidParam() 209 { 210 $this->expectException(\InvalidArgumentException::class); 211 $a = Grpc\Timeval::compare(1000, 1100); 212 } 213 214 public function testSimilarInvalidParam() 215 { 216 $this->expectException(\InvalidArgumentException::class); 217 $a = Grpc\Timeval::similar(1000, 1100, 1200); 218 $this->assertNull($delta); 219 } 220} 221