Exemplo n.º 1
0
class ScorerTest(unittest.TestCase):
    def setUp(self):
        self.key = 'scorer_ss_test'

        class Ser(Serializer):
            loads = int
            dumps = str

        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            scorer=lambda i: i * -1,
            serializer=Ser(),
        )

    def tearDown(self):
        self.ss.clear()

    def test_scorer(self):
        for i in range(5):
            self.ss.add(i)

        self.assertEqual(
            [4, 3, 2, 1, 0],
            self.ss.take(5),
        )
Exemplo n.º 2
0
class ScorerTest(unittest.TestCase):

    def setUp(self):
        self.key = 'scorer_ss_test'

        class Ser(Serializer):
            loads = int
            dumps = str

        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            scorer=lambda i: i * -1,
            serializer=Ser(),
        )

    def tearDown(self):
        self.ss.clear()

    def test_scorer(self):
        for i in range(5):
            self.ss.add(i)

        self.assertEqual(
            [4, 3, 2, 1, 0],
            self.ss.take(5),
        )
Exemplo n.º 3
0
 def setUp(self):
     self.r = redis.Redis()
     self.set_name = self.__class__.__name__
     self.timeout_length = 0.001
     self.holder = SortedSet(redis.Redis(), self.set_name, lock_expires=10)
     self.chump = SortedSet(
         redis.Redis(),
         self.set_name,
         lock_timeout=self.timeout_length,
         lock_expires=self.timeout_length,
     )
Exemplo n.º 4
0
 def setUp(self):
     self.r = redis.Redis()
     self.set_name = self.__class__.__name__
     self.timeout_length = 0.001
     self.holder = SortedSet(redis.Redis(), self.set_name, lock_expires=10)
     self.chump = SortedSet(
         redis.Redis(),
         self.set_name,
         lock_timeout=self.timeout_length,
         lock_expires=self.timeout_length,
     )
Exemplo n.º 5
0
    def setUp(self):
        self.key = 'scorer_ss_test'

        class Ser(Serializer):
            loads = int
            dumps = str

        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            scorer=lambda i: i * -1,
            serializer=Ser(),
        )
Exemplo n.º 6
0
    def setUp(self):
        self.key = 'json_ss_test'
        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            serializer=self.FakeJsonSerializer(),
        )

        # has a bad serializer
        self.ss2 = SortedSet(
            redis.Redis(),
            self.key + '2',
            serializer=object(),
        )
Exemplo n.º 7
0
    def setUp(self):
        self.key = 'json_ss_test'
        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            serializer=self.FakeJsonSerializer(),
        )

        # has a bad serializer
        self.ss2 = SortedSet(
            redis.Redis(),
            self.key + '2',
            serializer=object(),
        )
Exemplo n.º 8
0
class LockExpiryTest(unittest.TestCase):
    """
    Ensure that we can bang on the sorted set from multiple processes without
    trouble.

    """

    def setUp(self):
        self.r = redis.Redis()
        self.set_name = self.__class__.__name__
        self.timeout_length = 0.001
        self.holder = SortedSet(redis.Redis(), self.set_name, lock_expires=10)
        self.chump = SortedSet(
            redis.Redis(),
            self.set_name,
            lock_timeout=self.timeout_length,
            lock_expires=self.timeout_length,
        )

    def tearDown(self):
        self.holder.clear()
        self.chump.clear()

    def test_lock_timeout(self):
        """
        One process holds the lock while the other times out.

        """
        with self.holder.lock:
            with self.assertRaises(LockTimeout):
                with self.chump.lock:
                    assert False, "shouldn't be able to acquire the lock"

    def test_lock_expires(self):
        """
        One process holds the lock, times out, and the other scoopes the lock.

        """
        got_the_lock = False

        # chump's acquisition should timeout, get picked up by holder
        with self.chump.lock:
            with self.holder.lock:
                got_the_lock = True

        assert got_the_lock, "`holder` should have acquired the lock"
