Exemplo n.º 1
0
class TestListenChannel(PyonTestCase):
    def setUp(self):
        self.ch = ListenChannel()

    def test__create_accepted_channel(self):
        newch = self.ch._create_accepted_channel(sentinel.amq_chan,
                                                 sentinel.msg)
        self.assertIsInstance(newch, ListenChannel.AcceptedListenChannel)
        self.assertEquals(newch._amq_chan, sentinel.amq_chan)

    def test_accept(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()

        self.ch.recv = rmock
        self.ch._create_accepted_channel = cacmock
        self.ch._amq_chan = sentinel.amq_chan
        self.ch._fsm.current_state = self.ch.S_CONSUMING

        with self.ch.accept() as retch:
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)
            cacmock.assert_called_once_with(sentinel.amq_chan, sentinel.msg)
            retch._recv_queue.put.assert_called_once_with(sentinel.msg)

        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CONSUMING)

    def test_close_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()

        self.ch.recv = rmock
        self.ch._create_accepted_channel = cacmock
        self.ch._amq_chan = sentinel.amq_chan
        self.ch._fsm.current_state = self.ch.S_CONSUMING

        # to test close to make sure nothing happened
        self.ch.close_impl = Mock()

        # stub out stop consume reaction
        self.ch._on_stop_consume = Mock()

        with self.ch.accept() as retch:
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

            self.ch.close()

            # ensure nothing close-like got called!
            self.assertFalse(self.ch.close_impl.called)
            self.assertFalse(self.ch._on_stop_consume.called)
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSING)

        self.assertTrue(self.ch.close_impl.called)
        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSED)

    def test_stop_consume_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()

        self.ch.recv = rmock
        self.ch._create_accepted_channel = cacmock
        self.ch._amq_chan = sentinel.amq_chan
        self.ch._fsm.current_state = self.ch.S_CONSUMING

        # to test stop_consume reaction
        self.ch._on_stop_consume = Mock()

        with self.ch.accept() as retch:
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

            self.ch.stop_consume()

            # we didn't stop consume yet
            self.assertFalse(self.ch._on_stop_consume.called)
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_STOPPING)

        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACTIVE)

    def test_AcceptedListenChannel_close_does_not_close_underlying_amqp_channel(
            self):
        ac = Mock(pchannel.Channel)
        newch = self.ch._create_accepted_channel(ac, sentinel.msg)

        newch.close()
        self.assertEquals(ac.close.call_count, 0)
Exemplo n.º 2
0
class TestListenChannel(PyonTestCase):
    def setUp(self):
        self.ch = ListenChannel()

    def test__create_accepted_channel(self):
        newch = self.ch._create_accepted_channel(sentinel.transport,
                                                 sentinel.msg)
        self.assertIsInstance(newch, ListenChannel.AcceptedListenChannel)
        self.assertEquals(newch._transport, sentinel.transport)

    def test_accept(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()
        transport = Mock()

        self.ch.recv = rmock
        self.ch._recv_queue.await_n = MagicMock()
        self.ch._create_accepted_channel = cacmock
        self.ch.on_channel_open(transport)
        self.ch._fsm.current_state = self.ch.S_ACTIVE
        self.ch._consuming = True

        retch = self.ch.accept()
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)
        cacmock.assert_called_once_with(transport, [sentinel.msg])
        retch._recv_queue.put.assert_called_once_with(sentinel.msg)

        # we've mocked all the working machinery of accept's return etc, so we must manually exit accept
        # as if we've ack'd/reject'd
        self.ch.exit_accept()

        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACTIVE)
        self.assertTrue(self.ch._consuming)

    def test_close_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()
        transport = Mock()

        self.ch.recv = rmock
        self.ch._recv_queue.await_n = MagicMock()
        self.ch._create_accepted_channel = cacmock
        self.ch.on_channel_open(transport)
        self.ch._fsm.current_state = self.ch.S_ACTIVE
        self.ch._consuming = True

        # to test close to make sure nothing happened
        self.ch.close_impl = Mock()

        # stub out stop consume reaction
        self.ch._on_stop_consume = Mock()

        retch = self.ch.accept()
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

        self.ch.close()

        # ensure nothing close-like got called!
        self.assertFalse(self.ch.close_impl.called)
        self.assertFalse(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSING)

        # we've mocked all the working machinery of accept's return etc, so we must manually exit accept
        # as if we've ack'd/reject'd
        self.ch.exit_accept()

        self.assertTrue(self.ch.close_impl.called)
        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSED)
        self.assertFalse(self.ch._consuming)

    def test_stop_consume_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()
        transport = Mock()

        self.ch.recv = rmock
        self.ch._recv_queue.await_n = MagicMock()
        self.ch._create_accepted_channel = cacmock
        self.ch.on_channel_open(transport)
        self.ch._fsm.current_state = self.ch.S_ACTIVE
        self.ch._consuming = True

        # to test stop_consume reaction
        self.ch._on_stop_consume = Mock()

        retch = self.ch.accept()
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

        self.ch.stop_consume()

        # we've stopped consuming, no state transition
        self.assertFalse(self.ch._consuming)
        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

        # we've mocked all the working machinery of accept's return etc, so we must manually exit accept
        # as if we've ack'd/reject'd
        self.ch.exit_accept()

        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACTIVE)

    def test_AcceptedListenChannel_close_does_not_close_underlying_amqp_channel(
            self):
        transport = Mock()
        newch = self.ch._create_accepted_channel(transport, sentinel.msg)

        newch.close()
        self.assertEquals(transport.close.call_count, 0)
