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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =====================================================
|
| 2 |
+
# Apckeyl Framework
|
| 3 |
+
# Version 1.0
|
| 4 |
+
# test_dispatcher.py
|
| 5 |
+
# =====================================================
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
Tests for Apckeyl Dispatcher.
|
| 9 |
+
|
| 10 |
+
Version 1.0
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from task_manager import TaskManager
|
| 14 |
+
from dispatcher import Dispatcher
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# =====================================================
|
| 18 |
+
# Test 1 — Dispatcher Exists
|
| 19 |
+
# =====================================================
|
| 20 |
+
|
| 21 |
+
def test_dispatcher_exists():
|
| 22 |
+
|
| 23 |
+
manager = TaskManager()
|
| 24 |
+
|
| 25 |
+
dispatcher = Dispatcher(
|
| 26 |
+
task_manager=manager
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
assert dispatcher is not None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# =====================================================
|
| 33 |
+
# Test 2 — Prepare Dispatch
|
| 34 |
+
# =====================================================
|
| 35 |
+
|
| 36 |
+
def test_prepare_dispatch():
|
| 37 |
+
|
| 38 |
+
manager = TaskManager()
|
| 39 |
+
|
| 40 |
+
dispatcher = Dispatcher(
|
| 41 |
+
task_manager=manager
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
task = manager.create_task(
|
| 45 |
+
task_type="image_upscale"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
request = dispatcher.prepare_dispatch(
|
| 49 |
+
task["task_id"]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
assert request is not None
|
| 53 |
+
|
| 54 |
+
assert request["task_id"] == (
|
| 55 |
+
task["task_id"]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
assert request["task_type"] == (
|
| 59 |
+
"image_upscale"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
assert request["module_id"] == (
|
| 63 |
+
"realesrgan"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# =====================================================
|
| 68 |
+
# Test 3 — Status Becomes Dispatched
|
| 69 |
+
# =====================================================
|
| 70 |
+
|
| 71 |
+
def test_dispatch_status():
|
| 72 |
+
|
| 73 |
+
manager = TaskManager()
|
| 74 |
+
|
| 75 |
+
dispatcher = Dispatcher(
|
| 76 |
+
task_manager=manager
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
task = manager.create_task(
|
| 80 |
+
task_type="image_upscale"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
dispatcher.prepare_dispatch(
|
| 84 |
+
task["task_id"]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
updated_task = manager.get_task(
|
| 88 |
+
task["task_id"]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
assert updated_task["status"] == (
|
| 92 |
+
"dispatched"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# =====================================================
|
| 97 |
+
# Test 4 — Payload Preserved
|
| 98 |
+
# =====================================================
|
| 99 |
+
|
| 100 |
+
def test_payload_preserved():
|
| 101 |
+
|
| 102 |
+
manager = TaskManager()
|
| 103 |
+
|
| 104 |
+
dispatcher = Dispatcher(
|
| 105 |
+
task_manager=manager
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
payload = {
|
| 109 |
+
|
| 110 |
+
"filename": "test.png",
|
| 111 |
+
|
| 112 |
+
"scale": 4,
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
task = manager.create_task(
|
| 116 |
+
|
| 117 |
+
task_type="image_upscale",
|
| 118 |
+
|
| 119 |
+
payload=payload,
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
request = dispatcher.prepare_dispatch(
|
| 123 |
+
task["task_id"]
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
assert request["payload"] == (
|
| 127 |
+
payload
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# =====================================================
|
| 132 |
+
# Test 5 — Unknown Task Rejected
|
| 133 |
+
# =====================================================
|
| 134 |
+
|
| 135 |
+
def test_unknown_task_rejected():
|
| 136 |
+
|
| 137 |
+
manager = TaskManager()
|
| 138 |
+
|
| 139 |
+
dispatcher = Dispatcher(
|
| 140 |
+
task_manager=manager
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
try:
|
| 144 |
+
|
| 145 |
+
dispatcher.prepare_dispatch(
|
| 146 |
+
"task_unknown"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
except KeyError:
|
| 150 |
+
|
| 151 |
+
return
|
| 152 |
+
|
| 153 |
+
raise AssertionError(
|
| 154 |
+
"Unknown task was not rejected"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# =====================================================
|
| 159 |
+
# Test 6 — Invalid Status Rejected
|
| 160 |
+
# =====================================================
|
| 161 |
+
|
| 162 |
+
def test_invalid_status_rejected():
|
| 163 |
+
|
| 164 |
+
manager = TaskManager()
|
| 165 |
+
|
| 166 |
+
dispatcher = Dispatcher(
|
| 167 |
+
task_manager=manager
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
task = manager.create_task(
|
| 171 |
+
task_type="image_upscale"
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
manager.update_status(
|
| 175 |
+
task["task_id"],
|
| 176 |
+
"processing"
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
try:
|
| 180 |
+
|
| 181 |
+
dispatcher.prepare_dispatch(
|
| 182 |
+
task["task_id"]
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
except RuntimeError:
|
| 186 |
+
|
| 187 |
+
return
|
| 188 |
+
|
| 189 |
+
raise AssertionError(
|
| 190 |
+
"Invalid task status was not rejected"
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# =====================================================
|
| 195 |
+
# Test 7 — Queued Task Can Be Dispatched
|
| 196 |
+
# =====================================================
|
| 197 |
+
|
| 198 |
+
def test_queued_task_dispatch():
|
| 199 |
+
|
| 200 |
+
manager = TaskManager()
|
| 201 |
+
|
| 202 |
+
dispatcher = Dispatcher(
|
| 203 |
+
task_manager=manager
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
task = manager.create_task(
|
| 207 |
+
task_type="image_upscale"
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
manager.update_status(
|
| 211 |
+
task["task_id"],
|
| 212 |
+
"queued"
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
request = dispatcher.prepare_dispatch(
|
| 216 |
+
task["task_id"]
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
assert request["task_id"] == (
|
| 220 |
+
task["task_id"]
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
updated_task = manager.get_task(
|
| 224 |
+
task["task_id"]
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
assert updated_task["status"] == (
|
| 228 |
+
"dispatched"
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
# =====================================================
|
| 233 |
+
# Test Runner
|
| 234 |
+
# =====================================================
|
| 235 |
+
|
| 236 |
+
def main():
|
| 237 |
+
|
| 238 |
+
tests = [
|
| 239 |
+
|
| 240 |
+
test_dispatcher_exists,
|
| 241 |
+
|
| 242 |
+
test_prepare_dispatch,
|
| 243 |
+
|
| 244 |
+
test_dispatch_status,
|
| 245 |
+
|
| 246 |
+
test_payload_preserved,
|
| 247 |
+
|
| 248 |
+
test_unknown_task_rejected,
|
| 249 |
+
|
| 250 |
+
test_invalid_status_rejected,
|
| 251 |
+
|
| 252 |
+
test_queued_task_dispatch,
|
| 253 |
+
]
|
| 254 |
+
|
| 255 |
+
passed = 0
|
| 256 |
+
|
| 257 |
+
failed = 0
|
| 258 |
+
|
| 259 |
+
print(
|
| 260 |
+
"===================================="
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
print(
|
| 264 |
+
"Apckeyl Dispatcher Test"
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
print(
|
| 268 |
+
"Version 1.0"
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
print(
|
| 272 |
+
"===================================="
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
for test in tests:
|
| 276 |
+
|
| 277 |
+
try:
|
| 278 |
+
|
| 279 |
+
test()
|
| 280 |
+
|
| 281 |
+
print(
|
| 282 |
+
f"PASS: {test.__name__}"
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
passed += 1
|
| 286 |
+
|
| 287 |
+
except Exception as error:
|
| 288 |
+
|
| 289 |
+
print(
|
| 290 |
+
f"FAIL: {test.__name__}"
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
print(
|
| 294 |
+
f"ERROR: {error}"
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
failed += 1
|
| 298 |
+
|
| 299 |
+
print(
|
| 300 |
+
"===================================="
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
print(
|
| 304 |
+
f"PASSED: {passed}"
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
print(
|
| 308 |
+
f"FAILED: {failed}"
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
print(
|
| 312 |
+
"===================================="
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
if failed > 0:
|
| 316 |
+
|
| 317 |
+
raise RuntimeError(
|
| 318 |
+
"Dispatcher Test FAILED"
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
print(
|
| 322 |
+
"DISPATCHER TEST: PASSED"
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
# =====================================================
|
| 327 |
+
# Entry Point
|
| 328 |
+
# =====================================================
|
| 329 |
+
|
| 330 |
+
if __name__ == "__main__":
|
| 331 |
+
|
| 332 |
+
main()
|