Exemplo n.º 1
0
  def testCancellation(self):
    method = 'test method'
    deadline = _FUTURE
    metadata_tag = object()
    finish_tag = object()
    write_tag = object()
    service_tag = object()
    read_tag = object()
    test_data = _BYTE_SEQUENCE_SEQUENCE

    server_data = []
    client_data = []

    client_call = _low.Call(self.channel, method, self.host, deadline)

    client_call.invoke(self.client_completion_queue, metadata_tag, finish_tag)

    self.server.service(service_tag)
    service_accepted = self.server_completion_queue.get(_FUTURE)
    server_call = service_accepted.service_acceptance.call

    server_call.accept(self.server_completion_queue, finish_tag)
    server_call.premetadata()

    metadata_accepted = self.client_completion_queue.get(_FUTURE)
    self.assertIsNotNone(metadata_accepted)

    for datum in test_data:
      client_call.write(datum, write_tag)
      write_accepted = self.client_completion_queue.get(_FUTURE)

      server_call.read(read_tag)
      read_accepted = self.server_completion_queue.get(_FUTURE)
      server_data.append(read_accepted.bytes)

      server_call.write(read_accepted.bytes, write_tag)
      write_accepted = self.server_completion_queue.get(_FUTURE)
      self.assertIsNotNone(write_accepted)

      client_call.read(read_tag)
      read_accepted = self.client_completion_queue.get(_FUTURE)
      client_data.append(read_accepted.bytes)

    client_call.cancel()
    # cancel() is idempotent.
    client_call.cancel()
    client_call.cancel()
    client_call.cancel()

    server_call.read(read_tag)

    server_terminal_event_one = self.server_completion_queue.get(_FUTURE)
    server_terminal_event_two = self.server_completion_queue.get(_FUTURE)
    if server_terminal_event_one.kind == _low.Event.Kind.READ_ACCEPTED:
      read_accepted = server_terminal_event_one
      rpc_accepted = server_terminal_event_two
    else:
      read_accepted = server_terminal_event_two
      rpc_accepted = server_terminal_event_one
    self.assertIsNotNone(read_accepted)
    self.assertIsNotNone(rpc_accepted)
    self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
    self.assertIsNone(read_accepted.bytes)
    self.assertEqual(_low.Event.Kind.FINISH, rpc_accepted.kind)
    self.assertEqual(_low.Status(_low.Code.CANCELLED, ''), rpc_accepted.status)

    finish_event = self.client_completion_queue.get(_FUTURE)
    self.assertEqual(_low.Event.Kind.FINISH, finish_event.kind)
    self.assertEqual(_low.Status(_low.Code.CANCELLED, ''), finish_event.status)

    server_timeout_none_event = self.server_completion_queue.get(0)
    self.assertIsNone(server_timeout_none_event)
    client_timeout_none_event = self.client_completion_queue.get(0)
    self.assertIsNone(client_timeout_none_event)

    self.assertSequenceEqual(test_data, server_data)
    self.assertSequenceEqual(test_data, client_data)
Exemplo n.º 2
0
def _status(call, rpc_state):
    call.status(_low.Status(_low.Code.OK, bytes()), call)
    rpc_state.write.low = _LowWrite.CLOSED
