def test_add_name_conflict(self, resources: ResourceCollection): """Tests that add() won't let replace existing resources.""" resource = resources.add(5, 'foo') exc = pytest.raises(ResourceConflict, resources.add, 4, 'foo') assert exc.value.resource is resource assert str(exc.value) == ('"foo" conflicts with Resource(types=(\'int\',), alias=\'foo\', ' 'value=5, context_var=None)')
def test_add(self, resources: ResourceCollection, event_loop, delay): """Tests that a resource is properly added to the collection and listeners are notified.""" events = [] resources.add_listener(ResourceEventType.added, events.append) if delay: call = partial(resources.add, 6, 'foo', 'foo.bar', extra_types=float) event_loop.call_soon(call) else: resources.add(6, 'foo', 'foo.bar', extra_types=float) value = yield from resources.request(int, 'foo', timeout=2) assert value == 6 assert len(events) == 1 resource = events[0].resource assert resource.alias == 'foo' assert resource.context_var == 'foo.bar' assert resource.value == 6 assert resource.types == ('int', 'float')
def test_remove(self, resources: ResourceCollection): """Tests that resources can be removed and that the listeners are notified.""" resource = resources.add(4) events = [] resources.add_listener(ResourceEventType.removed, events.append) resources.remove(resource) assert len(events) == 1 assert events[0].resource.value == 4 with pytest.raises(ResourceNotFoundError): yield from resources.request(int, timeout=0)