def test_stop(mocks: Mocks, context: yogi.Context): """Checks the stop() function in the error-free case""" def fn(context): assert context == 1234 return yogi.ErrorCode.OK mocks.MOCK_ContextStop(fn) context.stop()
def test_run_in_background(mocks: Mocks, context: yogi.Context): """Checks the run_in_background() function in the error-free case""" def fn(context): assert context == 1234 return yogi.ErrorCode.OK mocks.MOCK_ContextRunInBackground(fn) context.run_in_background()
def test_run_error(mocks: Mocks, context: yogi.Context): """Checks the run() function in the error case""" def fn(context, count, duration): assert -1 == duration return yogi.ErrorCode.UNKNOWN mocks.MOCK_ContextRun(fn) with pytest.raises(yogi.FailureException): context.run()
def test_wait_for_stopped_error(mocks: Mocks, context: yogi.Context): """Checks the wait_for_stopped() function in the error case""" def fn(context, duration): assert -1 == duration return yogi.ErrorCode.UNKNOWN mocks.MOCK_ContextWaitForStopped(fn) with pytest.raises(yogi.FailureException): context.wait_for_stopped()
def test_format_error(mocks: Mocks, context: yogi.Context): """Verifies that errors while formatting an object as a string get reported""" def fn(obj, str_, strsize, objfmt, nullstr): assert objfmt == b'foo' assert nullstr == b'bar' assert not strsize return yogi.ErrorCode.UNKNOWN mocks.MOCK_FormatObject(fn) with pytest.raises(yogi.FailureException): context.format('foo', 'bar')
def test_wait_for_stopped_timeout(mocks: Mocks, context: yogi.Context): """Checks the wait_for_stopped() function in the timeout case""" def fn(context, duration): return yogi.ErrorCode.TIMEOUT mocks.MOCK_ContextWaitForStopped(fn) assert not context.wait_for_stopped(yogi.Duration.from_milliseconds(1))
def test_post(mocks: Mocks, context: yogi.Context): """Checks the post() function in the error-free case""" def fn(context, fn, userarg): assert context == 1234 assert fn fn(userarg) return yogi.ErrorCode.OK mocks.MOCK_ContextPost(fn) called = False def handlerFn(): nonlocal called called = True context.post(handlerFn) assert called
def test_wait_for_stopped(mocks: Mocks, context: yogi.Context): """Checks the wait_for_stopped() function in the error-free case""" def fn(context, duration): assert context == 1234 assert 1000000 == duration return yogi.ErrorCode.OK mocks.MOCK_ContextWaitForStopped(fn) assert context.wait_for_stopped(yogi.Duration.from_milliseconds(1))
def test_poll_one(mocks: Mocks, context: yogi.Context): """Checks the poll_one() function in the error-free case""" def fn(context, count): assert context == 1234 assert count count.contents.value = 1 return yogi.ErrorCode.OK mocks.MOCK_ContextPollOne(fn) assert context.poll_one() == 1
def test_run_one(mocks: Mocks, context: yogi.Context): """Checks the run_one() function in the error-free case""" def fn(context, count, duration): assert context == 1234 assert count assert 1000000 == duration count.contents.value = 1 return yogi.ErrorCode.OK mocks.MOCK_ContextRunOne(fn) assert context.run_one(yogi.Duration.from_milliseconds(1)) == 1
def test_format(mocks: Mocks, context: yogi.Context, hello_bytes: bytes): """Verifies that an object can be formatted as a string""" def fn(obj, str_, strsize, objfmt, nullstr): assert obj == 1234 assert objfmt is None assert nullstr is None assert not strsize str_.contents.value = hello_bytes return yogi.ErrorCode.OK mocks.MOCK_FormatObject(fn) assert context.format() == 'hello'
def run_context_until_branches_are_connected( self, context: yogi.Context, branches: List[yogi.Branch]) -> None: uuids = {} for branch in branches: uuids[branch] = [x.uuid for x in branches if x is not branch] start = time.time() while uuids: context.poll() branch = next(iter(uuids)) for uuid in branch.get_connected_branches(): if uuid in uuids[branch]: uuids[branch].remove(uuid) if not uuids[branch]: del uuids[branch] if time.time() - start > 3: print(uuids) raise TimeoutError("Branches did not connect")
def test_poll_one_error(mocks: Mocks, context: yogi.Context): """Checks the poll_one() function in the error case""" mocks.MOCK_ContextPollOne(lambda *_: yogi.ErrorCode.WRONG_OBJECT_TYPE) with pytest.raises(yogi.FailureException): context.poll_one()
def test_run_in_background_error(mocks: Mocks, context: yogi.Context): """Checks the run_in_background() function in the error case""" mocks.MOCK_ContextRunInBackground( lambda *_: yogi.ErrorCode.WRONG_OBJECT_TYPE) with pytest.raises(yogi.FailureException): context.run_in_background()