Exemplo n.º 3
0
  def _perform_echo_test(self, test_data):
    method = 'test method'
    details = 'test details'
    server_leading_metadata_key = 'my_server_leading_key'
    server_leading_metadata_value = 'my_server_leading_value'
    server_trailing_metadata_key = 'my_server_trailing_key'
    server_trailing_metadata_value = 'my_server_trailing_value'
    client_metadata_key = 'my_client_key'
    client_metadata_value = 'my_client_value'
    server_leading_binary_metadata_key = 'my_server_leading_key-bin'
    server_leading_binary_metadata_value = b'\0'*2047
    server_trailing_binary_metadata_key = 'my_server_trailing_key-bin'
    server_trailing_binary_metadata_value = b'\0'*2047
    client_binary_metadata_key = 'my_client_key-bin'
    client_binary_metadata_value = b'\0'*2047
    deadline = _FUTURE
    metadata_tag = object()
    finish_tag = object()
    write_tag = object()
    complete_tag = object()
    service_tag = object()
    read_tag = object()
    status_tag = object()

    server_data = []
    client_data = []

    client_call = _low.Call(self.channel, method, self.host, deadline)
    client_call.add_metadata(client_metadata_key, client_metadata_value)
    client_call.add_metadata(client_binary_metadata_key,
                             client_binary_metadata_value)

    client_call.invoke(self.client_completion_queue, metadata_tag, finish_tag)

    self.server.service(service_tag)
    service_accepted = self.server_completion_queue.get(_FUTURE)
    self.assertIsNotNone(service_accepted)
    self.assertIs(service_accepted.kind, _low.Event.Kind.SERVICE_ACCEPTED)
    self.assertIs(service_accepted.tag, service_tag)
    self.assertEqual(method, service_accepted.service_acceptance.method)
    self.assertEqual(self.host, service_accepted.service_acceptance.host)
    self.assertIsNotNone(service_accepted.service_acceptance.call)
    metadata = dict(service_accepted.metadata)
    self.assertIn(client_metadata_key, metadata)
    self.assertEqual(client_metadata_value, metadata[client_metadata_key])
    self.assertIn(client_binary_metadata_key, metadata)
    self.assertEqual(client_binary_metadata_value,
                     metadata[client_binary_metadata_key])
    server_call = service_accepted.service_acceptance.call
    server_call.accept(self.server_completion_queue, finish_tag)
    server_call.add_metadata(server_leading_metadata_key,
                             server_leading_metadata_value)
    server_call.add_metadata(server_leading_binary_metadata_key,
                             server_leading_binary_metadata_value)
    server_call.premetadata()

    metadata_accepted = self.client_completion_queue.get(_FUTURE)
    self.assertIsNotNone(metadata_accepted)
    self.assertEqual(_low.Event.Kind.METADATA_ACCEPTED, metadata_accepted.kind)
    self.assertEqual(metadata_tag, metadata_accepted.tag)
    metadata = dict(metadata_accepted.metadata)
    self.assertIn(server_leading_metadata_key, metadata)
    self.assertEqual(server_leading_metadata_value,
                     metadata[server_leading_metadata_key])
    self.assertIn(server_leading_binary_metadata_key, metadata)
    self.assertEqual(server_leading_binary_metadata_value,
                     metadata[server_leading_binary_metadata_key])

    for datum in test_data:
      client_call.write(datum, write_tag)
      write_accepted = self.client_completion_queue.get(_FUTURE)
      self.assertIsNotNone(write_accepted)
      self.assertIs(write_accepted.kind, _low.Event.Kind.WRITE_ACCEPTED)
      self.assertIs(write_accepted.tag, write_tag)
      self.assertIs(write_accepted.write_accepted, True)

      server_call.read(read_tag)
      read_accepted = self.server_completion_queue.get(_FUTURE)
      self.assertIsNotNone(read_accepted)
      self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
      self.assertEqual(read_tag, read_accepted.tag)
      self.assertIsNotNone(read_accepted.bytes)
      server_data.append(read_accepted.bytes)

      server_call.write(read_accepted.bytes, write_tag)
      write_accepted = self.server_completion_queue.get(_FUTURE)
      self.assertIsNotNone(write_accepted)
      self.assertEqual(_low.Event.Kind.WRITE_ACCEPTED, write_accepted.kind)
      self.assertEqual(write_tag, write_accepted.tag)
      self.assertTrue(write_accepted.write_accepted)

      client_call.read(read_tag)
      read_accepted = self.client_completion_queue.get(_FUTURE)
      self.assertIsNotNone(read_accepted)
      self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
      self.assertEqual(read_tag, read_accepted.tag)
      self.assertIsNotNone(read_accepted.bytes)
      client_data.append(read_accepted.bytes)

    client_call.complete(complete_tag)
    complete_accepted = self.client_completion_queue.get(_FUTURE)
    self.assertIsNotNone(complete_accepted)
    self.assertIs(complete_accepted.kind, _low.Event.Kind.COMPLETE_ACCEPTED)
    self.assertIs(complete_accepted.tag, complete_tag)
    self.assertIs(complete_accepted.complete_accepted, True)

    server_call.read(read_tag)
    read_accepted = self.server_completion_queue.get(_FUTURE)
    self.assertIsNotNone(read_accepted)
    self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
    self.assertEqual(read_tag, read_accepted.tag)
    self.assertIsNone(read_accepted.bytes)

    server_call.add_metadata(server_trailing_metadata_key,
                             server_trailing_metadata_value)
    server_call.add_metadata(server_trailing_binary_metadata_key,
                             server_trailing_binary_metadata_value)

    server_call.status(_low.Status(_low.Code.OK, details), status_tag)
    server_terminal_event_one = self.server_completion_queue.get(_FUTURE)
    server_terminal_event_two = self.server_completion_queue.get(_FUTURE)
    if server_terminal_event_one.kind == _low.Event.Kind.COMPLETE_ACCEPTED:
      status_accepted = server_terminal_event_one
      rpc_accepted = server_terminal_event_two
    else:
      status_accepted = server_terminal_event_two
      rpc_accepted = server_terminal_event_one
    self.assertIsNotNone(status_accepted)
    self.assertIsNotNone(rpc_accepted)
    self.assertEqual(_low.Event.Kind.COMPLETE_ACCEPTED, status_accepted.kind)
    self.assertEqual(status_tag, status_accepted.tag)
    self.assertTrue(status_accepted.complete_accepted)
    self.assertEqual(_low.Event.Kind.FINISH, rpc_accepted.kind)
    self.assertEqual(finish_tag, rpc_accepted.tag)
    self.assertEqual(_low.Status(_low.Code.OK, ''), rpc_accepted.status)

    client_call.read(read_tag)
    client_terminal_event_one = self.client_completion_queue.get(_FUTURE)
    client_terminal_event_two = self.client_completion_queue.get(_FUTURE)
    if client_terminal_event_one.kind == _low.Event.Kind.READ_ACCEPTED:
      read_accepted = client_terminal_event_one
      finish_accepted = client_terminal_event_two
    else:
      read_accepted = client_terminal_event_two
      finish_accepted = client_terminal_event_one
    self.assertIsNotNone(read_accepted)
    self.assertIsNotNone(finish_accepted)
    self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
    self.assertEqual(read_tag, read_accepted.tag)
    self.assertIsNone(read_accepted.bytes)
    self.assertEqual(_low.Event.Kind.FINISH, finish_accepted.kind)
    self.assertEqual(finish_tag, finish_accepted.tag)
    self.assertEqual(_low.Status(_low.Code.OK, details), finish_accepted.status)
    metadata = dict(finish_accepted.metadata)
    self.assertIn(server_trailing_metadata_key, metadata)
    self.assertEqual(server_trailing_metadata_value,
                     metadata[server_trailing_metadata_key])
    self.assertIn(server_trailing_binary_metadata_key, metadata)
    self.assertEqual(server_trailing_binary_metadata_value,
                     metadata[server_trailing_binary_metadata_key])

    server_timeout_none_event = self.server_completion_queue.get(0)
    self.assertIsNone(server_timeout_none_event)
    client_timeout_none_event = self.client_completion_queue.get(0)
    self.assertIsNone(client_timeout_none_event)

    self.assertSequenceEqual(test_data, server_data)
    self.assertSequenceEqual(test_data, client_data)
