Ejemplo n.º 1
0
 def test_clear(self):
     s = LimitedSet(maxlen=2)
     s.add('foo')
     s.add('bar')
     self.assertEqual(len(s), 2)
     s.clear()
     self.assertFalse(s)
Ejemplo n.º 2
0
 def test_iter(self):
     s = LimitedSet(maxlen=3)
     items = ['foo', 'bar', 'baz', 'xaz']
     for item in items:
         s.add(item)
     l = list(iter(s))
     for item in items[1:]:
         self.assertIn(item, l)
     self.assertNotIn('foo', l)
     self.assertListEqual(l, items[1:], 'order by insertion time')
Ejemplo n.º 3
0
 def test_add(self):
     s = LimitedSet(maxlen=2)
     s.add('foo')
     s.add('bar')
     for n in 'foo', 'bar':
         self.assertIn(n, s)
     s.add('baz')
     for n in 'bar', 'baz':
         self.assertIn(n, s)
     self.assertNotIn('foo', s)
Ejemplo n.º 4
0
    def test_update(self):
        s1 = LimitedSet(maxlen=2)
        s1.add('foo')
        s1.add('bar')

        s2 = LimitedSet(maxlen=2)
        s2.update(s1)
        self.assertItemsEqual(list(s2), ['foo', 'bar'])

        s2.update(['bla'])
        self.assertItemsEqual(list(s2), ['bla', 'bar'])

        s2.update(['do', 're'])
        self.assertItemsEqual(list(s2), ['do', 're'])
Ejemplo n.º 5
0
    def test_start(self):
        try:
            c = Mock()
            mingle = Mingle(c)
            self.assertTrue(mingle.enabled)

            Aig = LimitedSet()
            Big = LimitedSet()
            Aig.add('Aig-1')
            Aig.add('Aig-2')
            Big.add('Big-1')

            I = c.app.control.inspect.return_value = Mock()
            I.hello.return_value = {
                '*****@*****.**': {
                    'clock': 312,
                    'revoked': Aig._data,
                },
                '*****@*****.**': {
                    'clock': 29,
                    'revoked': Big._data,
                },
                '*****@*****.**': {
                    'error': 'unknown method',
                },
            }

            mingle.start(c)
            I.hello.assert_called_with()
            c.app.clock.adjust.assert_has_calls([
                call(312), call(29),
            ], any_order=True)
            self.assertIn('Aig-1', worker_state.revoked)
            self.assertIn('Aig-2', worker_state.revoked)
            self.assertIn('Big-1', worker_state.revoked)
        finally:
            worker_state.revoked.clear()
Ejemplo n.º 6
0
 def test_as_dict(self):
     s = LimitedSet(maxlen=2)
     s.add('foo')
     self.assertIsInstance(s.as_dict(), dict)
Ejemplo n.º 7
0
 def test_discard(self):
     s = LimitedSet(maxlen=2)
     s.add('foo')
     s.discard('foo')
     self.assertNotIn('foo', s)
     s.discard('foo')
Ejemplo n.º 8
0
 def test_repr(self):
     s = LimitedSet(maxlen=2)
     items = 'foo', 'bar'
     for item in items:
         s.add(item)
     self.assertIn('LimitedSet(', repr(s))
Ejemplo n.º 9
0
 def test_pickleable(self):
     s = LimitedSet(maxlen=2)
     s.add('foo')
     s.add('bar')
     self.assertEqual(pickle.loads(pickle.dumps(s)), s)
Ejemplo n.º 10
0
    def test_purge(self):
        s = LimitedSet(maxlen=None)
        [s.add(i) for i in range(10)]
        s.maxlen = 2
        s.purge(1)
        self.assertEqual(len(s), 9)
        s.purge(None)
        self.assertEqual(len(s), 2)

        # expired
        s = LimitedSet(maxlen=None, expires=1)
        [s.add(i) for i in range(10)]
        s.maxlen = 2
        s.purge(1, now=lambda: time() + 100)
        self.assertEqual(len(s), 9)
        s.purge(None, now=lambda: time() + 100)
        self.assertEqual(len(s), 2)

        # not expired
        s = LimitedSet(maxlen=None, expires=1)
        [s.add(i) for i in range(10)]
        s.maxlen = 2
        s.purge(1, now=lambda: time() - 100)
        self.assertEqual(len(s), 10)
        s.purge(None, now=lambda: time() - 100)
        self.assertEqual(len(s), 10)

        s = LimitedSet(maxlen=None)
        [s.add(i) for i in range(10)]
        s.maxlen = 2
        with patch('celery.utils.datastructures.heappop') as hp:
            hp.side_effect = IndexError()
            s.purge()
            hp.assert_called_with(s._heap)
        with patch('celery.utils.datastructures.heappop') as hp:
            s._data = dict((i * 2, i * 2) for i in range(10))
            s.purge()
            self.assertEqual(hp.call_count, 10)