Ejemplo n.º 1
0
  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 tickets 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 tickets 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 tickets arrived within SMALL_TIMEOUT of one another at the back.
    # The back started processing based on the first ticket and then stopped
    # upon receiving the cancellation ticket.
    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))
Ejemplo n.º 2
0
  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])
Ejemplo n.º 3
0
 def _stop(self):
   with self._lock:
     self._fore_link.stop()
     base_utilities.wait_for_idle(self._back)
     self._back = None
     self._pool.shutdown(wait=True)
     self._pool = None
Ejemplo n.º 4
0
    def testBidirectionalStreamingEcho(self):
        """Tests sending multiple tickets 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())
Ejemplo n.º 5
0
 def tear_down_implementation(self, memo):
   rear_link, fore_link, front, back = memo
   # TODO(nathaniel): Waiting for the front and back to idle possibly should
   # not be necessary - investigate as part of graceful shutdown work.
   util.wait_for_idle(front)
   util.wait_for_idle(back)
   rear_link.stop()
   fore_link.stop()
Ejemplo n.º 6
0
 def tear_down_implementation(self, memo):
     rear_link, fore_link, front, back = memo
     # TODO(nathaniel): Waiting for the front and back to idle possibly should
     # not be necessary - investigate as part of graceful shutdown work.
     util.wait_for_idle(front)
     util.wait_for_idle(back)
     rear_link.stop()
     fore_link.stop()
Ejemplo n.º 7
0
 def __exit__(self, exc_type, exc_val, exc_tb):
   with self._lock:
     self._under_stub = None
     self._rear_link.stop()
     base_utilities.wait_for_idle(self._front)
     self._front = None
     self._pool.shutdown(wait=True)
     self._pool = None
   return False
Ejemplo n.º 8
0
 def tearDown(self):
   util.wait_for_idle(self.back)
   util.wait_for_idle(self.front)
   self.memory_transmission_pool.shutdown(wait=True)
   self.front_work_pool.shutdown(wait=True)
   self.front_transmission_pool.shutdown(wait=True)
   self.front_utility_pool.shutdown(wait=True)
   self.back_work_pool.shutdown(wait=True)
   self.back_transmission_pool.shutdown(wait=True)
   self.back_utility_pool.shutdown(wait=True)
   self.test_pool.shutdown(wait=True)
Ejemplo n.º 9
0
 def _stop(self):
     with self._lock:
         if self._pool is None:
             raise ValueError("Server not running!")
         else:
             self._fore_link.stop()
             _base_utilities.wait_for_idle(self._back)
             self._pool.shutdown(wait=True)
             self._fore_link = None
             self._back = None
             self._pool = None
Ejemplo n.º 10
0
 def _stop(self):
     with self._lock:
         if self._pool is None:
             raise ValueError('Server not running!')
         else:
             self._fore_link.stop()
             _base_utilities.wait_for_idle(self._back)
             self._pool.shutdown(wait=True)
             self._fore_link = None
             self._back = None
             self._pool = None
Ejemplo n.º 11
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     with self._lock:
         if self._pool is None:
             raise ValueError('Tried to __exit__ non-__enter__ed Stub!')
         else:
             self._rear_link.stop()
             _base_utilities.wait_for_idle(self._front)
             self._pool.shutdown(wait=True)
             self._rear_link = None
             self._front = None
             self._pool = None
             self._understub = None
     return False
Ejemplo n.º 12
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     with self._lock:
         if self._pool is None:
             raise ValueError("Tried to __exit__ non-__enter__ed Stub!")
         else:
             self._rear_link.stop()
             _base_utilities.wait_for_idle(self._front)
             self._pool.shutdown(wait=True)
             self._rear_link = None
             self._front = None
             self._pool = None
             self._understub = None
     return False
Ejemplo n.º 13
0
  def testEntireEcho(self):
    """Tests a very simple one-ticket-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)
Ejemplo n.º 14
0
  def testBidirectionalStreamingEcho(self):
    """Tests sending multiple tickets 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())
Ejemplo n.º 15
0
  def testSimplestCall(self):
    """Tests the absolute simplest call - a one-ticket 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 ticket 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 ticket 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 ticket 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))
Ejemplo n.º 16
0
    def shut_down(self):
        _base_util.wait_for_idle(self._front)
        _base_util.wait_for_idle(self._back)

        for pool in self._pools:
            pool.shutdown(wait=True)
Ejemplo n.º 17
0
  def shut_down(self):
    _base_util.wait_for_idle(self.front)
    _base_util.wait_for_idle(self.back)

    for pool in self._pools:
      pool.shutdown(wait=True)