def test_channel_closed_cb(self): cp = ChannelPool(None) cp._channels = 32 expect(cp._process_queue) cp._channel_closed_cb('channel') assert_equals(31, cp._channels)
def test_get_channel_when_two_free_and_one_closed(self): # Because we can't mock builtins .... class Set(set): def pop(self): pass conn = mock() ch1 = mock() ch1.closed = True ch2 = mock() ch2.closed = False cp = ChannelPool(conn) cp._free_channels = Set([ch1, ch2]) cp._channels = 2 # Because we want them in order expect(cp._free_channels.pop).returns( ch1).side_effect(super(Set, cp._free_channels).pop) expect(cp._free_channels.pop).returns( ch2).side_effect(super(Set, cp._free_channels).pop) self.assertEquals(ch2, cp._get_channel()) self.assertEquals(set(), cp._free_channels) assert_equals(2, cp._channels)
def test_get_channel_returns_new_when_none_free_and_at_limit(self): conn = mock() cp = ChannelPool(conn, 1) cp._channels = 1 stub(conn.channel) self.assertEquals(None, cp._get_channel()) self.assertEquals(set(), cp._free_channels)
def test_get_channel_returns_new_when_none_free_and_not_at_limit(self): conn = mock() cp = ChannelPool(conn) cp._channels = 1 with expect(conn.channel).returns(mock()) as newchannel: expect(newchannel.add_close_listener).args(cp._channel_closed_cb) self.assertEquals(newchannel, cp._get_channel()) self.assertEquals(set(), cp._free_channels) assert_equals(2, cp._channels)
def test_get_channel_when_two_free_and_all_closed(self): conn = mock() ch1 = mock() ch1.closed = True ch2 = mock() ch2.closed = True cp = ChannelPool(conn) cp._free_channels = set([ch1, ch2]) cp._channels = 2 with expect(conn.channel).returns(mock()) as newchannel: expect(newchannel.add_close_listener).args(cp._channel_closed_cb) self.assertEquals(newchannel, cp._get_channel()) self.assertEquals(set(), cp._free_channels) assert_equals(3, cp._channels)