Skip to content

Commit

Permalink
Merge pull request grpc#23994 from HannahShiSFB/32bittimeval
Browse files Browse the repository at this point in the history
PHP: support 32bit timeval value
  • Loading branch information
stanley-cheung authored Sep 11, 2020
2 parents fb2cc65 + 8c6be42 commit 61879df
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/php/bin/build_all_docker_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set -e
cd $(dirname $0)/../../..

ALL_IMAGES=( grpc-ext grpc-src alpine centos7 php5 php-src php-future php-zts
fork-support )
fork-support i386 )

if [[ "$1" == "--cmds" ]]; then
for arg in "${ALL_IMAGES[@]}"
Expand Down
2 changes: 1 addition & 1 deletion src/php/bin/run_all_docker_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set -e
cd $(dirname $0)/../../..

ALL_IMAGES=( grpc-ext grpc-src alpine centos7 php5 php-src php-future php-zts
fork-support )
fork-support i386 )

if [[ "$1" == "--cmds" ]]; then
for arg in "${ALL_IMAGES[@]}"
Expand Down
39 changes: 39 additions & 0 deletions src/php/docker/i386/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2020 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM i386/php:7.2

RUN apt-get -qq update && apt-get -qq install -y \
autoconf automake git libtool pkg-config \
valgrind wget zlib1g-dev

ARG MAKEFLAGS=-j8


WORKDIR /tmp

RUN wget https://phar.phpunit.de/phpunit-5.7.27.phar && \
mv phpunit-5.7.27.phar /usr/local/bin/phpunit && \
chmod +x /usr/local/bin/phpunit


WORKDIR /github/grpc

COPY . .

RUN pear package && \
find . -name grpc-*.tgz | xargs -I{} pecl install {}


CMD ["/github/grpc/src/php/bin/run_tests.sh", "--skip-persistent-channel-tests", "--ignore-valgrind-undef-errors"]
26 changes: 17 additions & 9 deletions src/php/ext/grpc/timeval.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,27 @@ zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC) {

/**
* Constructs a new instance of the Timeval class
* @param long $microseconds The number of microseconds in the interval
* @param number $microseconds The number of microseconds in the interval
*/
PHP_METHOD(Timeval, __construct) {
wrapped_grpc_timeval *timeval =
PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, getThis());
php_grpc_long microseconds;

/* "l" == 1 long */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &microseconds) ==
FAILURE) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Timeval expects a long", 1 TSRMLS_CC);
return;
int64_t microseconds = 0;

/* parse $microseconds as long */
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "l",
&microseconds) == FAILURE) {
double microsecondsDouble = 0.0;
/* parse $microseconds as double */
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "d",
&microsecondsDouble) == FAILURE) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Timeval expects a long or double", 1 TSRMLS_CC);
return;
}
microseconds = (int64_t)microsecondsDouble;
}
gpr_timespec time = gpr_time_from_micros(microseconds, GPR_TIMESPAN);
memcpy(&timeval->wrapped, &time, sizeof(gpr_timespec));
Expand Down
34 changes: 34 additions & 0 deletions src/php/tests/unit_tests/TimevalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ public function testConstructorWithFloat()
$this->time = new Grpc\Timeval(123.456);
$this->assertNotNull($this->time);
$this->assertSame('Grpc\Timeval', get_class($this->time));
$timeFromInt = new Grpc\Timeval(123);
$this->assertSame(0, Grpc\Timeval::compare($this->time, $timeFromInt));
}

public function testConstructorWithBigInt()
{
$this->time = new Grpc\Timeval(7200000000); // > 2^32
$this->assertNotNull($this->time);
$this->assertSame('Grpc\Timeval', get_class($this->time));
$halfHour = new Grpc\Timeval(1800000000); // < 2^31
$hour = $halfHour->add($halfHour);
$twoHour = $hour->add($hour);
$this->assertSame(0, Grpc\Timeval::compare($this->time, $twoHour));
}

public function testAddAndSubtractWithBigInt()
{
$time = new Grpc\Timeval(7200000000);
$delta = new Grpc\Timeval(7200000000);
$delta2 = new Grpc\Timeval(7200000000*2);
$time2 = $time->add($delta2);
$time2 = $time2->subtract($delta);
$time2 = $time2->subtract($delta);
$this->assertSame(0, Grpc\Timeval::compare($time, $time2));
}

public function testCompareSame()
Expand Down Expand Up @@ -129,6 +153,16 @@ public function testAddAndSubtract()
$this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now));
}

public function testAddAndSubtractBigInt()
{
$now = Grpc\Timeval::now();
$delta = new Grpc\Timeval(7200000000);
$deadline = $now->add($delta);
$back_to_now = $deadline->subtract($delta);
$this->assertSame(0, Grpc\Timeval::compare($back_to_now, $now));
}


public function testSimilar()
{
$a = Grpc\Timeval::now();
Expand Down
32 changes: 32 additions & 0 deletions templates/src/php/docker/i386/Dockerfile.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%YAML 1.2
--- |
# Copyright 2020 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM i386/php:${settings.php_version.php_current_version()}

RUN apt-get -qq update && apt-get -qq install -y ${'\\'}
autoconf automake git libtool pkg-config ${'\\'}
valgrind wget zlib1g-dev

ARG MAKEFLAGS=-j8


WORKDIR /tmp

<%include file="../download_phpunit.include" />

<%include file="../pecl_ext_build_src.include" />

CMD ["/github/grpc/src/php/bin/run_tests.sh", "--skip-persistent-channel-tests", "--ignore-valgrind-undef-errors"]

0 comments on commit 61879df

Please sign in to comment.