Exemplo n.º 9
0
class LockExpiryTest(unittest.TestCase):
    """
    Ensure that we can bang on the sorted set from multiple processes without
    trouble.

    """
    def setUp(self):
        self.r = redis.Redis()
        self.set_name = self.__class__.__name__
        self.timeout_length = 0.001
        self.holder = SortedSet(redis.Redis(), self.set_name, lock_expires=10)
        self.chump = SortedSet(
            redis.Redis(),
            self.set_name,
            lock_timeout=self.timeout_length,
            lock_expires=self.timeout_length,
        )

    def tearDown(self):
        self.holder.clear()
        self.chump.clear()

    def test_lock_timeout(self):
        """
        One process holds the lock while the other times out.

        """
        with self.holder.lock:
            with self.assertRaises(LockTimeout):
                with self.chump.lock:
                    assert False, "shouldn't be able to acquire the lock"

    def test_lock_expires(self):
        """
        One process holds the lock, times out, and the other scoopes the lock.

        """
        got_the_lock = False

        # chump's acquisition should timeout, get picked up by holder
        with self.chump.lock:
            with self.holder.lock:
                got_the_lock = True

        assert got_the_lock, "`holder` should have acquired the lock"
Exemplo n.º 10
0
    def _make_ss(self):
        class Serializer(object):
            loads = int
            dumps = str

        return SortedSet(
            redis.Redis(),
            self.set_name,
            serializer=Serializer(),
        )
Exemplo n.º 11
0
    def setUp(self):
        self.key = 'scorer_ss_test'

        class Ser(Serializer):
            loads = int
            dumps = str

        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            scorer=lambda i: i * -1,
            serializer=Ser(),
        )
Exemplo n.º 12
0
class TestNTSerializer(unittest.TestCase):
    def setUp(self):
        self.ss = SortedSet(
            redis.Redis(),
            self.__class__.__name__,
            serializer=NamedtupleSerializer(DiscoTask),
        )

    def tearDown(self):
        self.ss.clear()

    def test_nt_serializer(self):
        dt = DiscoTask(tiger='larry', woods='david')

        self.ss.add(dt)

        assert len(self.ss) == 1
        assert dt in self.ss

        self.assertEqual(
            dt,
            self.ss.pop(),
        )
Exemplo n.º 13
0
class TestNTSerializer(unittest.TestCase):

    def setUp(self):
        self.ss = SortedSet(
            redis.Redis(),
            self.__class__.__name__,
            serializer=NamedtupleSerializer(DiscoTask),
        )

    def tearDown(self):
        self.ss.clear()

    def test_nt_serializer(self):
        dt = DiscoTask(tiger='larry', woods='david')

        self.ss.add(dt)

        assert len(self.ss) == 1
        assert dt in self.ss

        self.assertEqual(
            dt,
            self.ss.pop(),
        )
Exemplo n.º 14
0
 def setUp(self):
     self.key = 'ss_test'
     self.ss = SortedSet(redis.Redis(), self.key)
Exemplo n.º 15
0
 def setUp(self):
     self.key = 'ss_test'
     self.ss = SortedSet(redis.Redis(), self.key)
