Debian Codegraph @

test/parallel_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2016-2020 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 import threading
17 import multiprocessing
18
19 from pyocd.core.helpers import ConnectHelper
20 from pyocd.probe.pydapaccess import DAPAccess
21
22 from test_util import run_in_parallel
23
24 def run_in_processes(function, args_list):
25 """Create and run a processes in parallel for each element in args_list
26
27 Wait until all processes finish executing. Throw an exception if an
28 exception occurred on any of the processes.
29 """
30 process_list = []
31 for args in args_list:
32 process = multiprocessing.Process(target=function, args=args)
33 process.start()
34 process_list.append(process)
35
36 error_during_run = False
37 for process in process_list:
38 process.join()
39 if process.exitcode != 0:
40 error_during_run = True
41 if error_during_run:
42 raise Exception("Running in process failed")
43
44
45 def list_boards(id_list):
46 """List all connected DAPLink boards repeatedly
47
48 Assert that they are the same as the id list passed in.
49 """
50 for _ in range(0, 20):
51 device_list = DAPAccess.get_connected_devices()
52 found_id_list = [device.get_unique_id() for device in device_list]
53 found_id_list.sort()
54 assert id_list == found_id_list, "Expected %s, got %s" % \
55 (id_list, found_id_list)
56
57
58 def search_and_lock(board_id):
59 """Repeatedly lock a board with the given ID"""
60 for _ in range(0, 20):
61 device = DAPAccess.get_device(board_id)
62 device.open()
63 device.close()
64 with ConnectHelper.session_with_chosen_probe(unique_id=board_id):
65 pass
66
67
68 def open_already_opened(board_id):
69 """Open a device that is already open to verify it gives an error"""
70 device = DAPAccess.get_device(board_id)
71 try:
72 device.open()
73 assert False
74 except DAPAccess.DeviceError:
75 pass
76
77
78 def parallel_test():
79 """Test that devices can be found and opened in parallel"""
80 device_list = DAPAccess.get_connected_devices()
81 id_list = [device.get_unique_id() for device in device_list]
82 id_list.sort()
83
84 if len(id_list) < 2:
85 print("Need at least 2 boards to run the parallel test")
86 exit(-1)
87
88 # Goal of this file is to test that:
89 # -The process of listing available boards does not interfere
90 # with other processes enumerating, opening, or using boards
91 # -Opening and using a board does not interfere with another process
92 # processes which is enumerating, opening, or using boards as
93 # long as that is not the current board
94
95 print("Listing board from multiple threads at the same time")
96 args_list = [(id_list,) for _ in range(5)]
97 run_in_parallel(list_boards, args_list)
98
99 print("Listing board from multiple processes at the same time")
100 run_in_processes(list_boards, args_list)
101
102 print("Opening same board from multiple threads at the same time")
103 device = DAPAccess.get_device(id_list[0])
104 device.open()
105 open_already_opened(id_list[0])
106 args_list = [(id_list[0],) for _ in range(5)]
107 run_in_parallel(open_already_opened, args_list)
108 device.close()
109
110 print("Opening same board from multiple processes at the same time")
111 device = DAPAccess.get_device(id_list[0])
112 device.open()
113 open_already_opened(id_list[0])
114 args_list = [(id_list[0],) for _ in range(5)]
115 run_in_processes(open_already_opened, args_list)
116 device.close()
117
118 print("Opening different boards from different threads")
119 args_list = [(board_id,) for board_id in id_list]
120 run_in_parallel(search_and_lock, args_list)
121
122 print("Opening different boards from different processes")
123 run_in_processes(search_and_lock, args_list)
124
125 print("Test passed")
126
127
128 if __name__ == "__main__":
129 multiprocessing.set_start_method('spawn')
130 parallel_test()