OLD | NEW |
(Empty) | |
| 1 <?php |
| 2 /* |
| 3 * |
| 4 * Copyright 2015-2016, Google Inc. |
| 5 * All rights reserved. |
| 6 * |
| 7 * Redistribution and use in source and binary forms, with or without |
| 8 * modification, are permitted provided that the following conditions are |
| 9 * met: |
| 10 * |
| 11 * * Redistributions of source code must retain the above copyright |
| 12 * notice, this list of conditions and the following disclaimer. |
| 13 * * Redistributions in binary form must reproduce the above |
| 14 * copyright notice, this list of conditions and the following disclaimer |
| 15 * in the documentation and/or other materials provided with the |
| 16 * distribution. |
| 17 * * Neither the name of Google Inc. nor the names of its |
| 18 * contributors may be used to endorse or promote products derived from |
| 19 * this software without specific prior written permission. |
| 20 * |
| 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 * |
| 33 */ |
| 34 class TimevalTest extends PHPUnit_Framework_TestCase |
| 35 { |
| 36 public function testCompareSame() |
| 37 { |
| 38 $zero = Grpc\Timeval::zero(); |
| 39 $this->assertSame(0, Grpc\Timeval::compare($zero, $zero)); |
| 40 } |
| 41 |
| 42 public function testPastIsLessThanZero() |
| 43 { |
| 44 $zero = Grpc\Timeval::zero(); |
| 45 $past = Grpc\Timeval::infPast(); |
| 46 $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero)); |
| 47 $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past)); |
| 48 } |
| 49 |
| 50 public function testFutureIsGreaterThanZero() |
| 51 { |
| 52 $zero = Grpc\Timeval::zero(); |
| 53 $future = Grpc\Timeval::infFuture(); |
| 54 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future)); |
| 55 $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero)); |
| 56 } |
| 57 |
| 58 /** |
| 59 * @depends testFutureIsGreaterThanZero |
| 60 */ |
| 61 public function testNowIsBetweenZeroAndFuture() |
| 62 { |
| 63 $zero = Grpc\Timeval::zero(); |
| 64 $future = Grpc\Timeval::infFuture(); |
| 65 $now = Grpc\Timeval::now(); |
| 66 $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); |
| 67 $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); |
| 68 } |
| 69 |
| 70 public function testNowAndAdd() |
| 71 { |
| 72 $now = Grpc\Timeval::now(); |
| 73 $delta = new Grpc\Timeval(1000); |
| 74 $deadline = $now->add($delta); |
| 75 $this->assertGreaterThan(0, Grpc\Timeval::compare($deadline, $now)); |
| 76 } |
| 77 |
| 78 public function testNowAndSubtract() |
| 79 { |
| 80 $now = Grpc\Timeval::now(); |
| 81 $delta = new Grpc\Timeval(1000); |
| 82 $deadline = $now->subtract($delta); |
| 83 $this->assertLessThan(0, Grpc\Timeval::compare($deadline, $now)); |
| 84 } |
| 85 |
| 86 public function testAddAndSubtract() |
| 87 { |
| 88 $now = Grpc\Timeval::now(); |
| 89 $delta = new Grpc\Timeval(1000); |
| 90 $deadline = $now->add($delta); |
| 91 $back_to_now = $deadline->subtract($delta); |
| 92 $this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now)); |
| 93 } |
| 94 |
| 95 public function testSimilar() |
| 96 { |
| 97 $a = Grpc\Timeval::now(); |
| 98 $delta = new Grpc\Timeval(1000); |
| 99 $b = $a->add($delta); |
| 100 $thresh = new Grpc\Timeval(1100); |
| 101 $this->assertTrue(Grpc\Timeval::similar($a, $b, $thresh)); |
| 102 $thresh = new Grpc\Timeval(900); |
| 103 $this->assertFalse(Grpc\Timeval::similar($a, $b, $thresh)); |
| 104 } |
| 105 |
| 106 public function testSleepUntil() |
| 107 { |
| 108 $curr_microtime = microtime(true); |
| 109 $now = Grpc\Timeval::now(); |
| 110 $delta = new Grpc\Timeval(1000); |
| 111 $deadline = $now->add($delta); |
| 112 $deadline->sleepUntil(); |
| 113 $done_microtime = microtime(true); |
| 114 $this->assertTrue(($done_microtime - $curr_microtime) > 0.0009); |
| 115 } |
| 116 |
| 117 /** |
| 118 * @expectedException InvalidArgumentException |
| 119 */ |
| 120 public function testConstructorInvalidParam() |
| 121 { |
| 122 $delta = new Grpc\Timeval('abc'); |
| 123 } |
| 124 |
| 125 /** |
| 126 * @expectedException InvalidArgumentException |
| 127 */ |
| 128 public function testAddInvalidParam() |
| 129 { |
| 130 $a = Grpc\Timeval::now(); |
| 131 $a->add(1000); |
| 132 } |
| 133 |
| 134 /** |
| 135 * @expectedException InvalidArgumentException |
| 136 */ |
| 137 public function testSubtractInvalidParam() |
| 138 { |
| 139 $a = Grpc\Timeval::now(); |
| 140 $a->subtract(1000); |
| 141 } |
| 142 |
| 143 /** |
| 144 * @expectedException InvalidArgumentException |
| 145 */ |
| 146 public function testCompareInvalidParam() |
| 147 { |
| 148 $a = Grpc\Timeval::compare(1000, 1100); |
| 149 } |
| 150 |
| 151 /** |
| 152 * @expectedException InvalidArgumentException |
| 153 */ |
| 154 public function testSimilarInvalidParam() |
| 155 { |
| 156 $a = Grpc\Timeval::similar(1000, 1100, 1200); |
| 157 } |
| 158 |
| 159 } |
OLD | NEW |