test_locks.py 1.42 KB
Newer Older
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_locks
----------

Tests for gms_preprocessing.misc.locks
"""

import unittest

from gms_preprocessing import set_config
from gms_preprocessing.misc.locks import MultiSlotLock, ProcessLock, AlreadyAcquired

from . import db_host


class Test_MultiSlotLock(unittest.TestCase):
    def setUp(self):
        self.config = set_config(job_ID=26186196, db_host=db_host, reset_status=True)

    def test_acquire_release(self):
        msl = MultiSlotLock('unittest', allowed_threads=15)
        msl.acquire()

        with self.assertRaises(AlreadyAcquired):
            msl.acquire()

        msl.release()

        self.failIf(True in [i.startswith('unittest') for i in msl.existing_locks])

    def test_with_statement(self):
        with MultiSlotLock('unittest', allowed_threads=15) as lock:
            self.assertNotEquals(lock, None)


class Test_ProcessLock(unittest.TestCase):
    def setUp(self):
        self.config = set_config(job_ID=26186196, db_host=db_host, reset_status=True)

    def test_acquire_release(self):
        msl = ProcessLock(processes=15)
        msl.acquire()

        with self.assertRaises(AlreadyAcquired):
            msl.acquire()

        msl.release()

        self.failIf(True in [i.startswith('ProcessLock') for i in msl.existing_locks])

    def test_with_statement(self):
        with ProcessLock(processes=15) as lock:
            self.assertNotEquals(lock, None)