def _make_ss(self): class Serializer(object): loads = int dumps = str return ScheduledSet( redis.Redis(), self.set_name, serializer=Serializer(), )
def setUp(self): self.key = 'scheduled_set_test' self.now = time.time() - 1 # offset to avoid having to sleep self.ss = ScheduledSet(redis.Redis(), self.key)
class ScheduledSetTest(unittest.TestCase): def setUp(self): self.key = 'scheduled_set_test' self.now = time.time() - 1 # offset to avoid having to sleep self.ss = ScheduledSet(redis.Redis(), self.key) def tearDown(self): self.ss.clear() def test_schedule(self): self.ss.add(1, self.now) self.ss.add(2, self.now + 1000) next_item = self.ss.pop() self.assertEquals(next_item, '1') with self.assertRaises(KeyError): self.ss.pop() self.assertEquals(len(self.ss), 1) def test_peek(self): with self.assertRaises(KeyError): self.ss.peek() self.ss.add(1, self.now - 1000) self.ss.add(2, self.now) self.ss.add(3, self.now + 1000) self.assertEquals( self.ss.peek(), '1', ) self.assertEquals( self.ss.peek(position=1), '2', ) with self.assertRaises(KeyError): self.ss.peek(position=2) self.ss.pop() self.ss.pop() with self.assertRaises(KeyError): self.ss.peek() self.assertEquals(len(self.ss), 1) def test_take(self): self.ss.add('1', self.now - 3) self.ss.add('2', self.now - 2) self.ss.add('3', self.now - 1) items = self.ss.take(2) self.assertEquals(len(items), 2) self.assertEquals(['1', '2'], items) self.assertEquals(self.ss.pop(), '3') self.assertEquals( len(self.ss), 0, ) self.assertEquals( self.ss.take(0), [], ) self.assertEquals( self.ss.take(-1), [], ) def test_length(self): for i in range(2): self.ss.add(i, self.now + 50) for i in range(3): self.ss.add(i + 2, self.now - 50) self.assertEquals( len(self.ss), 5, ) def test_length_available(self): for i in range(2): self.ss.add(i, self.now + 50) for i in range(3): self.ss.add(i + 2, self.now - 50) self.assertEquals( self.ss.available(), 3, ) def test_contains(self): for i in range(5): self.ss.add(i) self.assertTrue(0 in self.ss) self.assertFalse(-1 in self.ss) def test_add_dup(self): for i in range(5): self.ss.add(i) dup_added_at = 10 self.ss.add(0, score=dup_added_at) self.assertEquals( len(self.ss), 5, ) self.assertEquals( int(self.ss.score(0)), int(dup_added_at), ) def test_clear(self): self.assertFalse(self.ss.clear()) for i in range(5): self.ss.add(i) self.assertTrue(self.ss.clear()) self.assertEquals( len(self.ss), 0, ) def test_discard(self): self.ss.add(0) self.ss.add(1, self.now + 50) self.assertTrue(self.ss.discard(0)) self.assertFalse(self.ss.discard(0)) self.assertTrue(self.ss.discard(1)) self.assertFalse(self.ss.discard(1)) def test_peek_score(self): self.assertEquals( None, self.ss.peek_score(), ) for i in range(3): self.ss.add(i, self.now - i) self.assertEquals( int(self.now - 2), int(self.ss.peek_score()), ) self.ss.pop() self.assertEquals( int(self.now - 1), int(self.ss.peek_score()), )
class ScheduledSetTest(unittest.TestCase): def setUp(self): self.key = 'scheduled_set_test' self.now = time.time() - 1 # offset to avoid having to sleep self.ss = ScheduledSet(redis.Redis(), self.key) def tearDown(self): self.ss.clear() def test_schedule(self): self.ss.add(1, self.now) self.ss.add(2, self.now + 1000) next_item = self.ss.pop() self.assertEquals(next_item, '1') with self.assertRaises(KeyError): self.ss.pop() self.assertEquals(len(self.ss), 1) def test_peek(self): with self.assertRaises(KeyError): self.ss.peek() self.ss.add(1, self.now - 1000) self.ss.add(2, self.now) self.ss.add(3, self.now + 1000) self.assertEquals( self.ss.peek(), '1', ) self.assertEquals( self.ss.peek(position=1), '2', ) with self.assertRaises(KeyError): self.ss.peek(position=2) self.ss.pop() self.ss.pop() with self.assertRaises(KeyError): self.ss.peek() self.assertEquals(len(self.ss), 1) def test_take(self): self.ss.add('1', self.now - 3) self.ss.add('2', self.now - 2) self.ss.add('3', self.now - 1) items = self.ss.take(2) self.assertEquals(len(items), 2) self.assertEquals(['1', '2'], items) self.assertEquals(self.ss.pop(), '3') self.assertEquals( len(self.ss), 0, ) self.assertEquals( self.ss.take(0), [], ) self.assertEquals( self.ss.take(-1), [], ) def test_length(self): for i in range(2): self.ss.add(i, self.now + 50) for i in range(3): self.ss.add(i + 2, self.now - 50) self.assertEquals( len(self.ss), 5, ) def test_length_available(self): for i in range(2): self.ss.add(i, self.now + 50) for i in range(3): self.ss.add(i + 2, self.now - 50) self.assertEquals( self.ss.available(), 3, ) def test_contains(self): for i in range(5): self.ss.add(i) self.assertTrue( 0 in self.ss ) self.assertFalse( -1 in self.ss ) def test_add_dup(self): for i in range(5): self.ss.add(i) dup_added_at = 10 self.ss.add(0, score=dup_added_at) self.assertEquals( len(self.ss), 5, ) self.assertEquals( int(self.ss.score(0)), int(dup_added_at), ) def test_clear(self): self.assertFalse(self.ss.clear()) for i in range(5): self.ss.add(i) self.assertTrue(self.ss.clear()) self.assertEquals( len(self.ss), 0, ) def test_discard(self): self.ss.add(0) self.ss.add(1, self.now + 50) self.assertTrue(self.ss.discard(0)) self.assertFalse(self.ss.discard(0)) self.assertTrue(self.ss.discard(1)) self.assertFalse(self.ss.discard(1)) def test_peek_score(self): self.assertEquals( None, self.ss.peek_score(), ) for i in range(3): self.ss.add(i, self.now - i) self.assertEquals( int(self.now - 2), int(self.ss.peek_score()), ) self.ss.pop() self.assertEquals( int(self.now - 1), int(self.ss.peek_score()), )