aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/src/grpc/framework/base/interfaces_test.py
blob: b86011c449efe4cdaef7f707f2bf528afcf2412c (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright 2015, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Abstract tests against the interfaces of the base layer of RPC Framework."""

import threading
import time

from grpc.framework.base import interfaces
from grpc.framework.base import util
from grpc.framework.foundation import stream
from grpc.framework.foundation import stream_testing
from grpc.framework.foundation import stream_util

TICK = 0.1
SMALL_TIMEOUT = TICK * 50
STREAM_LENGTH = 100

SYNCHRONOUS_ECHO = 'synchronous echo'
ASYNCHRONOUS_ECHO = 'asynchronous echo'
IMMEDIATE_FAILURE = 'immediate failure'
TRIGGERED_FAILURE = 'triggered failure'
WAIT_ON_CONDITION = 'wait on condition'

EMPTY_OUTCOME_DICT = {
    interfaces.Outcome.COMPLETED: 0,
    interfaces.Outcome.CANCELLED: 0,
    interfaces.Outcome.EXPIRED: 0,
    interfaces.Outcome.RECEPTION_FAILURE: 0,
    interfaces.Outcome.TRANSMISSION_FAILURE: 0,
    interfaces.Outcome.SERVICER_FAILURE: 0,
    interfaces.Outcome.SERVICED_FAILURE: 0,
    }


def _synchronous_echo(output_consumer):
  return stream_util.TransformingConsumer(lambda x: x, output_consumer)


class AsynchronousEcho(stream.Consumer):
  """A stream.Consumer that echoes its input to another stream.Consumer."""

  def __init__(self, output_consumer, pool):
    self._lock = threading.Lock()
    self._output_consumer = output_consumer
    self._pool = pool

    self._queue = []
    self._spinning = False

  def _spin(self, value, complete):
    while True:
      if value:
        if complete:
          self._output_consumer.consume_and_terminate(value)
        else:
          self._output_consumer.consume(value)
      elif complete:
        self._output_consumer.terminate()
      with self._lock:
        if self._queue:
          value, complete = self._queue.pop(0)
        else:
          self._spinning = False
          return

  def consume(self, value):
    with self._lock:
      if self._spinning:
        self._queue.append((value, False))
      else:
        self._spinning = True
        self._pool.submit(self._spin, value, False)

  def terminate(self):
    with self._lock:
      if self._spinning:
        self._queue.append((None, True))
      else:
        self._spinning = True
        self._pool.submit(self._spin, None, True)

  def consume_and_terminate(self, value):
    with self._lock:
      if self._spinning:
        self._queue.append((value, True))
      else:
        self._spinning = True
        self._pool.submit(self._spin, value, True)


class TestServicer(interfaces.Servicer):
  """An interfaces.Servicer with instrumented for testing."""

  def __init__(self, pool):
    self._pool = pool
    self.condition = threading.Condition()
    self._released = False

  def service(self, name, context, output_consumer):
    if name == SYNCHRONOUS_ECHO:
      return _synchronous_echo(output_consumer)
    elif name == ASYNCHRONOUS_ECHO:
      return AsynchronousEcho(output_consumer, self._pool)
    elif name == IMMEDIATE_FAILURE:
      raise ValueError()
    elif name == TRIGGERED_FAILURE:
      raise NotImplementedError
    elif name == WAIT_ON_CONDITION:
      with self.condition:
        while not self._released:
          self.condition.wait()
      return _synchronous_echo(output_consumer)
    else:
      raise NotImplementedError()

  def release(self):
    with self.condition:
      self._released = True
      self.condition.notify_all()


class EasyServicedIngestor(interfaces.ServicedIngestor):
  """A trivial implementation of interfaces.ServicedIngestor."""

  def __init__(self, consumer):
    self._consumer = consumer

  def consumer(self, operation_context):
    """See interfaces.ServicedIngestor.consumer for specification."""
    return self._consumer


class FrontAndBackTest(object):
  """A test suite usable against any joined Front and Back."""

  # Pylint doesn't know that this is a unittest.TestCase mix-in.
  # pylint: disable=invalid-name

  def testSimplestCall(self):
    """Tests the absolute simplest call - a one-packet fire-and-forget."""
    self.front.operate(
        SYNCHRONOUS_ECHO, None, True, SMALL_TIMEOUT,
        util.none_serviced_subscription(), 'test trace ID')
    util.wait_for_idle(self.front)
    self.assertEqual(
        1, self.front.operation_stats()[interfaces.Outcome.COMPLETED])

    # Assuming nothing really pathological (such as pauses on the order of
    # SMALL_TIMEOUT interfering with this test) there are a two different ways
    # the back could have experienced execution up to this point:
    # (1) The packet is still either in the front waiting to be transmitted
    # or is somewhere on the link between the front and the back. The back has
    # no idea that this test is even happening. Calling wait_for_idle on it
    # would do no good because in this case the back is idle and the call would
    # return with the packet bound for it still in the front or on the link.
    back_operation_stats = self.back.operation_stats()
    first_back_possibility = EMPTY_OUTCOME_DICT
    # (2) The packet arrived at the back and the back completed the operation.
    second_back_possibility = dict(EMPTY_OUTCOME_DICT)
    second_back_possibility[interfaces.Outcome.COMPLETED] = 1
    self.assertIn(
        back_operation_stats, (first_back_possibility, second_back_possibility))
    # It's true that if the packet had arrived at the back and the back had
    # begun processing that wait_for_idle could hold test execution until the
    # back completed the operation, but that doesn't really collapse the
    # possibility space down to one solution.

  def testEntireEcho(self):
    """Tests a very simple one-packet-each-way round-trip."""
    test_payload = 'test payload'
    test_consumer = stream_testing.TestConsumer()
    subscription = util.full_serviced_subscription(
        EasyServicedIngestor(test_consumer))

    self.front.operate(
        ASYNCHRONOUS_ECHO, test_payload, True, SMALL_TIMEOUT, subscription,
        'test trace ID')

    util.wait_for_idle(self.front)
    util.wait_for_idle(self.back)
    self.assertEqual(
        1, self.front.operation_stats()[interfaces.Outcome.COMPLETED])
    self.assertEqual(
        1, self.back.operation_stats()[interfaces.Outcome.COMPLETED])
    self.assertListEqual([(test_payload, True)], test_consumer.calls)

  def testBidirectionalStreamingEcho(self):
    """Tests sending multiple packets each way."""
    test_payload_template = 'test_payload: %03d'
    test_payloads = [test_payload_template % i for i in range(STREAM_LENGTH)]
    test_consumer = stream_testing.TestConsumer()
    subscription = util.full_serviced_subscription(
        EasyServicedIngestor(test_consumer))

    operation = self.front.operate(
        SYNCHRONOUS_ECHO, None, False, SMALL_TIMEOUT, subscription,
        'test trace ID')

    for test_payload in test_payloads:
      operation.consumer.consume(test_payload)
    operation.consumer.terminate()

    util.wait_for_idle(self.front)
    util.wait_for_idle(self.back)
    self.assertEqual(
        1, self.front.operation_stats()[interfaces.Outcome.COMPLETED])
    self.assertEqual(
        1, self.back.operation_stats()[interfaces.Outcome.COMPLETED])
    self.assertListEqual(test_payloads, test_consumer.values())

  def testCancellation(self):
    """Tests cancelling a long-lived operation."""
    test_consumer = stream_testing.TestConsumer()
    subscription = util.full_serviced_subscription(
        EasyServicedIngestor(test_consumer))

    operation = self.front.operate(
        ASYNCHRONOUS_ECHO, None, False, SMALL_TIMEOUT, subscription,
        'test trace ID')
    operation.cancel()

    util.wait_for_idle(self.front)
    self.assertEqual(
        1, self.front.operation_stats()[interfaces.Outcome.CANCELLED])
    util.wait_for_idle(self.back)
    self.assertListEqual([], test_consumer.calls)

    # Assuming nothing really pathological (such as pauses on the order of
    # SMALL_TIMEOUT interfering with this test) there are a two different ways
    # the back could have experienced execution up to this point:
    # (1) Both packets are still either in the front waiting to be transmitted
    # or are somewhere on the link between the front and the back. The back has
    # no idea that this test is even happening. Calling wait_for_idle on it
    # would do no good because in this case the back is idle and the call would
    # return with the packets bound for it still in the front or on the link.
    back_operation_stats = self.back.operation_stats()
    first_back_possibility = EMPTY_OUTCOME_DICT
    # (2) Both packets arrived within SMALL_TIMEOUT of one another at the back.
    # The back started processing based on the first packet and then stopped
    # upon receiving the cancellation packet.
    second_back_possibility = dict(EMPTY_OUTCOME_DICT)
    second_back_possibility[interfaces.Outcome.CANCELLED] = 1
    self.assertIn(
        back_operation_stats, (first_back_possibility, second_back_possibility))

  def testExpiration(self):
    """Tests that operations time out."""
    timeout = TICK * 2
    allowance = TICK  # How much extra time to
    condition = threading.Condition()
    test_payload = 'test payload'
    subscription = util.termination_only_serviced_subscription()
    start_time = time.time()

    outcome_cell = [None]
    termination_time_cell = [None]
    def termination_action(outcome):
      with condition:
        outcome_cell[0] = outcome
        termination_time_cell[0] = time.time()
        condition.notify()

    with condition:
      operation = self.front.operate(
          SYNCHRONOUS_ECHO, test_payload, False, timeout, subscription,
          'test trace ID')
      operation.context.add_termination_callback(termination_action)
      while outcome_cell[0] is None:
        condition.wait()

    duration = termination_time_cell[0] - start_time
    self.assertLessEqual(timeout, duration)
    self.assertLess(duration, timeout + allowance)
    self.assertEqual(interfaces.Outcome.EXPIRED, outcome_cell[0])
    util.wait_for_idle(self.front)
    self.assertEqual(
        1, self.front.operation_stats()[interfaces.Outcome.EXPIRED])
    util.wait_for_idle(self.back)
    self.assertLessEqual(
        1, self.back.operation_stats()[interfaces.Outcome.EXPIRED])