|
| 1 | +""" |
| 2 | +Test cluster_estimation_worker process. |
| 3 | +""" |
| 4 | + |
| 5 | +import time |
| 6 | +import multiprocessing as mp |
| 7 | +from typing import List |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from utilities.workers import queue_proxy_wrapper, worker_controller |
| 12 | +from modules.cluster_estimation.cluster_estimation_worker import cluster_estimation_worker |
| 13 | +from modules.detection_in_world import DetectionInWorld |
| 14 | +from modules.object_in_world import ObjectInWorld |
| 15 | + |
| 16 | +MIN_ACTIVATION_THRESHOLD = 3 |
| 17 | +MIN_NEW_POINTS_TO_RUN = 0 |
| 18 | +MAX_NUM_COMPONENTS = 3 |
| 19 | +RANDOM_STATE = 0 |
| 20 | + |
| 21 | + |
| 22 | +def check_output_results(output_queue: queue_proxy_wrapper.QueueProxyWrapper) -> None: |
| 23 | + """ |
| 24 | + Checking if the output from the worker is of the correct type |
| 25 | + """ |
| 26 | + |
| 27 | + while not output_queue.queue.empty(): |
| 28 | + output_results: List[DetectionInWorld] = output_queue.queue.get() |
| 29 | + assert isinstance(output_results, list) |
| 30 | + assert all(isinstance(obj, ObjectInWorld) for obj in output_results) |
| 31 | + |
| 32 | + |
| 33 | +def test_cluster_estimation_worker() -> int: |
| 34 | + """ |
| 35 | + Integration test for cluster estimation worker. |
| 36 | + """ |
| 37 | + |
| 38 | + # Worker and controller setup. |
| 39 | + controller = worker_controller.WorkerController() |
| 40 | + |
| 41 | + mp_manager = mp.Manager() |
| 42 | + input_queue = queue_proxy_wrapper.QueueProxyWrapper(mp_manager) |
| 43 | + output_queue = queue_proxy_wrapper.QueueProxyWrapper(mp_manager) |
| 44 | + |
| 45 | + worker_process = mp.Process( |
| 46 | + target=cluster_estimation_worker, |
| 47 | + args=( |
| 48 | + MIN_ACTIVATION_THRESHOLD, |
| 49 | + MIN_NEW_POINTS_TO_RUN, |
| 50 | + MAX_NUM_COMPONENTS, |
| 51 | + RANDOM_STATE, |
| 52 | + input_queue, |
| 53 | + output_queue, |
| 54 | + controller, |
| 55 | + ), |
| 56 | + ) |
| 57 | + |
| 58 | + # Second test set: 1 clusters |
| 59 | + test_data_1 = [ |
| 60 | + # Landing pad 1 |
| 61 | + DetectionInWorld.create( |
| 62 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 63 | + )[1], |
| 64 | + DetectionInWorld.create( |
| 65 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 66 | + )[1], |
| 67 | + DetectionInWorld.create( |
| 68 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 69 | + )[1], |
| 70 | + DetectionInWorld.create( |
| 71 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 72 | + )[1], |
| 73 | + DetectionInWorld.create( |
| 74 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 75 | + )[1], |
| 76 | + ] |
| 77 | + |
| 78 | + # First test set: 2 clusters |
| 79 | + test_data_2 = [ |
| 80 | + # Landing pad 1 |
| 81 | + DetectionInWorld.create( |
| 82 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 83 | + )[1], |
| 84 | + DetectionInWorld.create( |
| 85 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 86 | + )[1], |
| 87 | + DetectionInWorld.create( |
| 88 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 89 | + )[1], |
| 90 | + DetectionInWorld.create( |
| 91 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 92 | + )[1], |
| 93 | + DetectionInWorld.create( |
| 94 | + np.array([[1, 1], [1, 2], [2, 2], [2, 1]]), np.array([1.5, 1.5]), 1, 0.9 |
| 95 | + )[1], |
| 96 | + # Landing pad 2 |
| 97 | + DetectionInWorld.create( |
| 98 | + np.array([[10, 10], [10, 11], [11, 11], [11, 10]]), np.array([10.5, 10.5]), 1, 0.9 |
| 99 | + )[1], |
| 100 | + DetectionInWorld.create( |
| 101 | + np.array([[10.1, 10.1], [10.1, 11.1], [11.1, 11.1], [11.1, 10.1]]), |
| 102 | + np.array([10.6, 10.6]), |
| 103 | + 1, |
| 104 | + 0.92, |
| 105 | + )[1], |
| 106 | + DetectionInWorld.create( |
| 107 | + np.array([[9.9, 9.9], [9.9, 10.9], [10.9, 10.9], [10.9, 9.9]]), |
| 108 | + np.array([10.4, 10.4]), |
| 109 | + 1, |
| 110 | + 0.88, |
| 111 | + )[1], |
| 112 | + DetectionInWorld.create( |
| 113 | + np.array([[10.2, 10.2], [10.2, 11.2], [11.2, 11.2], [11.2, 10.2]]), |
| 114 | + np.array([10.7, 10.7]), |
| 115 | + 1, |
| 116 | + 0.95, |
| 117 | + )[1], |
| 118 | + DetectionInWorld.create( |
| 119 | + np.array([[10.3, 10.3], [10.3, 11.3], [11.3, 11.3], [11.3, 10.3]]), |
| 120 | + np.array([10.8, 10.8]), |
| 121 | + 1, |
| 122 | + 0.93, |
| 123 | + )[1], |
| 124 | + ] |
| 125 | + |
| 126 | + # Testing with test_data_1 (1 cluster) |
| 127 | + |
| 128 | + input_queue.queue.put(test_data_1) |
| 129 | + worker_process.start() |
| 130 | + time.sleep(1) |
| 131 | + |
| 132 | + check_output_results(output_queue) |
| 133 | + |
| 134 | + time.sleep(1) |
| 135 | + |
| 136 | + # Testing with test_data_2 (2 clusters) |
| 137 | + |
| 138 | + input_queue.queue.put(test_data_2) |
| 139 | + time.sleep(1) |
| 140 | + |
| 141 | + check_output_results(output_queue) |
| 142 | + |
| 143 | + controller.request_exit() |
| 144 | + input_queue.queue.put(None) |
| 145 | + worker_process.join() |
| 146 | + |
| 147 | + return 0 |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + result_main = test_cluster_estimation_worker() |
| 152 | + if result_main < 0: |
| 153 | + print(f"ERROR: Status code: {result_main}") |
| 154 | + |
| 155 | + print("Done!") |
0 commit comments