def test_subscribersList(self): """ test_subscribersList Test that a brokerclient's connSubscribers instance variable is set by the subscribers parameter on the constructor """ sublist = [Mock(), Mock(), Mock()] c = KafkaBrokerClient('broker', subscribers=sublist) self.assertEqual(sublist, c.connSubscribers) c4 = Mock() c5 = Mock() c.addSubscriber(c4) c.addSubscriber(c5) addedList = sublist addedList.extend([c4, c5]) self.assertEqual(addedList, c.connSubscribers) rmdList = addedList rmdList.remove(sublist[2]) rmdList.remove(c4) c.delSubscriber(sublist[2]) c.delSubscriber(c4) self.assertEqual(rmdList, c.connSubscribers)
def test_subscribersListCalls(self): """ test_subscribersListCalls Test that a brokerclient's connSubscribers callbacks are called in the proper order, and that all the deferreds of a previous call are resolved before the next round of calls is done. """ reactor = MemoryReactorClock() callList = [] def c1(c, conn, reason): s = 'c1:{0}'.format(conn) if reason is not None: s += ':' + reason callList.append(s) def c2(c, conn, reason): def c2_cb(_, c, conn, reason): callList.append('c2_cb:{0}'.format(conn)) d = Deferred() d.addCallback(c2_cb, c, conn, reason) reactor.callLater(1.0, d.callback, None) s = 'c2:{0}'.format(conn) if reason is not None: s += ':' + reason callList.append(s) return d def c3(c, conn, reason): s = 'c3:{0}'.format(conn) if reason is not None: s += ':' + reason callList.append(s) def c4(c, conn, reason): callList.append('c4:{0}'.format(conn)) def c5(c, conn, reason): callList.append('c5:{0}'.format(conn)) sublist = [c1, c2, c3] c = KafkaBrokerClient('slc', subscribers=sublist, reactor=reactor) # Trigger the call to the 3 subscribers c._notify(True) self.assertEqual(callList, ['c1:True', 'c2:True', 'c3:True']) callList = [] c._notify(False) # Nothing should be called yet, because the c2_cb # callback hasn't been called yet... self.assertEqual(callList, []) # advance the clock to trigger the callback to c2_cb reactor.advance(1.0) self.assertEqual(callList, ['c2_cb:True', 'c1:False', 'c2:False', 'c3:False']) callList = [] reactor.advance(1.0) self.assertEqual(callList, ['c2_cb:False']) callList = [] # Trigger the call to the subscribers c._notify(True, reason='TheReason') c.addSubscriber(c4) self.assertEqual(callList, ['c1:True:TheReason', 'c2:True:TheReason', 'c3:True:TheReason']) callList = [] c._notify(False) self.assertEqual(callList, []) # Add a subscriber after the notify call, but before the advance # and ensure that the new subscriber isn't notified for the event # which occurred before it was added c.addSubscriber(c5) # advance the clock to trigger the callback to c2_cb reactor.advance(1.0) self.assertEqual(callList, ['c2_cb:True', 'c1:False', 'c2:False', 'c3:False', 'c4:False']) callList = [] c.delSubscriber(c2) # advance the clock to trigger the callback to c2_cb reactor.advance(1.0) # We should still get the c2_cb:False here... self.assertEqual(callList, ['c2_cb:False']) callList = [] c.delSubscriber(c4) # Trigger the call to the subscribers c._notify(True) reactor.advance(1.0) self.assertEqual(callList, ['c1:True', 'c3:True', 'c5:True']) callList = [] c._notify(False) reactor.advance(1.0) self.assertEqual(callList, ['c1:False', 'c3:False', 'c5:False']) callList = []
def test_subscribersListCalls(self): """ test_subscribersListCalls Test that a brokerclient's connSubscribers callbacks are called in the proper order, and that all the deferreds of a previous call are resolved before the next round of calls is done. """ reactor = MemoryReactorClock() callList = [] def c1(c, conn, reason): s = 'c1:{0}'.format(conn) if reason is not None: s += ':' + reason callList.append(s) def c2(c, conn, reason): def c2_cb(_, c, conn, reason): callList.append('c2_cb:{0}'.format(conn)) d = Deferred() d.addCallback(c2_cb, c, conn, reason) reactor.callLater(1.0, d.callback, None) s = 'c2:{0}'.format(conn) if reason is not None: s += ':' + reason callList.append(s) return d def c3(c, conn, reason): s = 'c3:{0}'.format(conn) if reason is not None: s += ':' + reason callList.append(s) def c4(c, conn, reason): callList.append('c4:{0}'.format(conn)) def c5(c, conn, reason): callList.append('c5:{0}'.format(conn)) sublist = [c1, c2, c3] c = KafkaBrokerClient('slc', subscribers=sublist, reactor=reactor) # Trigger the call to the 3 subscribers c._notify(True) self.assertEqual(callList, ['c1:True', 'c2:True', 'c3:True']) callList = [] c._notify(False) # Nothing should be called yet, because the c2_cb # callback hasn't been called yet... self.assertEqual(callList, []) # advance the clock to trigger the callback to c2_cb reactor.advance(1.0) self.assertEqual(callList, ['c2_cb:True', 'c1:False', 'c2:False', 'c3:False']) callList = [] reactor.advance(1.0) self.assertEqual(callList, ['c2_cb:False']) callList = [] # Trigger the call to the subscribers c._notify(True, reason='TheReason') c.addSubscriber(c4) self.assertEqual( callList, ['c1:True:TheReason', 'c2:True:TheReason', 'c3:True:TheReason']) callList = [] c._notify(False) self.assertEqual(callList, []) # Add a subscriber after the notify call, but before the advance # and ensure that the new subscriber isn't notified for the event # which occurred before it was added c.addSubscriber(c5) # advance the clock to trigger the callback to c2_cb reactor.advance(1.0) self.assertEqual( callList, ['c2_cb:True', 'c1:False', 'c2:False', 'c3:False', 'c4:False']) callList = [] c.delSubscriber(c2) # advance the clock to trigger the callback to c2_cb reactor.advance(1.0) # We should still get the c2_cb:False here... self.assertEqual(callList, ['c2_cb:False']) callList = [] c.delSubscriber(c4) # Trigger the call to the subscribers c._notify(True) reactor.advance(1.0) self.assertEqual(callList, ['c1:True', 'c3:True', 'c5:True']) callList = [] c._notify(False) reactor.advance(1.0) self.assertEqual(callList, ['c1:False', 'c3:False', 'c5:False']) callList = []