Exemplo n.º 16
0
class SortedSetTest(unittest.TestCase):

    def setUp(self):
        self.key = 'ss_test'
        self.ss = SortedSet(redis.Redis(), self.key)

    def tearDown(self):
        self.ss.clear()

    def test_repr(self):
        """Just make sure it doesn't blow up."""
        str(self.ss)

    def test_length(self):
        for i in range(5):
            self.ss.add(i)

        self.assertEquals(
            len(self.ss),
            5,
        )

    def test_add_with_score(self):
        item = 'samere'
        score = 123
        self.ss.add(item, score)

        assert self.ss.score(item) == score

    def test_and_and_update_score(self):
        item = 'samere'
        score = 123
        self.ss.add(item, score)

        new_score = 456
        self.ss.add(item, new_score)

        assert self.ss.score(item) == new_score

    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_ordering(self):
        for i in range(5):
            self.ss.add(i, score=i)

        self.assertEquals(
            [str(i) for i in range(5)],
            [self.ss.pop() for __ in range(5)],
        )

    def test_empty_pop(self):
        with self.assertRaises(KeyError):
            self.ss.pop()

    def test_empty_peek(self):
        with self.assertRaises(KeyError):
            self.ss.peek()

    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.assertTrue(self.ss.discard(0))
        self.assertFalse(self.ss.discard(0))

    def test_peek(self):
        with self.assertRaises(KeyError):
            self.ss.peek()

        self.ss.add(0)

        for __ in range(2):
            self.assertEquals(
                self.ss.peek(),
                '0',
            )

        with self.assertRaises(KeyError):
            self.ss.peek(position=1)

        self.ss.add(1)

        for __ in range(2):
            self.assertEquals(
                self.ss.peek(position=1),
                '1',
            )

    def test_take(self):
        for i in range(5):
            self.ss.add(i)

        self.assertEquals(
            set([str(i) for i in range(2)]),
            set(self.ss.take(2)),
        )

        self.assertEquals(
            set([str(i + 2) for i in range(3)]),
            set(self.ss.take(100)),
        )

        self.assertEquals(
            len(self.ss),
            0,
        )

        self.assertEquals(
            self.ss.take(0),
            [],
        )

        self.assertEquals(
            self.ss.take(-1),
            [],
        )
Exemplo n.º 17
0
class SerializerTest(unittest.TestCase):

    class FakeJsonSerializer(Serializer):
        """
        Handles JSON serialization.

        """
        def dumps(self, item):
            return json.dumps(item)

        def loads(self, item):
            if 'uhoh' in item:
                raise Exception("omg unserializing failed!")
            return json.loads(item)

    def setUp(self):
        self.key = 'json_ss_test'
        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            serializer=self.FakeJsonSerializer(),
        )

        # has a bad serializer
        self.ss2 = SortedSet(
            redis.Redis(),
            self.key + '2',
            serializer=object(),
        )

    def tearDown(self):
        self.ss.clear()
        self.ss2.clear()

    def test_add_and_pop(self):
        self.ss.add({'yo': 'json'}, score=1)
        self.ss.add({'yo': 'yaml'}, score=0)

        self.assertTrue(
            {'yo': 'json'} in self.ss
        )

        self.assertEqual(
            self.ss.pop(),
            {'yo': 'yaml'},
        )

        self.assertEqual(
            self.ss.pop(),
            {'yo': 'json'},
        )

        self.assertEqual(
            0,
            len(self.ss),
        )

    def test_cant_deserialize(self):
        self.ss.add({'yo': 'foo'}, score=0)
        self.ss.add({'yo': 'uhoh!'}, score=1)
        self.ss.add({'yo': 'hey'}, score=2)

        self.assertEquals(
            self.ss.take(3),
            [{'yo': 'foo'},
             {'yo': 'hey'}],
        )

    def test_bad_serializer(self):
        self.ss2.add(1, score=0)
        self.ss2.add(2, score=1)

        assert '2' in self.ss2

        # gets deserialied as a str, not an int
        self.assertEquals(
            '1',
            self.ss2.pop(),
        )
Exemplo n.º 18
0
 def setUp(self):
     self.ss = SortedSet(
         redis.Redis(),
         self.__class__.__name__,
         serializer=NamedtupleSerializer(DiscoTask),
     )