Exemplo n.º 3
0
class TestListenChannel(PyonTestCase):

    def setUp(self):
        self.ch = ListenChannel()

    def test__create_accepted_channel(self):
        newch = self.ch._create_accepted_channel(sentinel.transport, sentinel.msg)
        self.assertIsInstance(newch, ListenChannel.AcceptedListenChannel)
        self.assertEquals(newch._transport, sentinel.transport)

    def test_accept(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()
        transport = Mock()

        self.ch.recv = rmock
        self.ch._recv_queue.await_n = MagicMock()
        self.ch._create_accepted_channel = cacmock
        self.ch.on_channel_open(transport)
        self.ch._fsm.current_state = self.ch.S_ACTIVE
        self.ch._consuming = True

        retch = self.ch.accept()
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)
        cacmock.assert_called_once_with(transport, [sentinel.msg])
        retch._recv_queue.put.assert_called_once_with(sentinel.msg)

        # we've mocked all the working machinery of accept's return etc, so we must manually exit accept
        # as if we've ack'd/reject'd
        self.ch.exit_accept()

        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACTIVE)
        self.assertTrue(self.ch._consuming)

    def test_close_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()
        transport = Mock()

        self.ch.recv = rmock
        self.ch._recv_queue.await_n = MagicMock()
        self.ch._create_accepted_channel = cacmock
        self.ch.on_channel_open(transport)
        self.ch._fsm.current_state = self.ch.S_ACTIVE
        self.ch._consuming = True

        # to test close to make sure nothing happened
        self.ch.close_impl = Mock()

        # stub out stop consume reaction
        self.ch._on_stop_consume = Mock()

        retch = self.ch.accept()
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

        self.ch.close()

        # ensure nothing close-like got called!
        self.assertFalse(self.ch.close_impl.called)
        self.assertFalse(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSING)

        # we've mocked all the working machinery of accept's return etc, so we must manually exit accept
        # as if we've ack'd/reject'd
        self.ch.exit_accept()

        self.assertTrue(self.ch.close_impl.called)
        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSED)
        self.assertFalse(self.ch._consuming)

    def test_stop_consume_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()
        transport = Mock()

        self.ch.recv = rmock
        self.ch._recv_queue.await_n = MagicMock()
        self.ch._create_accepted_channel = cacmock
        self.ch.on_channel_open(transport)
        self.ch._fsm.current_state = self.ch.S_ACTIVE
        self.ch._consuming = True

        # to test stop_consume reaction
        self.ch._on_stop_consume = Mock()

        retch = self.ch.accept()
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

        self.ch.stop_consume()

        # we've stopped consuming, no state transition
        self.assertFalse(self.ch._consuming)
        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

        # we've mocked all the working machinery of accept's return etc, so we must manually exit accept
        # as if we've ack'd/reject'd
        self.ch.exit_accept()

        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACTIVE)

    def test_AcceptedListenChannel_close_does_not_close_underlying_amqp_channel(self):
        transport = Mock()
        newch = self.ch._create_accepted_channel(transport, sentinel.msg)

        newch.close()
        self.assertEquals(transport.close.call_count, 0)
Exemplo n.º 4
0
class TestListenChannel(PyonTestCase):

    def setUp(self):
        self.ch = ListenChannel()

    def test__create_accepted_channel(self):
        newch = self.ch._create_accepted_channel(sentinel.amq_chan, sentinel.msg)
        self.assertIsInstance(newch, ListenChannel.AcceptedListenChannel)
        self.assertEquals(newch._amq_chan, sentinel.amq_chan)

    def test_accept(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()

        self.ch.recv = rmock
        self.ch._create_accepted_channel = cacmock
        self.ch._amq_chan = sentinel.amq_chan
        self.ch._fsm.current_state = self.ch.S_CONSUMING

        with self.ch.accept() as retch:
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)
            cacmock.assert_called_once_with(sentinel.amq_chan, sentinel.msg)
            retch._recv_queue.put.assert_called_once_with(sentinel.msg)

        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CONSUMING)

    def test_close_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()

        self.ch.recv = rmock
        self.ch._create_accepted_channel = cacmock
        self.ch._amq_chan = sentinel.amq_chan
        self.ch._fsm.current_state = self.ch.S_CONSUMING

        # to test close to make sure nothing happened
        self.ch.close_impl = Mock()

        # stub out stop consume reaction
        self.ch._on_stop_consume = Mock()

        with self.ch.accept() as retch:
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

            self.ch.close()

            # ensure nothing close-like got called!
            self.assertFalse(self.ch.close_impl.called)
            self.assertFalse(self.ch._on_stop_consume.called)
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSING)

        self.assertTrue(self.ch.close_impl.called)
        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_CLOSED)

    def test_stop_consume_while_accepted(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()

        self.ch.recv = rmock
        self.ch._create_accepted_channel = cacmock
        self.ch._amq_chan = sentinel.amq_chan
        self.ch._fsm.current_state = self.ch.S_CONSUMING

        # to test stop_consume reaction
        self.ch._on_stop_consume = Mock()

        with self.ch.accept() as retch:
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)

            self.ch.stop_consume()

            # we didn't stop consume yet
            self.assertFalse(self.ch._on_stop_consume.called)
            self.assertEquals(self.ch._fsm.current_state, self.ch.S_STOPPING)

        self.assertTrue(self.ch._on_stop_consume.called)
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACTIVE)

    def test_AcceptedListenChannel_close_does_not_close_underlying_amqp_channel(self):
        ac = Mock(pchannel.Channel)
        newch = self.ch._create_accepted_channel(ac, sentinel.msg)

        newch.close()
        self.assertEquals(ac.close.call_count, 0)