|
|
import unittest |
|
|
import pytest |
|
|
from main import ExclusiveBox |
|
|
|
|
|
class TestExclusiveBox(unittest.TestCase): |
|
|
|
|
|
def test_put_get(self): |
|
|
async def async_fn(): |
|
|
box = ExclusiveBox() |
|
|
box.put(None, python='awesome') |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
|
|
|
task = ag.start(async_fn()) |
|
|
assert task.finished |
|
|
|
|
|
def test_update_get(self): |
|
|
async def async_fn(): |
|
|
box = ExclusiveBox() |
|
|
box.put_or_update(None, python='awesome') |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
|
|
|
task = ag.start(async_fn()) |
|
|
assert task.finished |
|
|
|
|
|
def test_get_put(self): |
|
|
async def async_fn(box): |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
|
|
|
box = ExclusiveBox() |
|
|
task = ag.start(async_fn(box)) |
|
|
assert task.state is ag.TaskState.STARTED |
|
|
box.put(None, python='awesome') |
|
|
assert task.finished |
|
|
|
|
|
def test_get_update(self): |
|
|
async def async_fn(box): |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
|
|
|
box = ExclusiveBox() |
|
|
task = ag.start(async_fn(box)) |
|
|
assert task.state is ag.TaskState.STARTED |
|
|
box.put_or_update(None, python='awesome') |
|
|
assert task.finished |
|
|
|
|
|
def test_put_put(self): |
|
|
async def async_fn(): |
|
|
box = ExclusiveBox() |
|
|
box.put(None) |
|
|
box.put(None) |
|
|
|
|
|
task = ag.start(async_fn()) |
|
|
assert task.finished |
|
|
|
|
|
def test_get_get(self): |
|
|
box = ExclusiveBox() |
|
|
ag.start(box.get()) |
|
|
with pytest.raises(ag.InvalidStateError): |
|
|
ag.start(box.get()) |
|
|
|
|
|
def test_put_get_put(self): |
|
|
async def async_fn(): |
|
|
box = ExclusiveBox() |
|
|
box.put(None, python='awesome') |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
box.put(None, python='awesome') |
|
|
|
|
|
task = ag.start(async_fn()) |
|
|
assert task.finished |
|
|
|
|
|
def test_put_get_update_get(self): |
|
|
async def async_fn(): |
|
|
box = ExclusiveBox() |
|
|
box.put(1) |
|
|
args, kwargs = await box.get() |
|
|
assert args == (1,) |
|
|
assert kwargs == {} |
|
|
box.update(2) |
|
|
args, kwargs = await box.get() |
|
|
assert args == (2,) |
|
|
assert kwargs == {} |
|
|
|
|
|
task = ag.start(async_fn()) |
|
|
assert task.finished |
|
|
|
|
|
def test_put_get_get(self): |
|
|
async def async_fn(): |
|
|
box = ExclusiveBox() |
|
|
box.put(None, python='awesome') |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
|
|
|
task = ag.start(async_fn()) |
|
|
assert task.finished |
|
|
|
|
|
def test_get_put_get(self): |
|
|
async def async_fn(box): |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
|
|
|
box = ExclusiveBox() |
|
|
task = ag.start(async_fn(box)) |
|
|
assert task.state is ag.TaskState.STARTED |
|
|
box.put(None, python='awesome') |
|
|
assert task.finished |
|
|
|
|
|
def test_get_put_put(self): |
|
|
async def async_fn(box): |
|
|
args, kwargs = await box.get() |
|
|
assert args == (None,) |
|
|
assert kwargs == {'python': 'awesome'} |
|
|
|
|
|
box = ExclusiveBox() |
|
|
task = ag.start(async_fn(box)) |
|
|
assert task.state is ag.TaskState.STARTED |
|
|
box.put(None, python='awesome') |
|
|
assert task.finished |
|
|
box.put(None, python='awesome') |
|
|
|
|
|
def test_cancel(self): |
|
|
TS = ag.TaskState |
|
|
|
|
|
async def async_fn(ctx, box): |
|
|
async with ag.open_cancel_scope() as scope: |
|
|
ctx['scope'] = scope |
|
|
await box.get() |
|
|
pytest.fail() |
|
|
await ag.sleep_forever() |
|
|
|
|
|
ctx = {} |
|
|
box = ExclusiveBox() |
|
|
task = ag.start(async_fn(ctx, box)) |
|
|
assert task.state is TS.STARTED |
|
|
ctx['scope'].cancel() |
|
|
assert task.state is TS.STARTED |
|
|
box.put(None, python='awesome') |
|
|
assert task.state is TS.STARTED |
|
|
task._step() |
|
|
assert task.state is TS.FINISHED |
|
|
|
|
|
def test_is_empty(self): |
|
|
async def async_fn(): |
|
|
box = ExclusiveBox() |
|
|
assert box.is_empty |
|
|
box.put(None) |
|
|
assert not box.is_empty |
|
|
box.clear() |
|
|
assert box.is_empty |
|
|
|
|
|
task = ag.start(async_fn()) |
|
|
assert task.finished |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
unittest.main() |