~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/rabbit/testing/server.py

Rip out RabbitFixture; it didn't work as intended. Replace with some functions copied from an unlanded testtools branch (with instructions to remove when/if that branch does land and is released).

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
    ]
11
11
 
12
12
import errno
13
 
import itertools
14
13
import os
15
14
import re
16
15
import socket
160
159
            s.close()
161
160
 
162
161
 
163
 
class RabbitFixture(Fixture):
164
 
    """Common fixture stuff for dealing with RabbitMQ servers.
165
 
 
166
 
    In particular this adopts detail handling code from `testtools` so that
167
 
    details from sub-fixtures are propagated up to the test case.
168
 
    """
169
 
 
170
 
    def useFixture(self, fixture):
171
 
        super(RabbitFixture, self).useFixture(fixture)
172
 
        self.addCleanup(self._gather_details, fixture.getDetails)
173
 
        return fixture
174
 
 
175
 
    def _gather_details(self, getDetails):
176
 
        """Merge the details from getDetails() into self.getDetails().
177
 
 
178
 
        Shamelessly adapted from `testtools.TestCase._gather_details`.
179
 
        """
180
 
        details = getDetails()
181
 
        my_details = self.getDetails()
182
 
        for name, content_object in details.items():
183
 
            new_name = name
184
 
            disambiguator = itertools.count(1)
185
 
            while new_name in my_details:
186
 
                new_name = '%s-%d' % (name, next(disambiguator))
187
 
            name = new_name
188
 
            content_bytes = list(content_object.iter_bytes())
189
 
            content_callback = lambda: content_bytes
190
 
            self.addDetail(name,
191
 
                Content(content_object.content_type, content_callback))
192
 
 
193
 
 
194
 
class RabbitServerResources(RabbitFixture):
 
162
class RabbitServerResources(Fixture):
195
163
    """Allocate the resources a RabbitMQ server needs.
196
164
 
197
165
    :ivar hostname: The host the RabbitMQ is on (always localhost for
222
190
        return "%s@%s" % (self.nodename, socket.gethostname())
223
191
 
224
192
 
225
 
class RabbitServerEnvironment(RabbitFixture):
 
193
class RabbitServerEnvironment(Fixture):
226
194
    """Export the environment variables needed to talk to a RabbitMQ instance.
227
195
 
228
196
    When setup this exports the key RabbitMQ variables:
321
289
            password="guest", virtual_host="/", insist=False)
322
290
 
323
291
 
324
 
class RabbitServerRunner(RabbitFixture):
 
292
class RabbitServerRunner(Fixture):
325
293
    """Run a RabbitMQ server.
326
294
 
327
295
    :ivar pid: The pid of the server.
416
384
                "RabbitMQ (pid=%d) did not quit." % (self.pid,))
417
385
 
418
386
 
419
 
class RabbitServer(RabbitFixture):
 
387
class RabbitServer(Fixture):
420
388
    """A RabbitMQ server fixture.
421
389
 
422
390
    When setup a RabbitMQ instance will be running and the environment
428
396
 
429
397
    def setUp(self):
430
398
        super(RabbitServer, self).setUp()
431
 
        self.config = self.useFixture(RabbitServerResources())
432
 
        self.runner = self.useFixture(RabbitServerRunner(self.config))
 
399
        self.config = RabbitServerResources()
 
400
        self.useFixture(self.config)
 
401
        self.runner = RabbitServerRunner(self.config)
 
402
        self.useFixture(self.runner)