Exemplo n.º 4
0
    def _perform_echo_test(self, test_data):
        method = 'test method'
        details = 'test details'
        deadline = _FUTURE
        metadata_tag = object()
        finish_tag = object()
        write_tag = object()
        complete_tag = object()
        service_tag = object()
        read_tag = object()
        status_tag = object()

        server_data = []
        client_data = []

        client_call = _low.Call(self.channel, method, self.host, deadline)

        client_call.invoke(self.client_completion_queue, metadata_tag,
                           finish_tag)

        self.server.service(service_tag)
        service_accepted = self.server_completion_queue.get(_FUTURE)
        self.assertIsNotNone(service_accepted)
        self.assertIs(service_accepted.kind, _low.Event.Kind.SERVICE_ACCEPTED)
        self.assertIs(service_accepted.tag, service_tag)
        self.assertEqual(method, service_accepted.service_acceptance.method)
        self.assertEqual(self.host, service_accepted.service_acceptance.host)
        self.assertIsNotNone(service_accepted.service_acceptance.call)
        server_call = service_accepted.service_acceptance.call
        server_call.accept(self.server_completion_queue, finish_tag)
        server_call.premetadata()

        metadata_accepted = self.client_completion_queue.get(_FUTURE)
        self.assertIsNotNone(metadata_accepted)
        self.assertEqual(_low.Event.Kind.METADATA_ACCEPTED,
                         metadata_accepted.kind)
        self.assertEqual(metadata_tag, metadata_accepted.tag)
        # TODO(nathaniel): Test transmission and reception of metadata.

        for datum in test_data:
            client_call.write(datum, write_tag)
            write_accepted = self.client_completion_queue.get(_FUTURE)
            self.assertIsNotNone(write_accepted)
            self.assertIs(write_accepted.kind, _low.Event.Kind.WRITE_ACCEPTED)
            self.assertIs(write_accepted.tag, write_tag)
            self.assertIs(write_accepted.write_accepted, True)

            server_call.read(read_tag)
            read_accepted = self.server_completion_queue.get(_FUTURE)
            self.assertIsNotNone(read_accepted)
            self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
            self.assertEqual(read_tag, read_accepted.tag)
            self.assertIsNotNone(read_accepted.bytes)
            server_data.append(read_accepted.bytes)

            server_call.write(read_accepted.bytes, write_tag)
            write_accepted = self.server_completion_queue.get(_FUTURE)
            self.assertIsNotNone(write_accepted)
            self.assertEqual(_low.Event.Kind.WRITE_ACCEPTED,
                             write_accepted.kind)
            self.assertEqual(write_tag, write_accepted.tag)
            self.assertTrue(write_accepted.write_accepted)

            client_call.read(read_tag)
            read_accepted = self.client_completion_queue.get(_FUTURE)
            self.assertIsNotNone(read_accepted)
            self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
            self.assertEqual(read_tag, read_accepted.tag)
            self.assertIsNotNone(read_accepted.bytes)
            client_data.append(read_accepted.bytes)

        client_call.complete(complete_tag)
        complete_accepted = self.client_completion_queue.get(_FUTURE)
        self.assertIsNotNone(complete_accepted)
        self.assertIs(complete_accepted.kind,
                      _low.Event.Kind.COMPLETE_ACCEPTED)
        self.assertIs(complete_accepted.tag, complete_tag)
        self.assertIs(complete_accepted.complete_accepted, True)

        server_call.read(read_tag)
        read_accepted = self.server_completion_queue.get(_FUTURE)
        self.assertIsNotNone(read_accepted)
        self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
        self.assertEqual(read_tag, read_accepted.tag)
        self.assertIsNone(read_accepted.bytes)

        server_call.status(_low.Status(_low.Code.OK, details), status_tag)
        server_terminal_event_one = self.server_completion_queue.get(_FUTURE)
        server_terminal_event_two = self.server_completion_queue.get(_FUTURE)
        if server_terminal_event_one.kind == _low.Event.Kind.COMPLETE_ACCEPTED:
            status_accepted = server_terminal_event_one
            rpc_accepted = server_terminal_event_two
        else:
            status_accepted = server_terminal_event_two
            rpc_accepted = server_terminal_event_one
        self.assertIsNotNone(status_accepted)
        self.assertIsNotNone(rpc_accepted)
        self.assertEqual(_low.Event.Kind.COMPLETE_ACCEPTED,
                         status_accepted.kind)
        self.assertEqual(status_tag, status_accepted.tag)
        self.assertTrue(status_accepted.complete_accepted)
        self.assertEqual(_low.Event.Kind.FINISH, rpc_accepted.kind)
        self.assertEqual(finish_tag, rpc_accepted.tag)
        self.assertEqual(_low.Status(_low.Code.OK, ''), rpc_accepted.status)

        client_call.read(read_tag)
        client_terminal_event_one = self.client_completion_queue.get(_FUTURE)
        client_terminal_event_two = self.client_completion_queue.get(_FUTURE)
        if client_terminal_event_one.kind == _low.Event.Kind.READ_ACCEPTED:
            read_accepted = client_terminal_event_one
            finish_accepted = client_terminal_event_two
        else:
            read_accepted = client_terminal_event_two
            finish_accepted = client_terminal_event_one
        self.assertIsNotNone(read_accepted)
        self.assertIsNotNone(finish_accepted)
        self.assertEqual(_low.Event.Kind.READ_ACCEPTED, read_accepted.kind)
        self.assertEqual(read_tag, read_accepted.tag)
        self.assertIsNone(read_accepted.bytes)
        self.assertEqual(_low.Event.Kind.FINISH, finish_accepted.kind)
        self.assertEqual(finish_tag, finish_accepted.tag)
        self.assertEqual(_low.Status(_low.Code.OK, details),
                         finish_accepted.status)

        server_timeout_none_event = self.server_completion_queue.get(0)
        self.assertIsNone(server_timeout_none_event)
        client_timeout_none_event = self.client_completion_queue.get(0)
        self.assertIsNone(client_timeout_none_event)

        self.assertSequenceEqual(test_data, server_data)
        self.assertSequenceEqual(test_data, client_data)