async def watch_compacted_revision_test(): events_iterator, cancel = await etcd.watch( b'/watchcompation', start_revision=(revision - 1)) error_raised = False compacted_revision = 0 try: await events_iterator.__anext__() except Exception as err: error_raised = True assert isinstance(err, aetcd3.exceptions.RevisionCompactedError) compacted_revision = err.compacted_revision assert error_raised is True assert compacted_revision == revision change_count = 0 events_iterator, cancel = await etcd.watch( b'/watchcompation', start_revision=compacted_revision) async for event in events_iterator: assert event.key == b'/watchcompation' assert event.value == \ utils.to_bytes(str(change_count)) # if cancel worked, we should not receive event 3 assert event.value != utils.to_bytes('3') change_count += 1 if change_count > 2: await cancel()
async def test_watch_prefix(self, etcd): def update_etcd(v): etcdctl('put', '/doot/watch/prefix/' + v, v) out = etcdctl('get', '/doot/watch/prefix/' + v) assert base64.b64decode(out['kvs'][0]['value']) == \ utils.to_bytes(v) def update_key(): # sleep to make watch can get the event time.sleep(3) update_etcd('0') time.sleep(1) update_etcd('1') time.sleep(1) update_etcd('2') time.sleep(1) update_etcd('3') time.sleep(1) t = threading.Thread(name="update_key_prefix", target=update_key) t.start() change_count = 0 events_iterator, cancel = await etcd.watch_prefix( '/doot/watch/prefix/') async for event in events_iterator: assert event.key == \ utils.to_bytes('/doot/watch/prefix/{}'.format(change_count)) assert event.value == \ utils.to_bytes(str(change_count)) # if cancel worked, we should not receive event 3 assert event.value != utils.to_bytes('3') change_count += 1 if change_count > 2: # if cancel not work, we will block in this for-loop forever await cancel() t.join()
async def test_lease_expire(self, etcd): key = '/doot/lease_test_expire' lease = await etcd.lease(1) await etcd.put(key, 'this is a lease', lease=lease) assert (await lease.keys()) == [utils.to_bytes(key)] v, _ = await etcd.get(key) assert v == b'this is a lease' assert (await lease.remaining_ttl()) <= (await lease.granted_ttl()) # wait for the lease to expire await asyncio.sleep((await lease.granted_ttl()) + 2) v, _ = await etcd.get(key) assert v is None
def update_etcd(v): etcdctl('put', '/doot/watch/prefix/' + v, v) out = etcdctl('get', '/doot/watch/prefix/' + v) assert base64.b64decode(out['kvs'][0]['value']) == \ utils.to_bytes(v)
def update_etcd(v): etcdctl('put', '/watchcompation', v) out = etcdctl('get', '/watchcompation') assert base64.b64decode(out['kvs'][0]['value']) == \ utils.to_bytes(v)