Use C-Core API to perform time conversion

pull/18398/head
Lidi Zheng 6 years ago
parent 585582c4a7
commit c3ecc61867
  1. 6
      src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi
  2. 2
      src/python/grpcio/grpc/_cython/_cygrpc/time.pxd.pxi
  3. 13
      src/python/grpcio/grpc/_cython/_cygrpc/time.pyx.pxi
  4. 6
      src/python/grpcio_tests/tests/unit/_cython/_channel_test.py

@ -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

@ -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 *

@ -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(
<int64_t>(<double>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 <double>real_timespec.seconds + <double>real_timespec.nanoseconds / 1e9
return gpr_timespec_to_micros(real_timespec) / GPR_US_PER_SEC

@ -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)

Loading…
Cancel
Save