예제 #1
0
    def test09_mainTests_wrongRaise(self):
        import Queue
        import time
        from cStringIO import StringIO

        class MockException(Exception):
            pass

        class MockQueue:
            def qsize(qSelf):
                return 1

            def get(qSelf, timeout=1):
                raise MockException("MOCK")

            def empty(qSelf):
                return False

            def put(qSelf, val):
                pass

        oldStderr = sys.stderr
        sys.stderr = StringIO()
        anotherLock = pokerlock.PokerLock(self.parameters)
        anotherLock.q = MockQueue()
        anotherLock.start()
        time.sleep(2)
        value = sys.stderr.getvalue()
        sys.stderr = oldStderr
        self.failUnless(
            value.find('raise MockException("MOCK")\nMockException: MOCK') >= 0
        )
예제 #2
0
    def test10_mainTests_notRunningForCallback(self):
        import Queue
        import time

        global myLock

        def setNotRunning(name, timeout):
            global myLock
            myLock.running = False

        d = defer.Deferred()

        def succeeded(result):
            self.failIf(True)

        def failed(result):
            self.failIf(True)

        d.addErrback(failed)
        d.addCallback(succeeded)

        class MockQueue:
            def __init__(qSelf):
                qSelf.count = 1

            def qsize(qSelf):
                return qSelf.count

            def get(qSelf, timeout=1):
                if qSelf.count > 0:
                    qSelf.count = 0
                    return ("Mocky", setNotRunning, 10, d)
                else:
                    raise Queue.Empty

            def empty(qSelf):
                return qSelf.count <= 0

            def put(qSelf, val):
                pass

        class MockLock:
            def __init__(lSelf):
                lSelf.calledReleaseCount = 0

            def release(lSelf):
                lSelf.calledReleaseCount += 1

        log_history.reset()()
        anotherLock = pokerlock.PokerLock(self.parameters)
        anotherLock.q = MockQueue()
        anotherLock.lock = MockLock()
        myLock = anotherLock
        anotherLock.start()
        time.sleep(2)
        self.failUnless(log_history.search('release because not running'),
                        "missing 'release because not running' in output")
        self.assertEquals(anotherLock.running, False)
        self.assertEquals(anotherLock.lock.calledReleaseCount, 1)
예제 #3
0
 def locker2():
     log_history.reset()()
     self.locker2 = pokerlock.PokerLock(self.parameters)
     self.locker2.start()
     d = self.locker2.acquire('lock01', 0)
     d.addCallback(locker2_succeeded)
     d.addErrback(locker2_failed)
     return d
예제 #4
0
    def test11_mainTests_raiseForceRelease(self):
        import Queue
        import time
        class MockException(Exception): pass

        def raiseForceRelease(name, timeout):
            raise MockException()

        def succeeded(result): 
            self.failIf(True)
            
        def failed(result): 
            self.failUnless(issinstance(result, MockException))
            # FIXME: this callback never happens; it should, however.  I
            # am not completely sure why; I assume it's because the
            # reactor.callFromThread() errback call in the main() doesn't
            # get executed before the reactor dies.  OTOH, I don't fully
            # understand the thread/reactor interaction issues .  If
            # someone can figure out and make sure this callback happens,
            # I'd appreciate it.
        d = defer.Deferred()
        d.addErrback(failed)
        d.addCallback(succeeded)

        class  MockQueue:
            def __init__(qSelf):
                qSelf.count = 1
            def qsize(qSelf):
                return qSelf.count
            def get(qSelf, timeout = 1):
                if qSelf.count > 0:
                    qSelf.count = 0
                    return ("Mocky", raiseForceRelease, 10, d)
                else:
                    raise Queue.Empty
            def empty(qSelf):
                return qSelf.count <= 0
            def put(qSelf, val):
                pass
        class  MockLock:
            def release(lSelf):
                raise MockException("MOCKY NO LOCK RELEASE")

        silence_all_messages()
        clear_all_messages()
        anotherLock = pokerlock.PokerLock(self.parameters)

        anotherLock.q = MockQueue()
        anotherLock.lock = MockLock()
        anotherLock.verbose = 6
        anotherLock.start()
        time.sleep(2)

        self.assertEquals(anotherLock.running, True)
        for string in [ 'exception in function Traceback', 'release because exception',
                        'raise MockException("MOCKY NO LOCK RELEASE")']:
            self.failUnless(search_output(string), "missing '%s' in output" % string)
예제 #5
0
 def locker2():
     clear_all_messages()
     self.locker2 = pokerlock.PokerLock(self.parameters)
     self.locker2.start()
     self.locker2.verbose = 6
     d = self.locker2.acquire('lock01', 0)
     d.addCallback(locker2_succeeded)
     d.addErrback(locker2_failed)
     return d
예제 #6
0
 def setUp(self):
     pokerlock.PokerLock.acquire_sleep = 1
     self.parameters = {
         'host': config.test.mysql.host,
         'user': config.test.mysql.root_user.name,
         'password': config.test.mysql.root_user.password
     }
     pokerlock.PokerLock.queue_timeout = 30
     self.locker = pokerlock.PokerLock(self.parameters)
     self.locker.start()
예제 #7
0
    def setUp(self, verbose = 6):
        pokerlock.PokerLock.acquire_sleep = 1
#        verbose = int(os.environ.get('VERBOSE_T', '-1'))
        silence_all_messages()
        if verbose < 0:
            pokerlock.PokerLock.message = lambda self, string: True
        self.parameters = {'host': 'localhost', 'user': '******', 'password': ''}
        pokerlock.PokerLock.queue_timeout = 30
        self.locker = pokerlock.PokerLock(self.parameters)
        if verbose >= 0: self.locker.verbose = verbose;
        self.locker.start()
예제 #8
0
    def lock(self, currency_serial):
        name = self.getLockName(currency_serial)

        if self.verbose > 2: self.message("get lock " + name)
        if self.locks.has_key(name):
            lock = self.locks[name]
            if lock.isAlive():
                create_lock = False
            else:
                lock.close()
                create_lock = True
        else:
            create_lock = True
        if create_lock:
            self.locks[name] = pokerlock.PokerLock(self.db_parameters)
            self.locks[name].verbose = self.verbose
            self.locks[name].start()

        return self.locks[name].acquire(
            name, int(self.parameters.get('acquire_timeout', 60)))