| 83 | |
| 84 | |
| 85 | def test_pidfilelock(): |
| 86 | lock_file = 'pidfile.lock' |
| 87 | # there should be no info |
| 88 | lock = library.python.filelock.PidFileLock(lock_file) |
| 89 | assert lock.info.pid == 0 |
| 90 | assert lock.info.time == 0 |
| 91 | |
| 92 | with library.python.filelock.PidFileLock(lock_file) as lock: |
| 93 | assert lock.info.pid == os.getpid() |
| 94 | assert lock.info.time <= time.time() |
| 95 | assert lock.info.time > time.time() - 2 |
| 96 | |
| 97 | newlock = library.python.filelock.PidFileLock(lock_file) |
| 98 | # info shouldn't require locking |
| 99 | assert newlock.info.pid == os.getpid() |
| 100 | assert not newlock.acquire(blocking=False) |
| 101 | |
| 102 | newlock = library.python.filelock.PidFileLock(lock_file) |
| 103 | # info is still accessible |
| 104 | assert newlock.info.pid == os.getpid() |
| 105 | t = newlock.info.time |
| 106 | # info is updated |
| 107 | time.sleep(1) |
| 108 | with newlock as lock: |
| 109 | assert lock.info.time > t |
| 110 | |
| 111 | |
| 112 | def _try_acquire_pidlock(lock_file, out_file, lock_pid=None): |