Example #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)
Example #2
0
    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)')
Example #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')
Example #4
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)
Example #5
0
 def test_bad_request(self, resources: ResourceCollection, bad_arg, errormsg):
     type_ = None if bad_arg == 'type' else 'foo'
     alias = None if bad_arg == 'alias' else 'foo'
     with pytest.raises(ValueError) as exc:
         yield from resources.request(type_, alias)
     assert str(exc.value) == errormsg
Example #6
0
    def test_request_timeout(self, resources: ResourceCollection):
        with pytest.raises(ResourceNotFoundError) as exc:
            yield from resources.request(int, timeout=0.2)

        assert str(exc.value) == "no matching resource was found for type='int' alias='default'"