Esempio n. 1
0
 def setUp(self):
     self.port = InPort("port", Mock())
     self.peer_port = OutPort("peer_port", Mock())
     self.tunnel = Mock()
     self.scheduler = Mock()
     self.node_id = 123
     self.peer_node_id = 456
     self.tunnel_in = TunnelInEndpoint(self.port, self.tunnel,
                                       self.peer_node_id, self.peer_port.id,
                                       {}, self.scheduler)
     self.tunnel_out = TunnelOutEndpoint(self.peer_port, self.tunnel,
                                         self.node_id, self.port.id, {},
                                         self.scheduler)
     self.port.set_queue(
         queue.fanout_fifo.FanoutFIFO({
             'queue_length': 4,
             'direction': "in"
         }, {}))
     self.port.attach_endpoint(self.tunnel_in)
     self.peer_port.set_queue(
         queue.fanout_fifo.FanoutFIFO(
             {
                 'queue_length': 4,
                 'direction': "out"
             }, {}))
     self.peer_port.attach_endpoint(self.tunnel_out)
Esempio n. 2
0
 def setUp(self):
     self.port = InPort("port", Mock())
     self.peer_port = OutPort("peer_port", Mock())
     self.tunnel = Mock()
     self.trigger_loop = Mock()
     self.node_id = 123
     self.peer_node_id = 456
     self.tunnel_in = TunnelInEndpoint(self.port, self.tunnel,
                                       self.peer_node_id, self.peer_port.id,
                                       self.trigger_loop)
     self.tunnel_out = TunnelOutEndpoint(self.peer_port, self.tunnel,
                                         self.node_id, self.port.id,
                                         self.trigger_loop)
     self.port.set_queue(queue.fanout_fifo.FanoutFIFO(5))
     self.port.attach_endpoint(self.tunnel_in)
     self.peer_port.set_queue(queue.fanout_fifo.FanoutFIFO(5))
     self.peer_port.attach_endpoint(self.tunnel_out)
Esempio n. 3
0
 def setUp(self):
     self.port = InPort("port", Mock())
     self.peer_port = OutPort("peer_port", Mock())
     self.tunnel = Mock()
     self.trigger_loop = Mock()
     self.node_id = 123
     self.peer_node_id = 456
     self.tunnel_in = TunnelInEndpoint(self.port, self.tunnel, self.peer_node_id, self.peer_port.id, self.trigger_loop)
     self.tunnel_out = TunnelOutEndpoint(self.peer_port, self.tunnel, self.node_id, self.port.id, self.trigger_loop)
     self.port.set_queue(queue.fanout_fifo.FanoutFIFO(5))
     self.port.attach_endpoint(self.tunnel_in)
     self.peer_port.set_queue(queue.fanout_fifo.FanoutFIFO(5))
     self.peer_port.attach_endpoint(self.tunnel_out)
 def setUp(self):
     self.port = InPort("port", Mock())
     self.peer_port = OutPort("peer_port", Mock())
     self.tunnel = Mock()
     self.scheduler = Mock()
     self.node_id = 123
     self.peer_node_id = 456
     self.tunnel_in = TunnelInEndpoint(self.port, self.tunnel, self.peer_node_id, self.peer_port.id, {}, self.scheduler)
     self.tunnel_out = TunnelOutEndpoint(self.peer_port, self.tunnel, self.node_id, self.port.id, {}, self.scheduler)
     self.port.set_queue(queue.fanout_fifo.FanoutFIFO({'queue_length': 4, 'direction': "in"}, {}))
     self.port.attach_endpoint(self.tunnel_in)
     self.peer_port.set_queue(queue.fanout_fifo.FanoutFIFO({'queue_length': 4, 'direction': "out"}, {}))
     self.peer_port.attach_endpoint(self.tunnel_out)
