def test_multiple_leases_churn(caplog, mocker): project = os.environ['GCLOUD_PROJECT'] task_queue = 'test-pull' tasks = [ {'test_idx': 1}, {'test_idx': 2}, ] def succeed_after_multiple_leases(ts): _ = [x**2 for x in range(40000000)] return ['ok' for _ in ts] worker = mocker.Mock() worker.side_effect = succeed_after_multiple_leases tm = TaskManager(project, task_queue, worker, batch_size=len(tasks), lease_seconds=4) # drain old test tasks tm.tq.drain() # insert new ones for task in tasks: tm.tq.insert(encode(json.dumps(task)), tag=encode('gcloud-rest-manager-test-multilease')) caplog.clear() tm.find_and_process_work() assert worker.mock_calls == [mocker.call(tasks)] for record in caplog.records: assert record.levelname != 'ERROR'
def test_lifecycle(caplog, mocker): project = os.environ['GCLOUD_PROJECT'] task_queue = 'test-pull' tasks = [ {'test_idx': 1}, {'test_idx': 2}, {'test_idx': 3}, {'test_idx': 4}, ] worker = mocker.Mock() worker.return_value = ['ok' for _ in tasks] tm = TaskManager(project, task_queue, worker, batch_size=len(tasks), lease_seconds=10) # drain old test tasks tm.tq.drain() # insert new ones for task in tasks: tm.tq.insert(encode(json.dumps(task)), tag=encode('gcloud-rest-manager-test-lifecycle')) tm.find_and_process_work() assert worker.mock_calls == [mocker.call(tasks)] for record in caplog.records: assert record.levelname != 'ERROR'
def test_lifecycle(): project = os.environ['GCLOUD_PROJECT'] task_queue = 'test-pull' payload = 'do-the-lifecycle' tq = TaskQueue(project, task_queue) # drain old test tasks tq.drain() inserted = tq.insert(encode(payload), tag=encode('gcloud-rest-queue-test')) print(inserted) got = tq.get(inserted['name'], full=True) print(got) assert inserted == got listed = tq.list(full=True) print(listed) assert listed and listed['tasks'] assert inserted in listed['tasks'] leased = {'name': {'whyIsThisADict': 'subscriptableLinting'}} while leased['name'] != inserted['name']: leased_list = tq.lease(num_tasks=1) print(leased_list) assert len(leased_list['tasks']) == 1 leased = leased_list['tasks'][0] print(leased) for k, v in leased.items(): if k == 'scheduleTime': assert inserted[k] != v elif k == 'status': assert not inserted.get(k) assert v['attemptDispatchCount'] == 1 else: assert inserted[k] == v renewed = tq.renew(leased) print(renewed) for k, v in renewed.items(): if k == 'scheduleTime': assert leased[k] != v else: assert leased[k] == v # ack? # cancel? tq.delete(renewed['name'])
def test_multiple_leases(caplog, mocker, project, creds, pull_queue_name): tasks = [ { 'test_idx': 1 }, { 'test_idx': 2 }, ] def succeed_after_multiple_leases(ts): time.sleep(10) return ['ok' for _ in ts] worker = mocker.Mock() worker.side_effect = succeed_after_multiple_leases tm = TaskManager(project, pull_queue_name, worker, batch_size=len(tasks), lease_seconds=4, service_file=creds) # drain old test tasks tm.tq.drain() # insert new ones for task in tasks: tm.tq.insert(encode(json.dumps(task)), tag=encode('gcloud-rest-manager-test-multilease')) caplog.clear() tm.find_and_process_work() assert worker.mock_calls == [mocker.call(tasks)] for record in caplog.records: assert record.levelname != 'ERROR'