def setUp(self): self._node = NodeB()
def setUp(self): self._node = NodeB()
class TestNodeB(PyonTestCase): def setUp(self): self._node = NodeB() def test_start_node(self): self._node.start_node() self.assertEquals(self._node.running, 1) self.assertTrue(self._node.ready.is_set()) def test_on_channel_request_close_not_in_map(self): chm = Mock(spec=BaseChannel) # not in the pool, don't go to school with patch('pyon.net.messaging.log'): self.assertRaises(AssertionError, self._node.on_channel_request_close, chm) def test_on_channel_request_close_in_map(self): chm = Mock(spec=BidirClientChannel) chm._queue_auto_delete = False ourchid = self._node._pool.get_id() chm.get_channel_id.return_value = 5 # amqp's channel number differs from ours # setup pool/map self._node._bidir_pool[ourchid] = chm self._node._pool_map[5] = ourchid # make the call self._node.on_channel_request_close(chm) # check what happened self.assertFalse(chm.close_impl.called) self.assertEquals(self._node._pool_map, {}) self.assertEquals( self._node._pool.get_id(), ourchid ) # should get the same number back from the pool now, ensures we really freed it self.assertIn(ourchid, self._node._bidir_pool) self.assertEquals(self._node._bidir_pool[ourchid], chm) def test_on_channel_request_close_in_map_and_auto_delete_sanity_check( self): chm = Mock(spec=BidirClientChannel) chm._queue_auto_delete = True ourchid = self._node._pool.get_id() chm.get_channel_id.return_value = 5 # amqp's channel number differs from ours # setup pool/map self._node._bidir_pool[ourchid] = chm self._node._pool_map[5] = ourchid # make the call self._node.on_channel_request_close(chm) # retest same things as above because it will be removed from existence in the maps self.assertFalse(chm.close_impl.called) self.assertEquals(self._node._pool_map, {}) # the differing items now: self.assertNotEquals( self._node._pool.get_id(), ourchid ) # should get a new number back from the pool as we killed the last pooled number self.assertNotIn(ourchid, self._node._bidir_pool) @patch('pyon.net.messaging.blocking_cb') @patch('pyon.net.transport.AsyncResult') def test__new_channel(self, armock, bcbmock): self._node.client = Mock() ch = self._node._new_channel(BaseChannel) self.assertIsInstance(ch, BaseChannel) self.assertEquals(ch._transport._client, bcbmock()) @patch('pyon.net.messaging.NodeB._new_channel', return_value=sentinel.new_chan) def test_channel_nonpooled(self, ncmock): self._node.client = Mock(spec=Connection) ch = self._node.channel(BaseChannel) ncmock.assert_called_once_with(BaseChannel, transport=None) self.assertEquals(ch, sentinel.new_chan) def test_channel_pool(self): ncm = Mock() ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch = self._node.channel(BidirClientChannel) # should expect to see this show up in the node's mappings self.assertIn(ch, self._node._bidir_pool.itervalues()) self.assertIn(sentinel.chid, self._node._pool_map) self.assertEquals(len(self._node._pool_map), 1) self.assertEquals(len(self._node._pool_map), len(self._node._bidir_pool)) # let's grab another one to watch our pool grow # return value is not a mock factory - it returns the same mock instance as we declared above # so redeclare it so they get unique chids ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid2 with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch2 = self._node.channel(BidirClientChannel) self.assertEquals(ch.get_channel_id(), sentinel.chid) self.assertEquals(ch2.get_channel_id(), sentinel.chid2) self.assertNotEqual(ch, ch2) self.assertIn(ch2, self._node._bidir_pool.itervalues()) self.assertIn(sentinel.chid2, self._node._pool_map) self.assertEquals(len(self._node._pool_map), 2) self.assertEquals(len(self._node._pool_map), len(self._node._bidir_pool)) def test_channel_pool_release(self): ncm = Mock() ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch = self._node.channel(BidirClientChannel) # return value is not a mock factory - it returns the same mock instance as we declared above # so redeclare it so they get unique chids ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid2 with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch2 = self._node.channel(BidirClientChannel) self.assertEquals(ch.get_channel_id(), sentinel.chid) self.assertEquals(ch2.get_channel_id(), sentinel.chid2) # return ch to the pool with patch('pyon.net.messaging.log'): self._node.on_channel_request_close(ch) # expect to have bidir pool of two, pool map of 1 self.assertEquals(len(self._node._bidir_pool), 2) self.assertEquals(len(self._node._pool_map), 1) # ch2 still active so it should be in the pool map self.assertIn(sentinel.chid2, self._node._pool_map) @patch('pyon.net.messaging.blocking_cb', return_value=sentinel.amq_chan) def test_channel_pool_release_reacquire(self, bcbmock): ncm = Mock() ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid # mock out health check cpchmock = Mock(return_value=True) self._node._check_pooled_channel_health = cpchmock with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch = self._node.channel(BidirClientChannel) # return value is not a mock factory - it returns the same mock instance as we declared above # so redeclare it so they get unique chids ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid2 with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch2 = self._node.channel(BidirClientChannel) self.assertEquals(ch.get_channel_id(), sentinel.chid) self.assertEquals(ch2.get_channel_id(), sentinel.chid2) # return ch to the pool with patch('pyon.net.messaging.log'): self._node.on_channel_request_close(ch) # reacquire ch call_count = ncm.call_count with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch3 = self._node.channel(BidirClientChannel) # no new calls to the create method have been made self.assertEquals(ncm.call_count, call_count) # we got the first mocked channel back self.assertEquals(ch3.get_channel_id(), sentinel.chid) def test_stop_node(self): self._node.client = Mock() self._node._destroy_pool = Mock() self._node.running = True self._node.stop_node() self._node.client.close.assert_called_once_with() self._node._destroy_pool.assert_called_once_with() self.assertFalse(self._node.running) def test_stop_node_not_running(self): self._node.client = Mock() self._node._destroy_pool = Mock() self._node.running = False self._node.stop_node() self.assertFalse(self._node.client.close.called) self.assertFalse(self._node._destroy_pool.called) self.assertFalse(self._node.running) def test_destroy_pool(self): chmock = Mock(spec=RecvChannel) for x in xrange(20): self._node._bidir_pool[x] = chmock self._node._destroy_pool() self.assertEqual(chmock._destroy_queue.call_count, 20) @patch('pyon.net.messaging.blocking_cb') @patch('pyon.net.transport.AsyncResult') def test__new_transport(self, armock, bcbmock): self._node.client = Mock() transport = self._node._new_transport() bcbmock.assert_called_once_with(self._node.client.channel, 'on_open_callback', channel_number=None) @patch('pyon.net.messaging.blocking_cb', return_value=None) @patch('pyon.container.cc.Container.instance') def test__new_transport_fails(self, containermock, bcbmock): self._node.client = Mock() self.assertRaises(StandardError, self._node._new_transport) containermock.fail_fast.assert_called_once_with( "AMQCHAN IS NONE, messaging has failed", True)
class TestNodeB(PyonTestCase): def setUp(self): self._node = NodeB() def test_start_node(self): self._node.start_node() self.assertEquals(self._node.running, 1) self.assertTrue(self._node.ready.is_set()) def test_on_channel_request_close_not_in_map(self): chm = Mock(spec=BaseChannel) # not in the pool, don't go to school with patch('pyon.net.messaging.log'): self.assertRaises(AssertionError, self._node.on_channel_request_close, chm) def test_on_channel_request_close_in_map(self): chm = Mock(spec=BidirClientChannel) chm._queue_auto_delete = False ourchid = self._node._pool.get_id() chm.get_channel_id.return_value = 5 # amqp's channel number differs from ours # setup pool/map self._node._bidir_pool[ourchid] = chm self._node._pool_map[5] = ourchid # make the call self._node.on_channel_request_close(chm) # check what happened self.assertFalse(chm.close_impl.called) self.assertEquals(self._node._pool_map, {}) self.assertEquals(self._node._pool.get_id(), ourchid) # should get the same number back from the pool now, ensures we really freed it self.assertIn(ourchid, self._node._bidir_pool) self.assertEquals(self._node._bidir_pool[ourchid], chm) def test_on_channel_request_close_in_map_and_auto_delete_sanity_check(self): chm = Mock(spec=BidirClientChannel) chm._queue_auto_delete = True ourchid = self._node._pool.get_id() chm.get_channel_id.return_value = 5 # amqp's channel number differs from ours # setup pool/map self._node._bidir_pool[ourchid] = chm self._node._pool_map[5] = ourchid # make the call self._node.on_channel_request_close(chm) # retest same things as above because it will be removed from existence in the maps self.assertFalse(chm.close_impl.called) self.assertEquals(self._node._pool_map, {}) # the differing items now: self.assertNotEquals(self._node._pool.get_id(), ourchid) # should get a new number back from the pool as we killed the last pooled number self.assertNotIn(ourchid, self._node._bidir_pool) @patch('pyon.net.messaging.blocking_cb') def test__new_channel(self, bcbmock): self._node.client = Mock() ch = self._node._new_channel(BaseChannel) self.assertIsInstance(ch, BaseChannel) self.assertEquals(ch._transport._client, bcbmock()) @patch('pyon.net.messaging.NodeB._new_channel', return_value=sentinel.new_chan) def test_channel_nonpooled(self, ncmock): self._node.client = Mock(spec=Connection) ch = self._node.channel(BaseChannel) ncmock.assert_called_once_with(BaseChannel, transport=None) self.assertEquals(ch, sentinel.new_chan) def test_channel_pool(self): ncm = Mock() ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch = self._node.channel(BidirClientChannel) # should expect to see this show up in the node's mappings self.assertIn(ch, self._node._bidir_pool.itervalues()) self.assertIn(sentinel.chid, self._node._pool_map) self.assertEquals(len(self._node._pool_map), 1) self.assertEquals(len(self._node._pool_map), len(self._node._bidir_pool)) # let's grab another one to watch our pool grow # return value is not a mock factory - it returns the same mock instance as we declared above # so redeclare it so they get unique chids ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid2 with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch2 = self._node.channel(BidirClientChannel) self.assertEquals(ch.get_channel_id(), sentinel.chid) self.assertEquals(ch2.get_channel_id(), sentinel.chid2) self.assertNotEqual(ch, ch2) self.assertIn(ch2, self._node._bidir_pool.itervalues()) self.assertIn(sentinel.chid2, self._node._pool_map) self.assertEquals(len(self._node._pool_map), 2) self.assertEquals(len(self._node._pool_map), len(self._node._bidir_pool)) def test_channel_pool_release(self): ncm = Mock() ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch = self._node.channel(BidirClientChannel) # return value is not a mock factory - it returns the same mock instance as we declared above # so redeclare it so they get unique chids ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid2 with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch2 = self._node.channel(BidirClientChannel) self.assertEquals(ch.get_channel_id(), sentinel.chid) self.assertEquals(ch2.get_channel_id(), sentinel.chid2) # return ch to the pool with patch('pyon.net.messaging.log'): self._node.on_channel_request_close(ch) # expect to have bidir pool of two, pool map of 1 self.assertEquals(len(self._node._bidir_pool), 2) self.assertEquals(len(self._node._pool_map), 1) # ch2 still active so it should be in the pool map self.assertIn(sentinel.chid2, self._node._pool_map) @patch('pyon.net.messaging.blocking_cb', return_value=sentinel.amq_chan) def test_channel_pool_release_reacquire(self, bcbmock): ncm = Mock() ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid # mock out health check cpchmock = Mock(return_value=True) self._node._check_pooled_channel_health = cpchmock with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch = self._node.channel(BidirClientChannel) # return value is not a mock factory - it returns the same mock instance as we declared above # so redeclare it so they get unique chids ncm.return_value = Mock(spec=BidirClientChannel) ncm.return_value._queue_auto_delete = False ncm.return_value.get_channel_id.return_value = sentinel.chid2 with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch2 = self._node.channel(BidirClientChannel) self.assertEquals(ch.get_channel_id(), sentinel.chid) self.assertEquals(ch2.get_channel_id(), sentinel.chid2) # return ch to the pool with patch('pyon.net.messaging.log'): self._node.on_channel_request_close(ch) # reacquire ch call_count = ncm.call_count with patch('pyon.net.messaging.NodeB._new_channel', ncm): ch3 = self._node.channel(BidirClientChannel) # no new calls to the create method have been made self.assertEquals(ncm.call_count, call_count) # we got the first mocked channel back self.assertEquals(ch3.get_channel_id(), sentinel.chid) def test_stop_node(self): self._node.client = Mock() self._node._destroy_pool = Mock() self._node.running = True self._node.stop_node() self._node.client.close.assert_called_once_with() self._node._destroy_pool.assert_called_once_with() self.assertFalse(self._node.running) def test_stop_node_not_running(self): self._node.client = Mock() self._node._destroy_pool = Mock() self._node.running = False self._node.stop_node() self.assertFalse(self._node.client.close.called) self.assertFalse(self._node._destroy_pool.called) self.assertFalse(self._node.running) def test_destroy_pool(self): chmock = Mock(spec=RecvChannel) for x in xrange(20): self._node._bidir_pool[x] = chmock self._node._destroy_pool() self.assertEqual(chmock._destroy_queue.call_count, 20) @patch('pyon.net.messaging.blocking_cb') def test__new_transport(self, bcbmock): self._node.client = Mock() transport = self._node._new_transport() bcbmock.assert_called_once_with(self._node.client.channel, 'on_open_callback', channel_number=None) @patch('pyon.net.messaging.traceback', Mock()) @patch('pyon.net.messaging.blocking_cb', return_value=None) @patch('pyon.container.cc.Container.instance') def test__new_transport_fails(self, containermock, bcbmock): self._node.client = Mock() self.assertRaises(StandardError, self._node._new_transport) containermock.fail_fast.assert_called_once_with("AMQCHAN IS NONE, messaging has failed", True)