Debian Codegraph @

src/python/grpcio_tests/tests/unit/beta/_utilities_test.py

raw
1 # Copyright 2015 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 """Tests of grpc.beta.utilities."""
15
16 import threading
17 import time
18 import unittest
19
20 from grpc.beta import implementations
21 from grpc.beta import utilities
22 from grpc.framework.foundation import future
23
24 from tests.unit.framework.common import test_constants
25
26
27 class _Callback(object):
28
29 def __init__(self):
30 self._condition = threading.Condition()
31 self._value = None
32
33 def accept_value(self, value):
34 with self._condition:
35 self._value = value
36 self._condition.notify_all()
37
38 def block_until_called(self):
39 with self._condition:
40 while self._value is None:
41 self._condition.wait()
42 return self._value
43
44
45 @unittest.skip('https://github.com/grpc/grpc/issues/16134')
46 class ChannelConnectivityTest(unittest.TestCase):
47
48 def test_lonely_channel_connectivity(self):
49 channel = implementations.insecure_channel('localhost', 12345)
50 callback = _Callback()
51
52 ready_future = utilities.channel_ready_future(channel)
53 ready_future.add_done_callback(callback.accept_value)
54 with self.assertRaises(future.TimeoutError):
55 ready_future.result(timeout=test_constants.SHORT_TIMEOUT)
56 self.assertFalse(ready_future.cancelled())
57 self.assertFalse(ready_future.done())
58 self.assertTrue(ready_future.running())
59 ready_future.cancel()
60 value_passed_to_callback = callback.block_until_called()
61 self.assertIs(ready_future, value_passed_to_callback)
62 self.assertTrue(ready_future.cancelled())
63 self.assertTrue(ready_future.done())
64 self.assertFalse(ready_future.running())
65
66 def test_immediately_connectable_channel_connectivity(self):
67 server = implementations.server({})
68 port = server.add_insecure_port('[::]:0')
69 server.start()
70 channel = implementations.insecure_channel('localhost', port)
71 callback = _Callback()
72
73 try:
74 ready_future = utilities.channel_ready_future(channel)
75 ready_future.add_done_callback(callback.accept_value)
76 self.assertIsNone(
77 ready_future.result(timeout=test_constants.LONG_TIMEOUT))
78 value_passed_to_callback = callback.block_until_called()
79 self.assertIs(ready_future, value_passed_to_callback)
80 self.assertFalse(ready_future.cancelled())
81 self.assertTrue(ready_future.done())
82 self.assertFalse(ready_future.running())
83 # Cancellation after maturity has no effect.
84 ready_future.cancel()
85 self.assertFalse(ready_future.cancelled())
86 self.assertTrue(ready_future.done())
87 self.assertFalse(ready_future.running())
88 finally:
89 ready_future.cancel()
90 server.stop(0)
91
92
93 if __name__ == '__main__':
94 unittest.main(verbosity=2)