def test_mark_token_as_completed(): ct = CancellationToken() ct.complete() assert not ct.cancelled assert ct.completed
def test_remove_callback(): flag = Counter() ct = CancellationToken() ct.on_cancel(flag.inc) ct.remove_callback(flag.inc) ct.cancel() assert flag.count == 0
def test_call_only_new_callback_on_canceled(): flag = Counter() flag2 = Counter() ct = CancellationToken() ct.on_cancel(flag2.inc) ct.cancel() ct.on_cancel(flag.inc) assert flag.count == 1 assert flag2.count == 1
def test_dont_call_callback_being_added_to_completed_token(): ct = CancellationToken() flag = Counter() ct.complete() ct.on_cancel(flag.inc) assert flag.count == 0
def test_call_callback_on_cancel(): flag = Counter() ct = CancellationToken() ct.on_cancel(flag.inc) ct.cancel() assert flag.count == 1
def test_remove_callback_from_callback(): flag = Counter() ct = CancellationToken() def callback2(): flag.inc() def callback(): ct.remove_callback(callback2) ct.on_cancel(callback) ct.on_cancel(callback2) ct.cancel() assert flag.count == 1
def test_callback_added_from_callback(): flag = Counter() ct = CancellationToken() def new_callback(): flag.inc() def callback(): ct.on_cancel(new_callback) ct.on_cancel(callback) ct.cancel() assert flag.count == 1
def test_check_if_cancelled(): ct = CancellationToken() ct.cancel() assert ct.cancelled assert ct.completed
def setup_token(): holder[0] = CancellationToken() holder[0].on_cancel(flag.inc)