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 private $time; 22 23 public function setUp(): void 24 { 25 } 26 27 public function tearDown(): void 28 { 29 unset($this->time); 30 } 31 32 public function testConstructorWithInt() 33 { 34 $this->time = new Grpc\Timeval(1234); 35 $this->assertNotNull($this->time); 36 $this->assertSame('Grpc\Timeval', get_class($this->time)); 37 } 38 39 public function testConstructorWithNegative() 40 { 41 $this->time = new Grpc\Timeval(-123); 42 $this->assertNotNull($this->time); 43 $this->assertSame('Grpc\Timeval', get_class($this->time)); 44 } 45 46 public function testConstructorWithZero() 47 { 48 $this->time = new Grpc\Timeval(0); 49 $this->assertNotNull($this->time); 50 $this->assertSame('Grpc\Timeval', get_class($this->time)); 51 } 52 53 public function testConstructorWithOct() 54 { 55 $this->time = new Grpc\Timeval(0123); 56 $this->assertNotNull($this->time); 57 $this->assertSame('Grpc\Timeval', get_class($this->time)); 58 } 59 60 public function testConstructorWithHex() 61 { 62 $this->time = new Grpc\Timeval(0x1A); 63 $this->assertNotNull($this->time); 64 $this->assertSame('Grpc\Timeval', get_class($this->time)); 65 } 66 67 public function testConstructorWithBigInt() 68 { 69 $this->time = new Grpc\Timeval(7200000000); // > 2^32 70 $this->assertNotNull($this->time); 71 $this->assertSame('Grpc\Timeval', get_class($this->time)); 72 $halfHour = new Grpc\Timeval(1800000000); // < 2^31 73 $hour = $halfHour->add($halfHour); 74 $twoHour = $hour->add($hour); 75 $this->assertSame(0, Grpc\Timeval::compare($this->time, $twoHour)); 76 } 77 78 public function testAddAndSubtractWithBigInt() 79 { 80 $time = new Grpc\Timeval(7200000000); 81 $delta = new Grpc\Timeval(7200000000); 82 $delta2 = new Grpc\Timeval(7200000000*2); 83 $time2 = $time->add($delta2); 84 $time2 = $time2->subtract($delta); 85 $time2 = $time2->subtract($delta); 86 $this->assertSame(0, Grpc\Timeval::compare($time, $time2)); 87 } 88 89 public function testCompareSame() 90 { 91 $zero = Grpc\Timeval::zero(); 92 $this->assertSame(0, Grpc\Timeval::compare($zero, $zero)); 93 } 94 95 public function testPastIsLessThanZero() 96 { 97 $zero = Grpc\Timeval::zero(); 98 $past = Grpc\Timeval::infPast(); 99 $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero)); 100 $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past)); 101 } 102 103 public function testFutureIsGreaterThanZero() 104 { 105 $zero = Grpc\Timeval::zero(); 106 $future = Grpc\Timeval::infFuture(); 107 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future)); 108 $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero)); 109 } 110 111 /** 112 * @depends testFutureIsGreaterThanZero 113 */ 114 public function testNowIsBetweenZeroAndFuture() 115 { 116 $zero = Grpc\Timeval::zero(); 117 $future = Grpc\Timeval::infFuture(); 118 $now = Grpc\Timeval::now(); 119 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); 120 $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); 121 } 122 123 public function testNowAndAdd() 124 { 125 $now = Grpc\Timeval::now(); 126 $this->assertNotNull($now); 127 $delta = new Grpc\Timeval(1000); 128 $deadline = $now->add($delta); 129 $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now)); 130 } 131 132 public function testNowAndSubtract() 133 { 134 $now = Grpc\Timeval::now(); 135 $delta = new Grpc\Timeval(1000); 136 $deadline = $now->subtract($delta); 137 $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now)); 138 } 139 140 public function testAddAndSubtract() 141 { 142 $now = Grpc\Timeval::now(); 143 $delta = new Grpc\Timeval(1000); 144 $deadline = $now->add($delta); 145 $back_to_now = $deadline->subtract($delta); 146 $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); 147 } 148 149 public function testAddAndSubtractBigInt() 150 { 151 $now = Grpc\Timeval::now(); 152 $delta = new Grpc\Timeval(7200000000); 153 $deadline = $now->add($delta); 154 $back_to_now = $deadline->subtract($delta); 155 $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); 156 } 157 158 159 public function testSimilar() 160 { 161 $a = Grpc\Timeval::now(); 162 $delta = new Grpc\Timeval(1000); 163 $b = $a->add($delta); 164 $thresh = new Grpc\Timeval(1100); 165 $this->assertTrue(Grpc\Timeval::similar($a, $b, $thresh)); 166 $thresh = new Grpc\Timeval(900); 167 $this->assertFalse(Grpc\Timeval::similar($a, $b, $thresh)); 168 } 169 170 public function testSleepUntil() 171 { 172 $curr_microtime = microtime(true); 173 $now = Grpc\Timeval::now(); 174 $delta = new Grpc\Timeval(1000); 175 $deadline = $now->add($delta); 176 $deadline->sleepUntil(); 177 $done_microtime = microtime(true); 178 $this->assertTrue(($done_microtime - $curr_microtime) > 0.0009); 179 } 180 181 public function testConstructorInvalidParam() 182 { 183 $this->expectException(\InvalidArgumentException::class); 184 $delta = new Grpc\Timeval('abc'); 185 } 186 187 public function testAddInvalidParam() 188 { 189 $this->expectException(\InvalidArgumentException::class); 190 $a = Grpc\Timeval::now(); 191 $a->add(1000); 192 } 193 194 public function testSubtractInvalidParam() 195 { 196 $this->expectException(\InvalidArgumentException::class); 197 $a = Grpc\Timeval::now(); 198 $a->subtract(1000); 199 } 200 201 public function testCompareInvalidParam() 202 { 203 $this->expectException(\InvalidArgumentException::class); 204 $a = Grpc\Timeval::compare(1000, 1100); 205 } 206 207 public function testSimilarInvalidParam() 208 { 209 $this->expectException(\InvalidArgumentException::class); 210 $a = Grpc\Timeval::similar(1000, 1100, 1200); 211 $this->assertNull($delta); 212 } 213} 214