Esempio n. 5
0
class TestTunnelEndpoint(unittest.TestCase):

    def setUp(self):
        self.port = InPort("port", Mock())
        self.peer_port = OutPort("peer_port", Mock())
        self.tunnel = Mock()
        self.trigger_loop = Mock()
        self.node_id = 123
        self.peer_node_id = 456
        self.tunnel_in = TunnelInEndpoint(self.port, self.tunnel, self.peer_node_id, self.peer_port.id, self.trigger_loop)
        self.tunnel_out = TunnelOutEndpoint(self.peer_port, self.tunnel, self.node_id, self.port.id, self.trigger_loop)
        self.port.set_queue(queue.fanout_fifo.FanoutFIFO(5))
        self.port.attach_endpoint(self.tunnel_in)
        self.peer_port.set_queue(queue.fanout_fifo.FanoutFIFO(5))
        self.peer_port.attach_endpoint(self.tunnel_out)

    def test_recv_token(self):
        expected_reply = {
            'cmd': 'TOKEN_REPLY',
            'port_id': self.port.id,
            'peer_port_id': self.peer_port.id,
            'sequencenbr': 0,
            'value': 'ACK'
        }
        payload = {
            'port_id': self.port.id,
            'peer_port_id': self.peer_port.id,
            'sequencenbr': 0,
            'token': {'type': 'Token', 'data': 5}
        }
        self.tunnel_in.recv_token(payload)
        assert self.trigger_loop.called
        assert self.port.queue.fifo[0].value == 5
        self.tunnel.send.assert_called_with(expected_reply)

        self.trigger_loop.reset_mock()
        self.tunnel.send.reset_mock()

        payload['sequencenbr'] = 100
        self.tunnel_in.recv_token(payload)
        assert not self.trigger_loop.called
        expected_reply['sequencenbr'] = 100
        expected_reply['value'] = 'NACK'
        self.tunnel.send.assert_called_with(expected_reply)

        self.trigger_loop.reset_mock()
        self.tunnel.send.reset_mock()

        payload['sequencenbr'] = 0
        self.tunnel_in.recv_token(payload)
        assert not self.trigger_loop.called
        expected_reply['sequencenbr'] = 0
        expected_reply['value'] = 'ACK'
        self.tunnel.send.assert_called_with(expected_reply)

    def test_get_peer(self):
        assert self.tunnel_in.get_peer() == (self.peer_node_id, self.peer_port.id)
        assert self.tunnel_out.get_peer() == (self.node_id, self.port.id)

    def test_reply(self):
        self.tunnel_out.port.queue.com_commit = Mock()
        self.tunnel_out.port.queue.com_cancel = Mock()
        self.tunnel.send = Mock()

        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out._send_one_token()
        nbr = self.tunnel.send.call_args_list[-1][0][0]['sequencenbr']

        self.tunnel_out.reply(0, 'ACK')
        self.tunnel_out.port.queue.com_commit.assert_called_with(self.port.id, nbr)
        assert self.trigger_loop.called

        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out._send_one_token()
        nbr = self.tunnel.send.call_args_list[-1][0][0]['sequencenbr']

        self.tunnel_out.reply(nbr, 'NACK')
        assert self.tunnel_out.port.queue.com_cancel.called

    def test_nack_reply(self):
        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out._send_one_token()

        self.tunnel_out.port.queue.commit(self.port.id)
        assert self.tunnel_out.port.queue.tentative_read_pos[self.port.id] == 1
        assert self.tunnel_out.port.queue.read_pos[self.port.id] == 1

        self.tunnel_out.port.write_token(Token(2))
        self.tunnel_out.port.write_token(Token(3))
        self.tunnel_out._send_one_token()
        self.tunnel_out._send_one_token()

        assert self.tunnel_out.port.queue.read_pos[self.port.id] == 1
        assert self.tunnel_out.port.queue.tentative_read_pos[self.port.id] == 3

        self.tunnel_out.reply(1, 'NACK')
        assert self.tunnel_out.port.queue.tentative_read_pos[self.port.id] == 1
        assert self.tunnel_out.port.queue.read_pos[self.port.id] == 1

    def test_bulk_communicate(self):
        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out.port.write_token(Token(2))
        self.tunnel_out.bulk = True
        self.tunnel_out.communicate()
        assert self.tunnel.send.call_count == 2

    def test_communicate(self):
        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out.port.write_token(Token(2))

        self.tunnel_out.bulk = False

        assert self.tunnel_out.communicate() is True
        assert self.tunnel.send.call_count == 1

        assert self.tunnel_out.communicate() is False

        self.tunnel_out.reply(1, 'ACK')
        assert self.tunnel_out.communicate() is True
        assert self.tunnel.send.call_count == 2
