File size: 3,324 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import time
from threading import Thread

import pytest

from ...base import DblEvent


@pytest.mark.unittest
class TestInteractionBaseThreading:
    # noinspection DuplicatedCode
    @pytest.mark.execution_timeout(5.0, method='thread')
    def test_dbl_event_open(self):
        event = DblEvent()
        assert event.is_close()
        assert not event.is_open()

        # Opening test
        _time_1, _time_2 = 0.0, 0.0

        def _run_1_wait_for_open():
            nonlocal _time_1
            event.wait_for_open()
            _time_1 = time.time()

        def _run_2_wait_for_open():
            nonlocal _time_2
            event.wait_for_open()
            _time_2 = time.time()

        _thread_1 = Thread(target=_run_1_wait_for_open)
        _thread_2 = Thread(target=_run_2_wait_for_open)

        _thread_1.start()
        _thread_2.start()

        time.sleep(0.2)
        assert event.is_close()
        assert not event.is_open()
        assert _time_1 == 0.0
        assert _time_2 == 0.0

        time.sleep(0.8)
        event.open()
        _thread_1.join()
        _thread_2.join()

        assert abs(time.time() - _time_1) < 0.3
        assert abs(time.time() - _time_2) < 0.3
        assert not event.is_close()
        assert event.is_open()

        # Closing test
        _time_1, _time_2 = 0.0, 0.0

        def _run_1_wait_for_close():
            nonlocal _time_1
            event.wait_for_close()
            _time_1 = time.time()

        def _run_2_wait_for_close():
            nonlocal _time_2
            event.wait_for_close()
            _time_2 = time.time()

        _thread_1 = Thread(target=_run_1_wait_for_close)
        _thread_2 = Thread(target=_run_2_wait_for_close)

        _thread_1.start()
        _thread_2.start()

        time.sleep(0.2)
        assert not event.is_close()
        assert event.is_open()
        assert _time_1 == 0.0
        assert _time_2 == 0.0

        time.sleep(0.8)
        event.close()
        _thread_1.join()
        _thread_2.join()

        assert abs(time.time() - _time_1) < 0.3
        assert abs(time.time() - _time_2) < 0.3
        assert event.is_close()
        assert not event.is_open()

    # noinspection DuplicatedCode
    @pytest.mark.execution_timeout(5.0, method='thread')
    def test_dbl_event_close(self):
        event = DblEvent(True)
        assert not event.is_close()
        assert event.is_open()

        # Closing test
        _time_1, _time_2 = 0.0, 0.0

        def _run_1_wait_for_close():
            nonlocal _time_1
            event.wait_for_close()
            _time_1 = time.time()

        def _run_2_wait_for_close():
            nonlocal _time_2
            event.wait_for_close()
            _time_2 = time.time()

        _thread_1 = Thread(target=_run_1_wait_for_close)
        _thread_2 = Thread(target=_run_2_wait_for_close)

        _thread_1.start()
        _thread_2.start()

        time.sleep(0.2)
        assert not event.is_close()
        assert event.is_open()
        assert _time_1 == 0.0
        assert _time_2 == 0.0

        time.sleep(0.8)
        event.close()
        _thread_1.join()
        _thread_2.join()

        assert abs(time.time() - _time_1) < 0.3
        assert abs(time.time() - _time_2) < 0.3
        assert event.is_close()
        assert not event.is_open()