-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_callbacks.py
More file actions
178 lines (129 loc) · 3.98 KB
/
Copy pathtest_callbacks.py
File metadata and controls
178 lines (129 loc) · 3.98 KB
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
"""
Callback tests for octproengine
Covers:
- Single input/output callbacks
- Multiple buffers and buffer IDs
- Cleanup / stop without crash
"""
import sys
import time
import numpy as np
try:
import octproengine as ope
print(f"[OK] Successfully imported octproengine version {ope.__version__}")
except ImportError as e:
print(f"[FAIL] Failed to import octproengine: {e}")
sys.exit(1)
# Common parameters for all tests
WIDTH = 512
HEIGHT = 512
BSCAN = 1
DTYPE = ope.DataType.UINT16
def make_processor():
"""Create and initialize a cuda processor with common parameters."""
processor = ope.Processor(ope.Backend.CUDA)
processor.set_input_parameters(WIDTH, HEIGHT, BSCAN, DTYPE)
processor.initialize()
return processor
def test_single_input_output():
"""Test that both input and output callbacks are called once."""
print("TEST 1: Single input and output callbacks")
processor = make_processor()
calls = {"input": 0, "output": 0}
def input_callback(data, buffer_id):
calls["input"] += 1
print(f" Input callback: buffer_id={buffer_id}, shape={data.shape}")
def output_callback(data, buffer_id):
calls["output"] += 1
print(f" Output callback: buffer_id={buffer_id}, shape={data.shape}")
processor.add_input_callback(input_callback)
processor.add_output_callback(output_callback)
buffer = processor.get_next_available_buffer()
buffer.fill(123)
processor.process(buffer)
time.sleep(0.2)
assert calls["input"] == 1, f"Expected 1 input callback, got {calls['input']}"
assert calls["output"] == 1, f"Expected 1 output callback, got {calls['output']}"
print(" [OK] Both input and output callbacks called exactly once")
processor.clear_input_callbacks()
processor.clear_output_callbacks()
processor.stop()
time.sleep(0.1)
print(" PASSED\n")
return True
def test_multiple_output_buffers():
"""Test that output callback sees all buffer IDs in order."""
print("TEST 2: Multiple buffers and buffer IDs")
processor = make_processor()
buffer_ids = []
nbuffers = 10
def output_callback(data, buffer_id):
buffer_ids.append(buffer_id)
print(f" Output callback: buffer_id={buffer_id}")
processor.add_output_callback(output_callback)
for i in range(nbuffers):
buffer = processor.get_next_available_buffer()
buffer.fill(i)
processor.process(buffer)
print(f" Processed buffer {i + 1}/{nbuffers}")
time.sleep(0.5)
assert len(buffer_ids) == nbuffers, f"Expected {nbuffers} callbacks, got {len(buffer_ids)}"
expected_ids = list(range(nbuffers))
assert buffer_ids == expected_ids, f"Expected IDs {expected_ids}, got {buffer_ids}"
print(f" [OK] Received all {nbuffers} buffer IDs in order")
processor.clear_output_callbacks()
processor.stop()
time.sleep(0.1)
print(" PASSED\n")
return True
def test_cleanup_no_crash():
"""Test that clearing callbacks and stopping does not crash."""
print("TEST 3: Cleanup behavior")
processor = make_processor()
def callback(data, buffer_id):
print(f" Callback: buffer_id={buffer_id}")
processor.add_output_callback(callback)
buffer = processor.get_next_available_buffer()
buffer.fill(999)
processor.process(buffer)
time.sleep(0.2)
print(" Clearing callbacks...")
processor.clear_output_callbacks()
time.sleep(0.1)
print(" Stopping processor...")
processor.stop()
time.sleep(0.1)
print(" [OK] Cleanup completed without crash")
print(" PASSED\n")
return True
def main():
print("=" * 60)
print("Callback Python Tests")
print("=" * 60)
print()
tests = [
test_single_input_output,
test_multiple_output_buffers,
test_cleanup_no_crash,
]
passed = 0
total = len(tests)
for test in tests:
try:
if test():
passed += 1
except Exception as e:
print(f" [FAIL] {test.__name__}: {e}")
import traceback
traceback.print_exc()
print()
print("=" * 60)
print(f"RESULTS: {passed}/{total} tests passed")
if passed == total:
print("[OK] ALL TESTS PASSED!")
else:
print("[FAIL] SOME TESTS FAILED!")
print("=" * 60)
return 0 if passed == total else 1
if __name__ == "__main__":
sys.exit(main())