blob: c22c77ddbd28ce211670011b78d1809667d0638a [file] [log] [blame]
Jan Tattermusch7897ae92017-06-07 22:57:36 +02001# Copyright 2016 gRPC authors.
Nathaniel Manistabe31d462016-01-20 00:01:34 +00002#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# 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
Nathaniel Manistabe31d462016-01-20 00:01:34 +00006#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02007# http://www.apache.org/licenses/LICENSE-2.0
Nathaniel Manistabe31d462016-01-20 00:01:34 +00008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# 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.
Nathaniel Manistabe31d462016-01-20 00:01:34 +000014
15import time
16import threading
17import unittest
18
19from grpc._cython import cygrpc
20
Nathaniel Manista7809f2b2016-05-05 20:38:42 +000021from tests.unit.framework.common import test_constants
Nathaniel Manistabe31d462016-01-20 00:01:34 +000022
23
24def _channel_and_completion_queue():
Masood Malekghassemicc793702017-01-13 19:20:10 -080025 channel = cygrpc.Channel(b'localhost:54321', cygrpc.ChannelArgs(()))
26 completion_queue = cygrpc.CompletionQueue()
27 return channel, completion_queue
Nathaniel Manistabe31d462016-01-20 00:01:34 +000028
29
30def _connectivity_loop(channel, completion_queue):
Masood Malekghassemicc793702017-01-13 19:20:10 -080031 for _ in range(100):
32 connectivity = channel.check_connectivity_state(True)
33 channel.watch_connectivity_state(connectivity,
Nathaniel Manista31ddbff2018-01-10 22:58:00 +000034 time.time() + 0.2, completion_queue,
35 None)
36 completion_queue.poll()
Nathaniel Manistabe31d462016-01-20 00:01:34 +000037
38
39def _create_loop_destroy():
Masood Malekghassemicc793702017-01-13 19:20:10 -080040 channel, completion_queue = _channel_and_completion_queue()
41 _connectivity_loop(channel, completion_queue)
42 completion_queue.shutdown()
Nathaniel Manistabe31d462016-01-20 00:01:34 +000043
44
45def _in_parallel(behavior, arguments):
Masood Malekghassemicc793702017-01-13 19:20:10 -080046 threads = tuple(
David Garcia Quintas4a1fa692017-01-30 10:29:49 -080047 threading.Thread(target=behavior, args=arguments)
Masood Malekghassemicc793702017-01-13 19:20:10 -080048 for _ in range(test_constants.THREAD_CONCURRENCY))
49 for thread in threads:
50 thread.start()
51 for thread in threads:
52 thread.join()
Nathaniel Manistabe31d462016-01-20 00:01:34 +000053
54
55class ChannelTest(unittest.TestCase):
56
Masood Malekghassemicc793702017-01-13 19:20:10 -080057 def test_single_channel_lonely_connectivity(self):
58 channel, completion_queue = _channel_and_completion_queue()
Mehrdad Afshari87cd9942018-01-02 14:40:00 -080059 _in_parallel(_connectivity_loop, (
60 channel,
61 completion_queue,
62 ))
Masood Malekghassemicc793702017-01-13 19:20:10 -080063 completion_queue.shutdown()
Nathaniel Manistabe31d462016-01-20 00:01:34 +000064
Masood Malekghassemicc793702017-01-13 19:20:10 -080065 def test_multiple_channels_lonely_connectivity(self):
66 _in_parallel(_create_loop_destroy, ())
Nathaniel Manistabe31d462016-01-20 00:01:34 +000067
68
69if __name__ == '__main__':
Masood Malekghassemicc793702017-01-13 19:20:10 -080070 unittest.main(verbosity=2)