From c3ecc618672631c36db5a204b94fc6b259bda191 Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Fri, 15 Mar 2019 11:48:10 -0700 Subject: [PATCH] Use C-Core API to perform time conversion --- src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi | 6 ++++++ src/python/grpcio/grpc/_cython/_cygrpc/time.pxd.pxi | 2 +- src/python/grpcio/grpc/_cython/_cygrpc/time.pyx.pxi | 13 ++++++------- .../tests/unit/_cython/_channel_test.py | 6 ++++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi index 88a6c0039c6..7e20f7a4e2d 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi @@ -53,6 +53,10 @@ cdef extern from "grpc/grpc.h": void *grpc_slice_start_ptr "GRPC_SLICE_START_PTR" (grpc_slice s) nogil size_t grpc_slice_length "GRPC_SLICE_LENGTH" (grpc_slice s) nogil + const int GPR_MS_PER_SEC + const int GPR_US_PER_SEC + const int GPR_NS_PER_SEC + ctypedef enum gpr_clock_type: GPR_CLOCK_MONOTONIC GPR_CLOCK_REALTIME @@ -74,6 +78,8 @@ cdef extern from "grpc/grpc.h": gpr_clock_type target_clock) nogil gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type type) nogil + gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type type) nogil + double gpr_timespec_to_micros(gpr_timespec t) nogil gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b) nogil diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/time.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/time.pxd.pxi index 1319ac0481d..c46e8a98b04 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/time.pxd.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/time.pxd.pxi @@ -13,7 +13,7 @@ # limitations under the License. -cdef gpr_timespec _timespec_from_time(object time) +cdef gpr_timespec _timespec_from_time(object time) except * cdef double _time_from_timespec(gpr_timespec timespec) except * diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/time.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/time.pyx.pxi index c452dd54f82..6d181bb1d60 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/time.pyx.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/time.pyx.pxi @@ -13,18 +13,17 @@ # limitations under the License. -cdef gpr_timespec _timespec_from_time(object time): - cdef gpr_timespec timespec +cdef gpr_timespec _timespec_from_time(object time) except *: if time is None: return gpr_inf_future(GPR_CLOCK_REALTIME) else: - timespec.seconds = time - timespec.nanoseconds = (time - float(timespec.seconds)) * 1e9 - timespec.clock_type = GPR_CLOCK_REALTIME - return timespec + return gpr_time_from_nanos( + (time * GPR_NS_PER_SEC), + GPR_CLOCK_REALTIME, + ) cdef double _time_from_timespec(gpr_timespec timespec) except *: cdef gpr_timespec real_timespec = gpr_convert_clock_type( timespec, GPR_CLOCK_REALTIME) - return real_timespec.seconds + real_timespec.nanoseconds / 1e9 + return gpr_timespec_to_micros(real_timespec) / GPR_US_PER_SEC diff --git a/src/python/grpcio_tests/tests/unit/_cython/_channel_test.py b/src/python/grpcio_tests/tests/unit/_cython/_channel_test.py index d95286071d5..9fd9ede144d 100644 --- a/src/python/grpcio_tests/tests/unit/_cython/_channel_test.py +++ b/src/python/grpcio_tests/tests/unit/_cython/_channel_test.py @@ -57,6 +57,12 @@ class ChannelTest(unittest.TestCase): def test_multiple_channels_lonely_connectivity(self): _in_parallel(_create_loop_destroy, ()) + def test_negative_deadline_connectivity(self): + channel = _channel() + connectivity = channel.check_connectivity_state(True) + channel.watch_connectivity_state(connectivity, -3.14) + channel.close(cygrpc.StatusCode.ok, 'Channel close!') + if __name__ == '__main__': unittest.main(verbosity=2)