Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

import unittest 

import mock 

import os 

import gc 

import errno 

 

import testlib 

 

import lock 

 

 

class TestLock(unittest.TestCase): 

    @testlib.with_context 

    def test_lock_without_namespace_creates_nil_namespace(self, context): 

        lck = lock.Lock('somename') 

 

        self.assertTrue( 

            os.path.exists( 

                os.path.join(lck.BASE_DIR, '.nil'))) 

 

    @testlib.with_context 

    def test_lock_with_namespace_creates_namespace(self, context): 

        lck = lock.Lock('somename', ns='namespace') 

 

        self.assertTrue( 

            os.path.exists( 

                os.path.join(lck.BASE_DIR, 'namespace'))) 

 

    @testlib.with_context 

    def test_lock_without_namespace_creates_file(self, context): 

        lck = lock.Lock('somename') 

 

        self.assertTrue( 

            os.path.exists( 

                os.path.join(lck.BASE_DIR, '.nil', 'somename'))) 

 

    @testlib.with_context 

    def test_lock_with_namespace_creates_file(self, context): 

        lck = lock.Lock('somename', ns='namespace') 

 

        self.assertTrue( 

            os.path.exists( 

                os.path.join(lck.BASE_DIR, 'namespace', 'somename'))) 

 

    @testlib.with_context 

    def test_lock_file_create_fails_retried(self, context): 

        Lock = create_lock_class_that_fails_to_create_file(1) 

        lck = Lock('somename', ns='namespace') 

 

        self.assertTrue( 

            os.path.exists( 

                os.path.join(lck.BASE_DIR, 'namespace', 'somename'))) 

 

 

def create_lock_class_that_fails_to_create_file(number_of_failures): 

 

    class LockThatFailsToCreateFile(lock.Lock): 

        _failures = number_of_failures 

 

        def _open_lockfile(self): 

            if self._failures > 0: 

                error = IOError('No such file') 

                error.errno = errno.ENOENT 

                self._failures -= 1 

                raise error 

            return lock.Lock._open_lockfile(self) 

 

    return LockThatFailsToCreateFile