Exemplo n.º 19
0
class SortedSetTest(unittest.TestCase):
    def setUp(self):
        self.key = 'ss_test'
        self.ss = SortedSet(redis.Redis(), self.key)

    def tearDown(self):
        self.ss.clear()

    def test_repr(self):
        """Just make sure it doesn't blow up."""
        str(self.ss)

    def test_length(self):
        for i in range(5):
            self.ss.add(i)

        self.assertEquals(
            len(self.ss),
            5,
        )

    def test_add_with_score(self):
        item = 'samere'
        score = 123
        self.ss.add(item, score)

        assert self.ss.score(item) == score

    def test_and_and_update_score(self):
        item = 'samere'
        score = 123
        self.ss.add(item, score)

        new_score = 456
        self.ss.add(item, new_score)

        assert self.ss.score(item) == new_score

    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_ordering(self):
        for i in range(5):
            self.ss.add(i, score=i)

        self.assertEquals(
            [str(i) for i in range(5)],
            [self.ss.pop() for __ in range(5)],
        )

    def test_empty_pop(self):
        with self.assertRaises(KeyError):
            self.ss.pop()

    def test_empty_peek(self):
        with self.assertRaises(KeyError):
            self.ss.peek()

    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.assertTrue(self.ss.discard(0))
        self.assertFalse(self.ss.discard(0))

    def test_peek(self):
        with self.assertRaises(KeyError):
            self.ss.peek()

        self.ss.add(0)

        for __ in range(2):
            self.assertEquals(
                self.ss.peek(),
                '0',
            )

        with self.assertRaises(KeyError):
            self.ss.peek(position=1)

        self.ss.add(1)

        for __ in range(2):
            self.assertEquals(
                self.ss.peek(position=1),
                '1',
            )

    def test_take(self):
        for i in range(5):
            self.ss.add(i)

        self.assertEquals(
            set([str(i) for i in range(2)]),
            set(self.ss.take(2)),
        )

        self.assertEquals(
            set([str(i + 2) for i in range(3)]),
            set(self.ss.take(100)),
        )

        self.assertEquals(
            len(self.ss),
            0,
        )

        self.assertEquals(
            self.ss.take(0),
            [],
        )

        self.assertEquals(
            self.ss.take(-1),
            [],
        )
Exemplo n.º 20
0
 def setUp(self):
     self.ss = SortedSet(
         redis.Redis(),
         self.__class__.__name__,
         serializer=NamedtupleSerializer(DiscoTask),
     )
Exemplo n.º 21
0
class SerializerTest(unittest.TestCase):
    class FakeJsonSerializer(Serializer):
        """
        Handles JSON serialization.

        """
        def dumps(self, item):
            return json.dumps(item)

        def loads(self, item):
            if 'uhoh' in item:
                raise Exception("omg unserializing failed!")
            return json.loads(item)

    def setUp(self):
        self.key = 'json_ss_test'
        self.ss = SortedSet(
            redis.Redis(),
            self.key,
            serializer=self.FakeJsonSerializer(),
        )

        # has a bad serializer
        self.ss2 = SortedSet(
            redis.Redis(),
            self.key + '2',
            serializer=object(),
        )

    def tearDown(self):
        self.ss.clear()
        self.ss2.clear()

    def test_add_and_pop(self):
        self.ss.add({'yo': 'json'}, score=1)
        self.ss.add({'yo': 'yaml'}, score=0)

        self.assertTrue({'yo': 'json'} in self.ss)

        self.assertEqual(
            self.ss.pop(),
            {'yo': 'yaml'},
        )

        self.assertEqual(
            self.ss.pop(),
            {'yo': 'json'},
        )

        self.assertEqual(
            0,
            len(self.ss),
        )

    def test_cant_deserialize(self):
        self.ss.add({'yo': 'foo'}, score=0)
        self.ss.add({'yo': 'uhoh!'}, score=1)
        self.ss.add({'yo': 'hey'}, score=2)

        self.assertEquals(
            self.ss.take(3),
            [{
                'yo': 'foo'
            }, {
                'yo': 'hey'
            }],
        )

    def test_bad_serializer(self):
        self.ss2.add(1, score=0)
        self.ss2.add(2, score=1)

        assert '2' in self.ss2

        # gets deserialied as a str, not an int
        self.assertEquals(
            '1',
            self.ss2.pop(),
        )