コード例 #1
0
    def test_send_to_new_contact_successful_connection(self):
        """
        Send a message to a new contact causes a new connection to be made
        whose associated protocol object is cached for later use.
        """
        nc = NetstringConnector(self.event_loop)
        contact = PeerNode(PUBLIC_KEY, self.version,
                           'netstring://192.168.0.1:1908')
        msg = OK('uuid', 'recipient', 'sender', 9999, 'version', 'seal')
        protocol = mock.MagicMock()
        protocol.send_string = mock.MagicMock()
        sender = Node(PUBLIC_KEY, PRIVATE_KEY, self.event_loop, nc, 1908)

        @asyncio.coroutine
        def faux_connect(protocol=protocol):
            return ('foo', protocol)

        with mock.patch.object(self.event_loop, 'create_connection',
                               return_value=faux_connect()):
            result = nc.send(contact, msg, sender)
            self.event_loop.run_until_complete(result)
            self.assertEqual(1, protocol.send_string.call_count)
            self.assertTrue(result.done())
            self.assertEqual(True, result.result())
            self.assertIn(contact.network_id, nc._connections)
            self.assertEqual(nc._connections[contact.network_id], protocol)
            expected = to_dict(msg)
            actual = json.loads(protocol.send_string.call_args[0][0])
            self.assertEqual(expected, actual)
コード例 #2
0
 def test_to_dict(self):
     """
     Simple good case.
     """
     result = to_dict(self.mock_message)
     self.assertIsInstance(result, dict)
     for k in ['uuid', 'recipient', 'sender', 'reply_port', 'version',
               'seal', 'key', 'value', 'timestamp',
               'expires', 'created_with', 'public_key', 'name',
               'signature', 'message']:
         self.assertIn(k, result.keys())
         self.assertEqual(result[k], getattr(self, k))
コード例 #3
0
    def test_send(self):
        """
        Test the good case. We should end up with a task wrapping an
        appropriate call to aiohttp.request.
        """
        contact = PeerNode(PUBLIC_KEY, self.version, 'http://192.168.0.1:80')
        msg = OK('uuid', 'recipient', 'sender', 9999, 'version', 'seal')
        msg_json = json.dumps(to_dict(msg))
        headers = {'content-type': 'application/json'}
        connector = HttpConnector(self.event_loop)

        @asyncio.coroutine
        def faux_request(*args, **kwargs):
            return 'foo'

        with mock.patch.object(aiohttp, 'request',
                               return_value=faux_request()) as request:
            result = connector.send(contact, msg)
            self.assertIsInstance(result, asyncio.Task)
            request.assert_called_once_with('post', contact.uri, data=msg_json,
                                            headers=headers)
コード例 #4
0
 def test_from_dict_value(self):
     """
     Ensures a valid value message is correctly parsed.
     """
     mock_message = to_dict(self.mock_message)
     result = from_dict(mock_message)
     self.assertIsInstance(result, Value)
     self.assertEqual(result.uuid, self.uuid)
     self.assertEqual(result.recipient, self.node)
     self.assertEqual(result.sender, self.node)
     self.assertEqual(result.reply_port, self.reply_port)
     self.assertEqual(result.version, self.version)
     self.assertEqual(result.seal, self.seal)
     self.assertEqual(result.key, self.key)
     self.assertEqual(result.value, self.value)
     self.assertEqual(result.timestamp, self.timestamp)
     self.assertEqual(result.expires, self.expires)
     self.assertEqual(result.created_with, self.created_with)
     self.assertEqual(result.public_key, self.public_key)
     self.assertEqual(result.name, self.name)
     self.assertEqual(result.signature, self.signature)
コード例 #5
0
    def test_send_with_failing_cached_protocol(self):
        """
        Attempting to send a message to the referenced contact using a
        cached protocol object that cannot send (e.g. perhaps the transport
        was dropped?) causes a retry as if the contact were new.
        """
        nc = NetstringConnector(self.event_loop)
        contact = PeerNode(PUBLIC_KEY, self.version,
                           'netstring://192.168.0.1:1908')
        msg = OK('uuid', 'recipient', 'sender', 9999, 'version', 'seal')
        protocol = mock.MagicMock()

        def side_effect(*args, **kwargs):
            raise ValueError()

        protocol.send_string = mock.MagicMock(side_effect=side_effect)
        nc._connections[contact.network_id] = protocol

        new_protocol = mock.MagicMock()
        new_protocol.send_string = mock.MagicMock()
        sender = Node(PUBLIC_KEY, PRIVATE_KEY, self.event_loop, nc, 1908)

        @asyncio.coroutine
        def faux_connect(protocol=new_protocol):
            return ('foo', protocol)

        with mock.patch.object(self.event_loop, 'create_connection',
                               return_value=faux_connect()):
            result = nc.send(contact, msg, sender)
            self.event_loop.run_until_complete(result)
            self.assertEqual(1, new_protocol.send_string.call_count)
            self.assertTrue(result.done())
            self.assertEqual(True, result.result())
            self.assertIn(contact.network_id, nc._connections)
            self.assertEqual(nc._connections[contact.network_id],
                             new_protocol)
            expected = to_dict(msg)
            actual = json.loads(protocol.send_string.call_args[0][0])
            self.assertEqual(expected, actual)