Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 1 | # Copyright 2016 gRPC authors. |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 2 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 6 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 14 | |
| 15 | import time |
| 16 | import threading |
| 17 | import unittest |
| 18 | |
| 19 | from grpc._cython import cygrpc |
| 20 | |
Nathaniel Manista | 7809f2b | 2016-05-05 20:38:42 +0000 | [diff] [blame] | 21 | from tests.unit.framework.common import test_constants |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def _channel_and_completion_queue(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 25 | channel = cygrpc.Channel(b'localhost:54321', cygrpc.ChannelArgs(())) |
| 26 | completion_queue = cygrpc.CompletionQueue() |
| 27 | return channel, completion_queue |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def _connectivity_loop(channel, completion_queue): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 31 | for _ in range(100): |
| 32 | connectivity = channel.check_connectivity_state(True) |
| 33 | channel.watch_connectivity_state(connectivity, |
Nathaniel Manista | 31ddbff | 2018-01-10 22:58:00 +0000 | [diff] [blame] | 34 | time.time() + 0.2, completion_queue, |
| 35 | None) |
| 36 | completion_queue.poll() |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def _create_loop_destroy(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 40 | channel, completion_queue = _channel_and_completion_queue() |
| 41 | _connectivity_loop(channel, completion_queue) |
| 42 | completion_queue.shutdown() |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def _in_parallel(behavior, arguments): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 46 | threads = tuple( |
David Garcia Quintas | 4a1fa69 | 2017-01-30 10:29:49 -0800 | [diff] [blame] | 47 | threading.Thread(target=behavior, args=arguments) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 48 | for _ in range(test_constants.THREAD_CONCURRENCY)) |
| 49 | for thread in threads: |
| 50 | thread.start() |
| 51 | for thread in threads: |
| 52 | thread.join() |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class ChannelTest(unittest.TestCase): |
| 56 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 57 | def test_single_channel_lonely_connectivity(self): |
| 58 | channel, completion_queue = _channel_and_completion_queue() |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 59 | _in_parallel(_connectivity_loop, ( |
| 60 | channel, |
| 61 | completion_queue, |
| 62 | )) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 63 | completion_queue.shutdown() |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 64 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 65 | def test_multiple_channels_lonely_connectivity(self): |
| 66 | _in_parallel(_create_loop_destroy, ()) |
Nathaniel Manista | be31d46 | 2016-01-20 00:01:34 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | if __name__ == '__main__': |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 70 | unittest.main(verbosity=2) |