Example #1
0
 def test_lock_path(self):
     inwith = False
     with util.lock_path('test', 15):
         inwith = True
     self.assertTrue(inwith)
     self.assertEqual(self.os_open_calls, [('test', 0)])
     self.assertEqual(self.flock_calls, [(1, util.LOCK_EX | util.LOCK_NB)])
     self.assertEqual(self.sleep_calls, [])
     self.assertEqual(self.os_close_calls, [(1,)])
Example #2
0
    def test_lock_path_other_exception(self):
        flock_calls = []

        def _flock(*args):
            flock_calls.append(args)
            raise IOError('testing')

        util.flock = _flock
        inwith = False
        try:
            with util.lock_path('test', 15):
                inwith = True
        except Exception, err:
            exc = err
Example #3
0
    def test_lock_path_timeout(self):
        flock_calls = []

        def _flock(*args):
            flock_calls.append(args)
            err = IOError('testing')
            err.errno = util.EAGAIN
            raise err

        util.flock = _flock
        inwith = False
        try:
            with util.lock_path('test', 15):
                inwith = True
        except Exception, err:
            exc = err
Example #4
0
    def test_lock_path_time_delay(self):
        flock_calls = []

        def _flock(*args):
            flock_calls.append(args)
            if len(flock_calls) == 1:
                err = IOError('testing')
                err.errno = util.EAGAIN
                raise err

        util.flock = _flock
        inwith = False
        with util.lock_path('test', 15):
            inwith = True
        self.assertTrue(inwith)
        self.assertEqual(self.os_open_calls, [('test', 0)])
        self.assertEqual(flock_calls, [(1, util.LOCK_EX | util.LOCK_NB)] * 2)
        self.assertEqual(self.sleep_calls, [(0.01,)])
        self.assertEqual(self.os_close_calls, [(1,)])
Example #5
0
    def test_lock_path_other_exception(self):
        flock_calls = []

        def _flock(*args):
            flock_calls.append(args)
            raise IOError('testing')

        util.flock = _flock
        inwith = False
        try:
            with util.lock_path('test', 15):
                inwith = True
        except Exception as err:
            exc = err
        self.assertFalse(inwith)
        self.assertTrue(isinstance(exc, IOError))
        self.assertEqual(str(exc), 'testing')
        self.assertEqual(self.os_open_calls, [('test', 0)])
        self.assertEqual(flock_calls, [(1, util.LOCK_EX | util.LOCK_NB)])
        self.assertEqual(self.sleep_calls, [])
        self.assertEqual(self.os_close_calls, [(1,)])
Example #6
0
    def test_lock_path_timeout(self):
        flock_calls = []

        def _flock(*args):
            flock_calls.append(args)
            err = IOError('testing')
            err.errno = util.EAGAIN
            raise err

        util.flock = _flock
        inwith = False
        try:
            with util.lock_path('test', 15):
                inwith = True
        except Exception as err:
            exc = err
        self.assertFalse(inwith)
        self.assertTrue(isinstance(exc, util.LockPathTimeout))
        self.assertEqual(str(exc), "Timeout 15s trying to lock 'test'.")
        self.assertEqual(self.os_open_calls, [('test', 0)])
        self.assertEqual(flock_calls, [(1, util.LOCK_EX | util.LOCK_NB)] * 15)
        self.assertEqual(self.sleep_calls, [(0.01,)] * 15)
        self.assertEqual(self.os_close_calls, [(1,)])