| 1 |
# Copyright 2016 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import threading |
| 16 |
import time |
| 17 |
import unittest |
| 18 |
|
| 19 |
from grpc._cython import cygrpc |
| 20 |
|
| 21 |
from tests.unit.framework.common import test_constants |
| 22 |
|
| 23 |
|
| 24 |
def _channel(): |
| 25 |
return cygrpc.Channel(b'localhost:54321', (), None) |
| 26 |
|
| 27 |
|
| 28 |
def _connectivity_loop(channel): |
| 29 |
for _ in range(100): |
| 30 |
connectivity = channel.check_connectivity_state(True) |
| 31 |
channel.watch_connectivity_state(connectivity, time.time() + 0.2) |
| 32 |
|
| 33 |
|
| 34 |
def _create_loop_destroy(): |
| 35 |
channel = _channel() |
| 36 |
_connectivity_loop(channel) |
| 37 |
channel.close(cygrpc.StatusCode.ok, 'Channel close!') |
| 38 |
|
| 39 |
|
| 40 |
def _in_parallel(behavior, arguments): |
| 41 |
threads = tuple( |
| 42 |
threading.Thread(target=behavior, args=arguments) |
| 43 |
for _ in range(test_constants.THREAD_CONCURRENCY)) |
| 44 |
for thread in threads: |
| 45 |
thread.start() |
| 46 |
for thread in threads: |
| 47 |
thread.join() |
| 48 |
|
| 49 |
|
| 50 |
class ChannelTest(unittest.TestCase): |
| 51 |
|
| 52 |
def test_single_channel_lonely_connectivity(self): |
| 53 |
channel = _channel() |
| 54 |
_connectivity_loop(channel) |
| 55 |
channel.close(cygrpc.StatusCode.ok, 'Channel close!') |
| 56 |
|
| 57 |
def test_multiple_channels_lonely_connectivity(self): |
| 58 |
_in_parallel(_create_loop_destroy, ()) |
| 59 |
|
| 60 |
def test_negative_deadline_connectivity(self): |
| 61 |
channel = _channel() |
| 62 |
connectivity = channel.check_connectivity_state(True) |
| 63 |
channel.watch_connectivity_state(connectivity, -3.14) |
| 64 |
channel.close(cygrpc.StatusCode.ok, 'Channel close!') |
| 65 |
# NOTE(lidiz) The negative timeout should not trigger SIGABRT. |
| 66 |
# Bug report: https://github.com/grpc/grpc/issues/18244 |
| 67 |
|
| 68 |
|
| 69 |
if __name__ == '__main__': |
| 70 |
unittest.main(verbosity=2) |