예제 #1
0
    def test_add_event_conflict(self, resources: ResourceCollection):
        """
        Tests that a resource adding is cancelled if an event listener raises ResourceConflict.
        """

        def listener(event):
            raise ResourceConflict('conflict test', event.resource)

        resources.add_listener(ResourceEventType.added, listener)
        pytest.raises(ResourceConflict, resources.add, 5)
        with pytest.raises(ResourceNotFoundError):
            yield from resources.request(int, timeout=0)
예제 #2
0
    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)
예제 #3
0
    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')