Esempio n. 6
0
class TestTunnelEndpoint(unittest.TestCase):
    def setUp(self):
        self.port = InPort("port", Mock())
        self.peer_port = OutPort("peer_port", Mock())
        self.tunnel = Mock()
        self.scheduler = Mock()
        self.node_id = 123
        self.peer_node_id = 456
        self.tunnel_in = TunnelInEndpoint(self.port, self.tunnel,
                                          self.peer_node_id, self.peer_port.id,
                                          {}, self.scheduler)
        self.tunnel_out = TunnelOutEndpoint(self.peer_port, self.tunnel,
                                            self.node_id, self.port.id, {},
                                            self.scheduler)
        self.port.set_queue(
            queue.fanout_fifo.FanoutFIFO({
                'queue_length': 4,
                'direction': "in"
            }, {}))
        self.port.attach_endpoint(self.tunnel_in)
        self.peer_port.set_queue(
            queue.fanout_fifo.FanoutFIFO(
                {
                    'queue_length': 4,
                    'direction': "out"
                }, {}))
        self.peer_port.attach_endpoint(self.tunnel_out)

    def test_recv_token(self):
        expected_reply = {
            'cmd': 'TOKEN_REPLY',
            'port_id': self.port.id,
            'peer_port_id': self.peer_port.id,
            'sequencenbr': 0,
            'value': 'ACK'
        }
        payload = {
            'port_id': self.port.id,
            'peer_port_id': self.peer_port.id,
            'sequencenbr': 0,
            'token': {
                'type': 'Token',
                'data': 5
            }
        }
        self.tunnel_in.recv_token(payload)
        assert self.scheduler.tunnel_rx.called
        assert self.port.queue.fifo[0].value == 5
        self.tunnel.send.assert_called_with(expected_reply)

        self.scheduler.reset_mock()
        self.tunnel.send.reset_mock()

        payload['sequencenbr'] = 100
        self.tunnel_in.recv_token(payload)
        assert not self.scheduler.tunnel_rx.called
        expected_reply['sequencenbr'] = 100
        expected_reply['value'] = 'NACK'
        self.tunnel.send.assert_called_with(expected_reply)

        self.scheduler.reset_mock()
        self.tunnel.send.reset_mock()

        payload['sequencenbr'] = 0
        self.tunnel_in.recv_token(payload)
        assert not self.scheduler.called
        expected_reply['sequencenbr'] = 0
        expected_reply['value'] = 'ACK'
        self.tunnel.send.assert_called_with(expected_reply)

    def test_get_peer(self):
        assert self.tunnel_in.get_peer() == (self.peer_node_id,
                                             self.peer_port.id)
        assert self.tunnel_out.get_peer() == (self.node_id, self.port.id)

    def test_reply(self):
        self.tunnel_out.port.queue.com_commit = Mock()
        self.tunnel_out.port.queue.com_cancel = Mock()
        self.tunnel.send = Mock()

        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out._send_one_token()
        nbr = self.tunnel.send.call_args_list[-1][0][0]['sequencenbr']

        self.tunnel_out.reply(0, 'ACK')
        self.tunnel_out.port.queue.com_commit.assert_called_with(
            self.port.id, nbr)
        assert self.scheduler.tunnel_tx_ack.called

        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out._send_one_token()
        nbr = self.tunnel.send.call_args_list[-1][0][0]['sequencenbr']

        self.tunnel_out.reply(nbr, 'NACK')
        assert self.tunnel_out.port.queue.com_cancel.called
        assert self.scheduler.tunnel_tx_nack.called

    def test_nack_reply(self):
        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out._send_one_token()

        self.tunnel_out.port.queue.commit(self.port.id)
        assert self.tunnel_out.port.queue.tentative_read_pos[self.port.id] == 1
        assert self.tunnel_out.port.queue.read_pos[self.port.id] == 1

        self.tunnel_out.port.write_token(Token(2))
        self.tunnel_out.port.write_token(Token(3))
        self.tunnel_out._send_one_token()
        self.tunnel_out._send_one_token()

        assert self.tunnel_out.port.queue.read_pos[self.port.id] == 1
        assert self.tunnel_out.port.queue.tentative_read_pos[self.port.id] == 3

        self.tunnel_out.reply(1, 'NACK')
        assert self.tunnel_out.port.queue.tentative_read_pos[self.port.id] == 1
        assert self.tunnel_out.port.queue.read_pos[self.port.id] == 1

    def test_bulk_communicate(self):
        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out.port.write_token(Token(2))
        self.tunnel_out.bulk = True
        self.tunnel_out.communicate()
        assert self.tunnel.send.call_count == 2

    def test_communicate(self):
        self.tunnel_out.port.write_token(Token(1))
        self.tunnel_out.port.write_token(Token(2))

        self.tunnel_out.bulk = False

        assert self.tunnel_out.communicate() is True
        assert self.tunnel.send.call_count == 1

        assert self.tunnel_out.communicate() is False

        self.tunnel_out.reply(1, 'ACK')
        assert self.tunnel_out.communicate() is True
        assert self.tunnel.send.call_count == 2