Coverage for test_mqtt_networking_example.py: 98%
204 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-02-27 14:21 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-02-27 14:21 +0000
1#-----------------------------------------------------------------------------------
2# Basic MQTT Networking system tests
3#
4# When run it uses the following example schematic file:
5# "../configuration_examples/mqtt_networked_example.sig"
6#
7#-----------------------------------------------------------------------------------
9from system_test_harness import *
10import test_object_edit_windows
12#-----------------------------------------------------------------------------------
13# Test the end to end layout with block instruments(2 seperate signalling areas)
14# This tests the publish/subscribe of Block Instruments and track sections
15#-----------------------------------------------------------------------------------
17def test_end_to_end_layout(delay=0.0):
18 print("End to End layout tests - networked block instruments and mirrored track sections")
19 # As we are using networking, we need to introduce an additional delay for messages
20 # to be sent to and received from the MQTT message broker - as we are using a
21 # local broker, a short delay of 100ms should suffice
22 network_delay = 0.1
23 sleep(delay)
24 reset_layout()
25 # Set up a couple of trains to test with
26 sleep(delay)
27 set_sections_occupied(2,12)
28 # Note the two distants are configured as fixed distants (can't be cleared)
29 assert_sections_occupied(2,12)
30 assert_sections_clear(1,3,4,14,13,11)
31 assert_block_section_ahead_not_clear(1,11)
32 assert_signals_locked(1,2,4,11,12,14)
33 assert_signals_unlocked(3,13)
34 # The 1st block instrument should clear signals 11 or 12
35 sleep(delay)
36 set_instrument_occupied(1)
37 sleep(network_delay)
38 assert_signals_locked(1,2,4,11,12,14)
39 assert_signals_unlocked(3,13)
40 assert_block_section_ahead_not_clear(1,11)
41 sleep(delay)
42 set_instrument_clear(1)
43 sleep(network_delay)
44 assert_signals_locked(1,2,4,12,14)
45 assert_signals_unlocked(3,11,13)
46 assert_block_section_ahead_clear(11)
47 assert_block_section_ahead_not_clear(1)
48 sleep(delay)
49 set_fpls_off(11)
50 sleep(delay)
51 set_points_switched(11)
52 sleep(delay)
53 set_fpls_on(11)
54 assert_signals_locked(1,2,4,11,14)
55 assert_signals_unlocked(3,12,13)
56 # Set the first train going
57 sleep(delay)
58 set_signals_off(12,3)
59 sleep(delay)
60 trigger_signals_passed(12)
61 assert_sections_occupied(2,13)
62 assert_sections_clear(1,3,4,14,11,12)
63 sleep(delay)
64 trigger_signals_passed(14)
65 sleep(network_delay)
66 assert_sections_occupied(2,4,14)
67 assert_sections_clear(1,3,11,12,13)
68 sleep(delay)
69 trigger_signals_passed(4)
70 sleep(network_delay)
71 assert_sections_occupied(2,3)
72 assert_sections_clear(1,4,11,12,13,14)
73 sleep(delay)
74 trigger_signals_passed(3)
75 sleep(network_delay)
76 assert_sections_occupied(1,2)
77 assert_sections_clear(3,4,11,12,13,14)
78 # Clear everything back down
79 sleep(delay)
80 set_signals_on(12,3)
81 sleep(delay)
82 set_fpls_off(11)
83 sleep(delay)
84 set_points_normal(11)
85 sleep(delay)
86 set_fpls_on(11)
87 sleep(delay)
88 set_instrument_blocked(1)
89 sleep(network_delay)
90 assert_signals_locked(1,2,4,11,12,14)
91 assert_signals_unlocked(3,13)
92 sleep(network_delay)
93 assert_sections_occupied(1,2)
94 assert_sections_clear(3,4,14,13,11,12)
95 assert_block_section_ahead_not_clear(1,11)
96 assert_signals_locked(1,2,4,11,12,14)
97 assert_signals_unlocked(3,13)
98 # The 2nd block instrument should clear signals 1 or 2
99 sleep(delay)
100 set_instrument_occupied(11)
101 sleep(network_delay)
102 assert_signals_locked(1,2,4,11,12,14)
103 assert_signals_unlocked(3,13)
104 assert_block_section_ahead_not_clear(1,11)
105 sleep(delay)
106 set_instrument_clear(11)
107 sleep(network_delay)
108 assert_signals_locked(2,11,12,4,14)
109 assert_signals_unlocked(1,3,13)
110 assert_block_section_ahead_clear(1)
111 assert_block_section_ahead_not_clear(11)
112 sleep(delay)
113 set_fpls_off(1)
114 sleep(delay)
115 set_points_switched(1)
116 sleep(delay)
117 set_fpls_on(1)
118 assert_signals_locked(11,12,4,14)
119 assert_signals_unlocked(2,3,13)
120 # Set the second train going
121 sleep(delay)
122 set_signals_off(2,13)
123 sleep(delay)
124 trigger_signals_passed(2)
125 assert_sections_occupied(1,3)
126 assert_sections_clear(2,4,14,13,11,12)
127 sleep(delay)
128 trigger_signals_passed(4)
129 sleep(network_delay)
130 assert_sections_occupied(1,4,14)
131 assert_sections_clear(2,3,11,12,13)
132 sleep(delay)
133 trigger_signals_passed(14)
134 sleep(network_delay)
135 assert_sections_occupied(1,13)
136 assert_sections_clear(2,3,4,11,12,14)
137 sleep(delay)
138 trigger_signals_passed(13)
139 sleep(network_delay)
140 assert_sections_occupied(1,11)
141 assert_sections_clear(2,3,4,12,13,14)
142 # Clear everything back down
143 sleep(delay)
144 set_signals_on(2,13)
145 sleep(delay)
146 set_fpls_off(1)
147 sleep(delay)
148 set_points_normal(1)
149 sleep(delay)
150 set_fpls_on(1)
151 sleep(delay)
152 set_instrument_blocked(11)
153 sleep(network_delay)
154 assert_signals_locked(1,2,4,11,12,14)
155 assert_signals_unlocked(3,13)
156 assert_sections_occupied(1,11)
157 assert_sections_clear(2,3,4,14,13,12)
158 assert_block_section_ahead_not_clear(1,11)
159 assert_signals_locked(1,2,4,11,12,14)
160 assert_signals_unlocked(3,13)
161 sleep(delay)
162 set_sections_clear(1,11)
163 return()
165#-----------------------------------------------------------------------------------
166# Test the circular one-way layout (2 seperate signalling areas)
167# This tests the publish/subscribe of signals and track sections
168# Note the use of simulating GPIO triggers rather than generating
169# the normal signal passed events
170#-----------------------------------------------------------------------------------
172def test_circular_layout(delay=0.0):
173 print("Circular layout tests - networked signals ahead and mirrored track sections")
174 # As we are using networking, we need to introduce an additional delay for messages
175 # to be sent to and received from the MQTT message broker - as we are using a
176 # local broker, a short delay of 100ms should suffice
177 # We also need to introduce a delay for triggering of the remote GPIO sensors
178 # as these are configured with a timeout period of 0.1 seconds (this means that
179 # any additional triggers received within the 0.1 seconds will be ignored
180 network_delay = 0.1
181 gpio_trigger_delay = 0.2
182 sleep(delay)
183 reset_layout()
184 sleep(delay)
185 set_signals_off(21,22,31,32)
186 sleep(delay)
187 set_sections_occupied(22)
188 sleep(delay)
189 for repeat in range(5):
190 sleep(delay+network_delay+gpio_trigger_delay)
191 assert_sections_occupied(22)
192 assert_sections_clear(21,23,31,32,33)
193 assert_signals_DANGER(21)
194 assert_signals_CAUTION(32)
195 assert_signals_PRELIM_CAUTION(31)
196 assert_signals_PROCEED(22)
197# trigger_signals_passed(22)
198 simulate_gpio_triggered(5)
199 sleep(delay+network_delay+gpio_trigger_delay)
200 assert_sections_occupied(23,31)
201 assert_sections_clear(21,22,32,33)
202 assert_signals_DANGER(22)
203 assert_signals_CAUTION(21)
204 assert_signals_PRELIM_CAUTION(32)
205 assert_signals_PROCEED(31)
206# trigger_signals_passed(31)
207 simulate_gpio_triggered(6)
208 sleep(delay+network_delay+gpio_trigger_delay)
209 assert_sections_occupied(32)
210 assert_sections_clear(21,22,23,31,33)
211 assert_signals_DANGER(31)
212 assert_signals_CAUTION(22)
213 assert_signals_PRELIM_CAUTION(21)
214 assert_signals_PROCEED(32)
215# trigger_signals_passed(32)
216 simulate_gpio_triggered(12)
217 sleep(delay+network_delay+gpio_trigger_delay)
218 assert_sections_occupied(33,21)
219 assert_sections_clear(22,23,31,32)
220 assert_signals_DANGER(32)
221 assert_signals_CAUTION(31)
222 assert_signals_PRELIM_CAUTION(22)
223 assert_signals_PROCEED(21)
224# trigger_signals_passed(21)
225 simulate_gpio_triggered(4)
226 sleep(delay+network_delay+gpio_trigger_delay)
227 assert_sections_occupied(22)
228 assert_sections_clear(23,31,32,33)
229 assert_signals_DANGER(21)
230 assert_signals_CAUTION(32)
231 assert_signals_PRELIM_CAUTION(31)
232 assert_signals_PROCEED(22)
233 # Revert everything to the default state
234 set_sections_clear(22)
235 sleep(delay)
236 set_signals_on(21,22,31,32)
237 sleep(delay)
238 return()
241######################################################################################################
243def run_all_mqtt_networking_example_tests(delay:float=0.0, shutdown:bool=False):
244 initialise_test_harness(filename="../configuration_examples/mqtt_networked_example.sig")
245 # Edit/save all schematic objects to give confidence that editing doesn't break the layout configuration
246 set_edit_mode()
247 test_object_edit_windows.test_all_object_edit_windows(delay)
248 set_run_mode()
249 test_circular_layout(delay)
250 test_end_to_end_layout(delay)
251 if shutdown: report_results()
253if __name__ == "__main__": 253 ↛ 254line 253 didn't jump to line 254, because the condition on line 253 was never true
254 start_application(lambda:run_all_mqtt_networking_example_tests(delay=0.0, shutdown=True))
256######################################################################################################