Debian Codegraph @

tests/experiment/test_workers.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24 import importlib
25 import logging
26
27 import pytest
28 import os
29 import tempfile
30 from time import sleep
31
32 from pymeasure.experiment import Listener, Procedure
33 from pymeasure.experiment.workers import Worker
34 from pymeasure.experiment.results import Results
35 from data.procedure_for_testing import RandomProcedure
36
37 tcp_libs_available = bool(importlib.util.find_spec('cloudpickle')
38 and importlib.util.find_spec('zmq'))
39
40
41 def test_worker_stop():
42 procedure = RandomProcedure()
43 file = tempfile.mktemp()
44 results = Results(procedure, file)
45 worker = Worker(results)
46 worker.start()
47 worker.stop()
48 assert worker.should_stop()
49 worker.join()
50
51
52 def test_worker_finish():
53 procedure = RandomProcedure()
54 procedure.iterations = 100
55 procedure.delay = 0.001
56 file = tempfile.mktemp()
57 results = Results(procedure, file)
58 worker = Worker(results)
59 worker.start()
60 worker.join(timeout=20.0)
61
62 assert not worker.is_alive()
63
64 new_results = Results.load(file, procedure_class=RandomProcedure)
65 assert new_results.data.shape == (100, 2)
66
67
68 def test_worker_closes_file_after_finishing():
69 procedure = RandomProcedure()
70 procedure.iterations = 100
71 procedure.delay = 0.001
72 file = tempfile.mktemp()
73 results = Results(procedure, file)
74 worker = Worker(results)
75 worker.start()
76 worker.join(timeout=20.0)
77
78 # Test if the file has been properly closed by removing the file
79 os.remove(file)
80
81
82 @pytest.mark.skipif(not tcp_libs_available,
83 reason='TCP communication packages not installed')
84 def test_zmq_does_not_crash_worker(caplog):
85 """Check that a ZMQ serialisation usage error does not cause a crash.
86
87 See https://github.com/ralph-group/pymeasure/issues/168
88 """
89 procedure = RandomProcedure()
90 file = tempfile.mktemp()
91 results = Results(procedure, file)
92 # If we define a port here we get ZMQ communication
93 # if cloudpickle is installed
94 worker = Worker(results, port=5888, log_level=logging.DEBUG)
95 worker.start()
96 worker.join(timeout=20.0) # give it enough time to finish the procedure
97 assert procedure.status == procedure.FINISHED
98 del worker # make sure to clean up, reduce the possibility of test
99 # dependencies via left-over sockets
100
101
102 @pytest.mark.skipif(not tcp_libs_available,
103 reason='TCP communication packages not installed')
104 def test_zmq_topic_filtering_works(caplog):
105
106 class ThreeEmitsProcedure(Procedure):
107 def execute(self):
108 self.emit('results', 'Data 1')
109 self.emit('progress', 33)
110 self.emit('results', 'Data 2')
111 self.emit('progress', 66)
112 self.emit('results', 'Data 3')
113 self.emit('progress', 99)
114
115 procedure = ThreeEmitsProcedure()
116 file = tempfile.mktemp()
117 results = Results(procedure, file)
118 received = []
119 worker = Worker(results, port=5888, log_level=logging.DEBUG)
120 listener = Listener(port=5888, topic='results', timeout=4.0)
121 sleep(4.0) # leave time for subscriber and publisher to establish a connection
122 worker.start()
123 while True:
124 if not listener.message_waiting():
125 break
126 topic, record = listener.receive()
127 received.append((topic, record))
128 worker.join(timeout=20.0) # give it enough time to finish the procedure
129 assert procedure.status == procedure.FINISHED
130 assert len(received) == 3
131 assert all([item[0] == 'results' for item in received])