Exemple #1
0
    def addCluster(self, protocol, rack_controller):
        """Add a new stub cluster using the given `protocol`.

        The `protocol` should be an instance of `amp.AMP`.

        :return: A `Deferred` that fires with the connected protocol
            instance.
        """
        endpoint = endpoints.UNIXClientEndpoint(reactor, self.sockfile)
        protocol = yield endpoints.connectProtocol(endpoint, protocol)

        # Mock the registration into the database, as the rack controller is
        # already created. We reset this once registration is complete so as
        # to not interfere with other tests.
        registered = rack_controller
        patcher = MonkeyPatcher()
        patcher.add_patch(rackcontrollers, "register",
                          (lambda *args, **kwargs: registered))

        # Register the rack controller with the region.
        patcher.patch()
        try:
            yield protocol.callRemote(
                region.RegisterRackController,
                system_id=rack_controller.system_id,
                hostname=rack_controller.hostname,
                interfaces={},
                url=urlparse(""),
            )
        finally:
            patcher.restore()

        defer.returnValue(protocol)
Exemple #2
0
 def configure(self):
     patcher = MonkeyPatcher()
     patcher.add_patch(current_app.conf, 'CELERY_ALWAYS_EAGER', True)
     patcher.add_patch(current_app.conf,
                       'CELERY_EAGER_PROPAGATES_EXCEPTIONS', True)
     self.addCleanup(patcher.restore)
     patcher.patch()
Exemple #3
0
 def test_construct_with_patches(self):
     # Constructing a 'MonkeyPatcher' with patches adds all of the given
     # patches to the patch list.
     patcher = MonkeyPatcher((self.test_object, 'foo', 'haha'),
                             (self.test_object, 'bar', 'hehe'))
     patcher.patch()
     self.assertEquals('haha', self.test_object.foo)
     self.assertEquals('hehe', self.test_object.bar)
     self.assertEquals(self.original_object.baz, self.test_object.baz)
Exemple #4
0
 def setUp(self):
     super().setUp()
     self.patcher = MonkeyPatcher()
     self.addCleanup(self.patcher.restore)
     # Patch sys.std* and self.std*. Use TextIOWrapper to provide an
     # identical API to the "real" stdin, stdout, and stderr objects.
     self._addStream("stdin", self._wrapStream(self._buf_in))
     self._addStream("stdout", self._wrapStream(self._buf_out))
     self._addStream("stderr", self._wrapStream(self._buf_err))
     self.patcher.patch()
Exemple #5
0
    def run(self, timeout, function, *args, **kwargs):
        """Run 'function' in a reactor.

        If 'function' returns a Deferred, the reactor will keep spinning until
        the Deferred fires and its chain completes or until the timeout is
        reached -- whichever comes first.

        :raise TimeoutError: If 'timeout' is reached before the Deferred
            returned by 'function' has completed its callback chain.
        :raise NoResultError: If the reactor is somehow interrupted before
            the Deferred returned by 'function' has completed its callback
            chain.
        :raise StaleJunkError: If there's junk in the spinner from a previous
            run.
        :return: Whatever is at the end of the function's callback chain.  If
            it's an error, then raise that.
        """
        debug = MonkeyPatcher()
        if self._debug:
            debug.add_patch(defer.Deferred, 'debug', True)
            debug.add_patch(DelayedCall, 'debug', True)
        debug.patch()
        try:
            junk = self.get_junk()
            if junk:
                raise StaleJunkError(junk)
            self._save_signals()
            self._timeout_call = self._reactor.callLater(
                timeout, self._timed_out, function, timeout)
            # Calling 'stop' on the reactor will make it impossible to
            # re-start the reactor.  Since the default signal handlers for
            # TERM, BREAK and INT all call reactor.stop(), we'll patch it over
            # with crash.  XXX: It might be a better idea to either install
            # custom signal handlers or to override the methods that are
            # Twisted's signal handlers.
            stop, self._reactor.stop = self._reactor.stop, self._reactor.crash

            def run_function():
                d = defer.maybeDeferred(function, *args, **kwargs)
                d.addCallbacks(self._got_success, self._got_failure)
                d.addBoth(self._stop_reactor)

            try:
                self._reactor.callWhenRunning(run_function)
                self._reactor.run()
            finally:
                self._reactor.stop = stop
                self._restore_signals()
            try:
                return self._get_result()
            finally:
                self._clean()
        finally:
            debug.restore()
Exemple #6
0
 def setUp(self):
     super(RabbitServerSettings, self).setUp()
     from django.conf import settings
     patcher = MonkeyPatcher()
     patcher.add_patch(settings, "RABBITMQ_HOST",
                       "%s:%d" % (self.config.hostname, self.config.port))
     patcher.add_patch(settings, "RABBITMQ_USERID", "guest")
     patcher.add_patch(settings, "RABBITMQ_PASSWORD", "guest")
     patcher.add_patch(settings, "RABBITMQ_VIRTUAL_HOST", "/")
     patcher.add_patch(settings, "RABBITMQ_PUBLISH", True)
     self.addCleanup(patcher.restore)
     patcher.patch()
Exemple #7
0
 def setUp(self):
     super().setUp()
     self.monkey = MonkeyPatcher()
     # We need the event-loop up and running.
     if not eventloop.loop.running:
         raise RuntimeError(
             "Please start the event-loop before using this fixture.")
     self.rpc = get_service_in_eventloop("rpc").wait(10)
     # Where we're going to put the UNIX socket files.
     self.sockdir = self.useFixture(fixtures.TempDir()).path
     self.sockfile = path.join(self.sockdir, "sock")
     # Configure the RPC service with a UNIX endpoint.
     self.addCleanup(self.stop)
     self.start()
Exemple #8
0
 def setUp(self):
     super(WorkerCacheFixture, self).setUp()
     patcher = MonkeyPatcher((cache, 'cache', cache.Cache({})),
                             (cache, 'initialized', True))
     self.addCleanup(patcher.restore)
     patcher.patch()
Exemple #9
0
 def setUp(self):
     super().setUp()
     self.test_object = TestObj()
     self.original_object = TestObj()
     self.monkey_patcher = MonkeyPatcher()