Exemplo n.º 1
0
    def __init__(self, testCase, action=None, timeout=10):
        """
        Creates a kqueue reactor for use in unit tests.  The reactor is patched
        with the vnode event handler.  Once the reactor is running, it will
        call a supplied method.  It's expected that the method will ultimately
        trigger the stop() of the reactor.  The reactor will time out after 10
        seconds.

        @param testCase: a test method which is needed for adding cleanup to
        @param action: a method which will get called after the reactor is
            running
        @param timeout: how many seconds to keep the reactor running before
            giving up and stopping it
        """
        self.testCase = testCase
        self.reactor = KQueueReactor()
        patchReactor(self.reactor)
        self.action = action
        self.timeout = timeout

        def maybeStop():
            if self.reactor.running:
                return self.reactor.stop()

        self.testCase.addCleanup(maybeStop)
Exemplo n.º 2
0
    def test_EINTR(self):
        """
        L{KQueueReactor} handles L{errno.EINTR} in C{doKEvent} by returning.
        """
        class FakeKQueue:
            """
            A fake KQueue that raises L{errno.EINTR} when C{control} is called,
            like a real KQueue would if it was interrupted.
            """
            def control(self, *args, **kwargs):
                raise OSError(errno.EINTR, "Interrupted")

        reactor = KQueueReactor(makeFakeKQueue(FakeKQueue, _fakeKEvent))
        # This should return cleanly -- should not raise the OSError we're
        # spawning, nor get upset and raise about the incomplete KQueue fake.
        reactor.doKEvent(0)
Exemplo n.º 3
0
    def test_EINTR(self):
        """
        L{KQueueReactor} handles L{errno.EINTR} in C{doKEvent} by returning.
        """
        class FakeKQueue(object):
            """
            A fake KQueue that raises L{errno.EINTR} when C{control} is called,
            a a real KQueue would if it was interrupted.
            """
            def control(self, *args, **kwargs):
                raise OSError(errno.EINTR, "Interrupted")

        reactor = KQueueReactor(makeFakeKQueue(FakeKQueue, _fakeKEvent))
        # This should return cleanly -- should not raise the OSError we're
        # spawning, nor get upset and raise about the incomplete KQueue fake.
        reactor.doKEvent(0)
Exemplo n.º 4
0
class KQueueReactorTestFixture(object):

    def __init__(self, testCase, action=None, timeout=10):
        """
        Creates a kqueue reactor for use in unit tests.  The reactor is patched
        with the vnode event handler.  Once the reactor is running, it will
        call a supplied method.  It's expected that the method will ultimately
        trigger the stop() of the reactor.  The reactor will time out after 10
        seconds.

        @param testCase: a test method which is needed for adding cleanup to
        @param action: a method which will get called after the reactor is
            running
        @param timeout: how many seconds to keep the reactor running before
            giving up and stopping it
        """
        self.testCase = testCase
        self.reactor = KQueueReactor()
        patchReactor(self.reactor)
        self.action = action
        self.timeout = timeout

        def maybeStop():
            if self.reactor.running:
                return self.reactor.stop()

        self.testCase.addCleanup(maybeStop)


    def runReactor(self):
        """
        Run the test reactor, adding cleanup code to stop if after a timeout,
        and calling the action method
        """
        def getReadyToStop():
            self.reactor.callLater(self.timeout, self.reactor.stop)
        self.reactor.callWhenRunning(getReadyToStop)
        if self.action is not None:
            self.reactor.callWhenRunning(self.action)
        self.reactor.run(installSignalHandlers=False)