aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/grpcio_tests/tests/unit/_cython/_no_messages_server_completion_queue_per_call_test.py
blob: 8a721788f4ea34c5a3d4d6593a171348f283a28e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2017 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test a corner-case at the level of the Cython API."""

import threading
import unittest

from grpc._cython import cygrpc

from tests.unit._cython import _common
from tests.unit._cython import test_utilities


class Test(_common.RpcTest, unittest.TestCase):

    def _do_rpcs(self):
        server_call_condition = threading.Condition()
        server_call_completion_queue = cygrpc.CompletionQueue()
        server_call_driver = _common.QueueDriver(server_call_condition,
                                                 server_call_completion_queue)

        server_request_call_tag = 'server_request_call_tag'
        server_send_initial_metadata_tag = 'server_send_initial_metadata_tag'
        server_complete_rpc_tag = 'server_complete_rpc_tag'

        with self.server_condition:
            server_request_call_start_batch_result = self.server.request_call(
                server_call_completion_queue, self.server_completion_queue,
                server_request_call_tag)
            self.server_driver.add_due({
                server_request_call_tag,
            })

        client_receive_initial_metadata_tag = 'client_receive_initial_metadata_tag'
        client_complete_rpc_tag = 'client_complete_rpc_tag'
        client_call = self.channel.integrated_call(
            _common.EMPTY_FLAGS, b'/twinkies', None, None,
            _common.INVOCATION_METADATA, None, [(
                [
                    cygrpc.ReceiveInitialMetadataOperation(_common.EMPTY_FLAGS),
                ],
                client_receive_initial_metadata_tag,
            )])
        client_call.operate([
            cygrpc.SendInitialMetadataOperation(_common.INVOCATION_METADATA,
                                                _common.EMPTY_FLAGS),
            cygrpc.SendCloseFromClientOperation(_common.EMPTY_FLAGS),
            cygrpc.ReceiveStatusOnClientOperation(_common.EMPTY_FLAGS),
        ], client_complete_rpc_tag)

        client_events_future = test_utilities.SimpleFuture(
            lambda: [
                self.channel.next_call_event(),
                self.channel.next_call_event(),])

        server_request_call_event = self.server_driver.event_with_tag(
            server_request_call_tag)

        with server_call_condition:
            server_send_initial_metadata_start_batch_result = (
                server_request_call_event.call.start_server_batch([
                    cygrpc.SendInitialMetadataOperation(
                        _common.INITIAL_METADATA, _common.EMPTY_FLAGS),
                ], server_send_initial_metadata_tag))
            server_call_driver.add_due({
                server_send_initial_metadata_tag,
            })
        server_send_initial_metadata_event = server_call_driver.event_with_tag(
            server_send_initial_metadata_tag)

        with server_call_condition:
            server_complete_rpc_start_batch_result = (
                server_request_call_event.call.start_server_batch([
                    cygrpc.ReceiveCloseOnServerOperation(_common.EMPTY_FLAGS),
                    cygrpc.SendStatusFromServerOperation(
                        _common.TRAILING_METADATA, cygrpc.StatusCode.ok,
                        b'test details', _common.EMPTY_FLAGS),
                ], server_complete_rpc_tag))
            server_call_driver.add_due({
                server_complete_rpc_tag,
            })
        server_complete_rpc_event = server_call_driver.event_with_tag(
            server_complete_rpc_tag)

        client_events = client_events_future.result()
        if client_events[0].tag is client_receive_initial_metadata_tag:
            client_receive_initial_metadata_event = client_events[0]
            client_complete_rpc_event = client_events[1]
        else:
            client_complete_rpc_event = client_events[0]
            client_receive_initial_metadata_event = client_events[1]

        return (
            _common.OperationResult(server_request_call_start_batch_result,
                                    server_request_call_event.completion_type,
                                    server_request_call_event.success),
            _common.OperationResult(
                cygrpc.CallError.ok,
                client_receive_initial_metadata_event.completion_type,
                client_receive_initial_metadata_event.success),
            _common.OperationResult(cygrpc.CallError.ok,
                                    client_complete_rpc_event.completion_type,
                                    client_complete_rpc_event.success),
            _common.OperationResult(
                server_send_initial_metadata_start_batch_result,
                server_send_initial_metadata_event.completion_type,
                server_send_initial_metadata_event.success),
            _common.OperationResult(server_complete_rpc_start_batch_result,
                                    server_complete_rpc_event.completion_type,
                                    server_complete_rpc_event.success),
        )

    def test_rpcs(self):
        expecteds = [(
            _common.SUCCESSFUL_OPERATION_RESULT,) * 5] * _common.RPC_COUNT
        actuallys = _common.execute_many_times(self._do_rpcs)
        self.assertSequenceEqual(expecteds, actuallys)


if __name__ == '__main__':
    unittest.main(verbosity=2)