~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/testing/layers.py

  • Committer: Stuart Bishop
  • Date: 2009-09-30 11:50:20 UTC
  • mto: (9232.3.7 memcache)
  • mto: This revision was merged to the branch mainline in revision 10064.
  • Revision ID: stuart.bishop@canonical.com-20090930115020-2a1vol6fe3ma1xih
MemcacheClient tests and layer fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
438
438
    """
439
439
    _reset_between_tests = True
440
440
 
441
 
    client = None # Class attribute. A memcache.Client instance.
 
441
    # A memcache.Client instance.
 
442
    client = None
 
443
 
 
444
    # A subprocess.Popen instance if this process spawned the test
 
445
    # memcached.
 
446
    _memcached_process = None
442
447
 
443
448
    @classmethod
444
449
    @profiled
446
451
        # Create a client
447
452
        MemcachedLayer.client = memcache_client_factory()
448
453
 
449
 
        # Ensure an old memcached isn't still running.
 
454
        # First, check to see if there is a memcached already running.
 
455
        # This happens when new layers are run as a subprocess.
450
456
        test_key = "MemcachedLayer__live_test"
451
457
        if MemcachedLayer.client.set(test_key, "live"):
452
 
            raise LayerInvariantError("memcached already running.")
 
458
            return
453
459
 
454
460
        cmd = [
455
461
            'memcached',
474
480
            MemcachedLayer.client.forget_dead_hosts()
475
481
            time.sleep(0.1)
476
482
 
 
483
        # Register an atexit hook just in case tearDown doesn't get
 
484
        # invoked for some perculiar reason.
477
485
        def terminate_on_exit(pid=MemcachedLayer._memcached_process.pid):
478
486
            try:
479
487
                if pid is not None:
488
496
        MemcachedLayer.client.disconnect_all()
489
497
        MemcachedLayer.client = None
490
498
        # Kill our memcached, and there is no reason to be nice about it.
491
 
        if MemcachedLayer._memcached_process.returncode is None:
 
499
        if (MemcachedLayer._memcached_process is not None
 
500
            and MemcachedLayer._memcached_process.pid is None):
492
501
            os.kill(MemcachedLayer._memcached_process.pid, signal.SIGKILL)
493
 
        MemcachedLayer._memcached_process.wait()
494
 
        del MemcachedLayer._memcached_process
 
502
            MemcachedLayer._memcached_process.wait()
 
503
        MemcachedLayer._memcached_process = None
495
504
 
496
505
    @classmethod
497
506
    @profiled
777
786
    return None
778
787
 
779
788
 
780
 
class LaunchpadLayer(DatabaseLayer, LibrarianLayer):
 
789
class LaunchpadLayer(DatabaseLayer, LibrarianLayer, MemcachedLayer):
781
790
    """Provides access to the Launchpad database and daemons.
782
791
 
783
792
    We need to ensure that the database setup runs before the daemon
785
794
    already connected to the database.
786
795
 
787
796
    This layer is mainly used by tests that call initZopeless() themselves.
 
797
    Most tests will use a sublayer such as LaunchpadFunctionalLayer that
 
798
    provides access to the Component Architecture.
788
799
    """
789
800
    @classmethod
790
801
    @profiled
1378
1389
    def resetBetweenTests(cls, flag):
1379
1390
        LibrarianLayer._reset_between_tests = flag
1380
1391
        DatabaseLayer._reset_between_tests = flag
 
1392
        MemcachedLayer._reset_between_tests = flag
1381
1393
 
1382
1394
    @classmethod
1383
1395
    @profiled