File: test_locks.py

package info (click to toggle)
python-nexusformat 1.0.6-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: python: 5,791; makefile: 5; sh: 1
file content (178 lines) | stat: -rw-r--r-- 4,102 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import time

import pytest
from nexusformat.nexus.tree import (NeXusError, NXentry, NXLock, NXroot,
                                    nxload, nxsetconfig, text)


def test_lock_creation(tmpdir, field4):

    nxsetconfig(lock=0)
    filename = os.path.join(tmpdir, "file1.nxs")
    root = NXroot(NXentry(field4))
    root.save(filename)

    assert root.nxfile.lock is not None

    root.nxfile.acquire_lock()

    assert not root.nxfile.locked
    assert not root.nxfile.is_locked()

    root.nxfile.release_lock()

    assert not root.nxfile.locked
    assert not root.nxfile.is_locked()

    root.nxfile.lock = True
    root.nxfile.acquire_lock()

    assert root.nxfile.locked
    assert root.nxfile.is_locked()
    assert root.nxfile.lock.timeout == 10

    root["entry/f1"] = "b"

    assert root["entry/f1"] == "b"

    assert not root.nxfile.locked
    assert not root.nxfile.is_locked()


def test_locked_assignments(tmpdir, field4):

    filename = os.path.join(tmpdir, "file1.nxs")
    root = NXroot(NXentry(field4))
    root.save(filename)

    assert root.nxfile.mtime == os.path.getmtime(filename)

    original_id = id(root.nxfile)
    originalmtime = root.mtime

    root["entry/f1"] = "b"

    assert root["entry/f1"] == "b"

    root.nxfile.lock = 10

    assert isinstance(root.nxfile.lock, NXLock)
    assert root.nxfile.lock.timeout == 10

    time.sleep(0.1)

    root["entry/f1"] = "c"

    assert root["entry/f1"] == "c"
    assert id(root.nxfile) == original_id
    assert root.mtime > originalmtime


def test_lock_interactions(tmpdir, field4):

    filename = os.path.join(tmpdir, "file1.nxs")
    root = NXroot(NXentry(field4))
    root.save(filename)

    assert not root.nxfile.is_open()

    root1 = nxload(filename, mode="rw")
    root2 = nxload(filename, mode="r")

    time.sleep(0.1)

    root1.nxfile.lock = 10
    root1["entry/f1"] = "b"

    assert root1.mtime > root2.mtime


def test_lock_defaults(tmpdir, field4):

    nxsetconfig(lock=20)
    filename = os.path.join(tmpdir, "file1.nxs")
    root = NXroot(NXentry(field4))
    root.save(filename, "w")

    with root.nxfile as f:

        assert isinstance(root.nxfile.lock, NXLock)
        assert root.nxfile.lock.timeout == 20
        assert root.nxfile.locked
        assert root.nxfile.is_locked()

    assert not root.nxfile.locked
    assert not root.nxfile.is_locked()

    nxsetconfig(lock=0)
    root = NXroot(NXentry(field4))
    root.save(filename, "w")

    with root.nxfile as f:

        assert not root.nxfile.locked
        assert not root.nxfile.is_locked()


def test_nested_locks(tmpdir, field4):

    filename = os.path.join(tmpdir, "file1.nxs")
    root = NXroot(NXentry(field4))
    root.save(filename, "w")
    root.nxfile.lock = True

    assert isinstance(root.nxfile.lock, NXLock)

    with root.nxfile:

        assert root.nxfile.locked
        assert root.nxfile.is_locked()

        root["entry/f1"] = "b"

        assert text(root.nxfile["entry/f1"][()]) == "b"

        with root.nxfile:

            root["entry/f1"] = "c"

            assert text(root.nxfile["entry/f1"][()]) == "c"
            assert root.nxfile.locked
            assert root.nxfile.is_locked()

        assert root.nxfile.locked
        assert root.nxfile.is_locked()

    assert not root.nxfile.locked
    assert not root.nxfile.is_locked()


def test_stale_locks(tmpdir):

    nxsetconfig(lock=2, lockexpiry=3600)
    filename = os.path.join(tmpdir, "file1.nxs")
    root = NXroot(NXentry())
    root.save(filename, "w")

    with open(root.nxfile.lock_file, 'w'):
        os.utime(root.nxfile.lock_file, None)

    assert os.path.exists(root.nxfile.lock_file)

    with pytest.raises(NeXusError):
        root.nxfile.acquire_lock()

    assert not root.nxfile.locked
    assert root.nxfile.is_locked()

    stat = os.stat(root.nxfile.lock_file)
    os.utime(root.nxfile.lock_file, (stat.st_atime, stat.st_mtime - 10000))

    with root.nxfile:

        assert root.nxfile.locked
        assert root.nxfile.is_locked()

    assert not root.nxfile.locked
    assert not root.nxfile.is_locked()