class TestKVStoreTTL(unittest.TestCase): @patch.object(SyncObjConf, 'fullDumpFile', PropertyMock(return_value=None), create=True) @patch.object(SyncObjConf, 'journalFile', PropertyMock(return_value=None), create=True) def setUp(self): callback = Mock() callback.replicated = False self.so = KVStoreTTL(None, callback, callback, self_addr='127.0.0.1:1234') self.so.startAutoTick() self.so.set_retry_timeout(10) def tearDown(self): if self.so: self.so.destroy() def test_set(self): self.assertTrue(self.so.set('foo', 'bar', prevExist=False, ttl=30)) self.assertFalse(self.so.set('foo', 'bar', prevExist=False, ttl=30)) self.assertFalse(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1}, prevValue='')) self.assertTrue(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1})) def test_delete(self): self.so.autoTickPeriod = 0.2 self.so.set('foo', 'bar') self.so.set('fooo', 'bar') self.assertFalse(self.so.delete('foo', prevValue='buz')) self.assertTrue(self.so.delete('foo', recursive=True)) self.assertFalse(self.so.retry(self.so._delete, 'foo', prevValue='')) def test_expire(self): self.so.set('foo', 'bar', ttl=0.001) time.sleep(1) self.assertIsNone(self.so.get('foo')) self.assertEqual(self.so.get('foo', recursive=True), {}) @patch('time.sleep', Mock()) def test_retry(self): return_values = [FAIL_REASON.QUEUE_FULL] * 2 + [FAIL_REASON.SUCCESS, FAIL_REASON.REQUEST_DENIED] def test(callback): callback(True, return_values.pop(0)) with patch('time.time', Mock(side_effect=[1, 100])): self.assertFalse(self.so.retry(test)) self.assertTrue(self.so.retry(test)) self.assertFalse(self.so.retry(test)) def test_on_ready_override(self): self.assertTrue(self.so.set('foo', 'bar')) self.so.destroy() self.so = None so = KVStoreTTL(Mock(), None, None, self_addr='127.0.0.1:1234', partner_addrs=['127.0.0.1:1235'], patronictl=True) so.doTick(0) so.destroy()
class TestKVStoreTTL(unittest.TestCase): def setUp(self): self.conf = SyncObjConf(appendEntriesUseBatch=False, appendEntriesPeriod=0.001, raftMinTimeout=0.004, raftMaxTimeout=0.005, autoTickPeriod=0.001) callback = Mock() callback.replicated = False self.so = KVStoreTTL('127.0.0.1:1234', [], self.conf, on_set=callback, on_delete=callback) self.so.set_retry_timeout(10) @staticmethod def destroy(so): so.destroy() so._SyncObj__thread.join() def tearDown(self): if self.so: self.destroy(self.so) def test_set(self): self.assertTrue(self.so.set('foo', 'bar', prevExist=False, ttl=30)) self.assertFalse(self.so.set('foo', 'bar', prevExist=False, ttl=30)) self.assertFalse(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1}, prevValue='')) self.assertTrue(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1})) def test_delete(self): self.conf.autoTickPeriod = 0.1 self.so.set('foo', 'bar') self.so.set('fooo', 'bar') self.assertFalse(self.so.delete('foo', prevValue='buz')) self.assertTrue(self.so.delete('foo', recursive=True)) self.assertFalse(self.so.retry(self.so._delete, 'foo', prevValue='')) def test_expire(self): self.so.set('foo', 'bar', ttl=0.001) time.sleep(1) self.assertIsNone(self.so.get('foo')) self.assertEqual(self.so.get('foo', recursive=True), {}) @patch('time.sleep', Mock()) def test_retry(self): return_values = [FAIL_REASON.QUEUE_FULL] * 2 + [FAIL_REASON.SUCCESS, FAIL_REASON.REQUEST_DENIED] def test(callback): callback(True, return_values.pop(0)) with patch('time.time', Mock(side_effect=[1, 100])): self.assertFalse(self.so.retry(test)) self.assertTrue(self.so.retry(test)) self.assertFalse(self.so.retry(test)) def test_on_ready_override(self): self.assertTrue(self.so.set('foo', 'bar')) self.destroy(self.so) self.so = None self.conf.onReady = Mock() self.conf.autoTick = False so = KVStoreTTL('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf) so.doTick(0) so.destroy()