def test_TokenHistory(self): history = TokenHistory(maxSize=5) # Ensure returned identifiers increment for id, token in enumerate(("one", "two", "three", "four", "five"), start=1): self.assertEquals(id, history.add(token)) self.assertEquals(len(history.history), 5) # History size never exceeds maxSize id = history.add("six") self.assertEquals(id, 6) self.assertEquals(len(history.history), 5) self.assertEquals( history.history, [(2, "two"), (3, "three"), (4, "four"), (5, "five"), (6, "six")] ) id = history.add("seven") self.assertEquals(id, 7) self.assertEquals(len(history.history), 5) self.assertEquals( history.history, [(3, "three"), (4, "four"), (5, "five"), (6, "six"), (7, "seven")] ) # Look up non-existent identifier token = history.extractIdentifier(9999) self.assertEquals(token, None) self.assertEquals( history.history, [(3, "three"), (4, "four"), (5, "five"), (6, "six"), (7, "seven")] ) # Look up oldest identifier in history token = history.extractIdentifier(3) self.assertEquals(token, "three") self.assertEquals( history.history, [(4, "four"), (5, "five"), (6, "six"), (7, "seven")] ) # Look up latest identifier in history token = history.extractIdentifier(7) self.assertEquals(token, "seven") self.assertEquals( history.history, [(4, "four"), (5, "five"), (6, "six")] ) # Look up an identifier in the middle token = history.extractIdentifier(5) self.assertEquals(token, "five") self.assertEquals( history.history, [(4, "four"), (6, "six")] )
def test_TokenHistory(self): history = TokenHistory(maxSize=5) # Ensure returned identifiers increment for id, token in enumerate( ("one", "two", "three", "four", "five"), start=1 ): self.assertEquals(id, history.add(token)) self.assertEquals(len(history.history), 5) # History size never exceeds maxSize id = history.add("six") self.assertEquals(id, 6) self.assertEquals(len(history.history), 5) self.assertEquals( history.history, [(2, "two"), (3, "three"), (4, "four"), (5, "five"), (6, "six")] ) id = history.add("seven") self.assertEquals(id, 7) self.assertEquals(len(history.history), 5) self.assertEquals( history.history, [(3, "three"), (4, "four"), (5, "five"), (6, "six"), (7, "seven")] ) # Look up non-existent identifier token = history.extractIdentifier(9999) self.assertEquals(token, None) self.assertEquals( history.history, [(3, "three"), (4, "four"), (5, "five"), (6, "six"), (7, "seven")] ) # Look up oldest identifier in history token = history.extractIdentifier(3) self.assertEquals(token, "three") self.assertEquals( history.history, [(4, "four"), (5, "five"), (6, "six"), (7, "seven")] ) # Look up latest identifier in history token = history.extractIdentifier(7) self.assertEquals(token, "seven") self.assertEquals( history.history, [(4, "four"), (5, "five"), (6, "six")] ) # Look up an identifier in the middle token = history.extractIdentifier(5) self.assertEquals(token, "five") self.assertEquals( history.history, [(4, "four"), (6, "six")] )
def makeConnection(self, transport): self.history = TokenHistory() self.log.debug("ProviderProtocol makeConnection") Protocol.makeConnection(self, transport)