|
| 1 | +import math |
| 2 | +import time |
| 3 | +import os |
| 4 | + |
| 5 | +import pytest |
| 6 | +from utils import get_result, qmpc |
| 7 | + |
| 8 | +from tests.common import data_id |
| 9 | +from quickmpc import JobStatus |
| 10 | + |
| 11 | +def execute_computation_param(dataIds=[data_id([[1, 2, 3], [4, 5, 6]])], |
| 12 | + src=[1, 2, 3]): |
| 13 | + return (dataIds, src) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize( |
| 17 | + ("param", "expected"), |
| 18 | + [ |
| 19 | + # usually case |
| 20 | + (execute_computation_param(), |
| 21 | + [5, 7, 9]), |
| 22 | +
|
| 23 | + # small table size case |
| 24 | + (execute_computation_param(dataIds=[data_id([[1]])], |
| 25 | + src=[1]), |
| 26 | + [1]), |
| 27 | +
|
| 28 | + # large data case |
| 29 | + (execute_computation_param(dataIds=[data_id([[10**18], [10**18]])], |
| 30 | + src=[1]), |
| 31 | + [2*10**18]), |
| 32 | +
|
| 33 | + # small data case |
| 34 | + (execute_computation_param(dataIds=[data_id([[10**-10], [10**-10]])], |
| 35 | + src=[1]), |
| 36 | + [2*10**-10]), |
| 37 | +
|
| 38 | + # duplicated src case |
| 39 | + (execute_computation_param(src=[1, 2, 2, 3, 3, 3]), |
| 40 | + [5, 7, 7, 9, 9, 9]), |
| 41 | + ] |
| 42 | +) |
| 43 | +def test_get_computation_result(param: tuple, expected: list): |
| 44 | + # 総和計算リクエスト送信 |
| 45 | + res = qmpc.sum(*param) |
| 46 | + assert (res["is_ok"]) |
| 47 | + for i in range(10000): |
| 48 | + time.sleep(i+1) |
| 49 | + get_res = qmpc.get_computation_result(res["job_uuid"]) |
| 50 | + assert (get_res["is_ok"]) |
| 51 | + |
| 52 | + if get_res["results"] is None: |
| 53 | + continue |
| 54 | + # 正しく取得できたか確認 |
| 55 | + for x, y in zip(get_res["results"], expected): |
| 56 | + assert (math.isclose(x, y, abs_tol=0.1)) |
| 57 | + break |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + ("param", "expected"), |
| 62 | + [ |
| 63 | + # usually case |
| 64 | + (execute_computation_param(), |
| 65 | + [5, 7, 9]), |
| 66 | +
|
| 67 | + # small table size case |
| 68 | + (execute_computation_param(dataIds=[data_id([[1]])], |
| 69 | + src=[1]), |
| 70 | + [1]), |
| 71 | +
|
| 72 | + # large data case |
| 73 | + (execute_computation_param(dataIds=[data_id([[10**18], [10**18]])], |
| 74 | + src=[1]), |
| 75 | + [2*10**18]), |
| 76 | +
|
| 77 | + # small data case |
| 78 | + (execute_computation_param(dataIds=[data_id([[10**-10], [10**-10]])], |
| 79 | + src=[1]), |
| 80 | + [2*10**-10]), |
| 81 | +
|
| 82 | + # duplicated src case |
| 83 | + (execute_computation_param(src=[1, 2, 2, 3, 3, 3]), |
| 84 | + [5, 7, 7, 9, 9, 9]), |
| 85 | + ] |
| 86 | +) |
| 87 | +def test_restore(param: tuple, expected: list): |
| 88 | + path = "./result" |
| 89 | + if not os.path.isdir(path): |
| 90 | + os.mkdir(path) |
| 91 | + |
| 92 | + res = qmpc.sum(*param) |
| 93 | + assert (res["is_ok"]) |
| 94 | + for i in range(10000): |
| 95 | + time.sleep(i+1) |
| 96 | + get_res = qmpc.get_computation_result(res["job_uuid"], path) |
| 97 | + assert (get_res["is_ok"]) |
| 98 | + |
| 99 | + all_completed = all([status == JobStatus.COMPLETED |
| 100 | + for status in get_res["statuses"]]) |
| 101 | + |
| 102 | + if not all_completed: |
| 103 | + continue |
| 104 | + res = qmpc.restore(res["job_uuid"], path) |
| 105 | + # 正しく取得できたか確認 |
| 106 | + for x, y in zip(res, expected): |
| 107 | + assert (math.isclose(x, y, abs_tol=0.1)) |
| 108 | + break |
0 commit comments