test_assessor.py 2.42 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3
4
5
6
7

from io import BytesIO
import json
from unittest import TestCase, main

8
9
10
11
12
13
from nni.assessor import Assessor, AssessResult
from nni.runtime import msg_dispatcher_base as msg_dispatcher_base
from nni.runtime.msg_dispatcher import MsgDispatcher
from nni.runtime import protocol
from nni.runtime.protocol import CommandType, send, receive

14
15
_trials = []
_end_trials = []
Deshui Yu's avatar
Deshui Yu committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


class NaiveAssessor(Assessor):
    def assess_trial(self, trial_job_id, trial_history):
        _trials.append(trial_job_id)
        if sum(trial_history) % 2 == 0:
            return AssessResult.Good
        else:
            return AssessResult.Bad

    def trial_end(self, trial_job_id, success):
        _end_trials.append((trial_job_id, success))


_in_buf = BytesIO()
_out_buf = BytesIO()

33

Deshui Yu's avatar
Deshui Yu committed
34
35
36
def _reverse_io():
    _in_buf.seek(0)
    _out_buf.seek(0)
37
38
    protocol._out_file = _in_buf
    protocol._in_file = _out_buf
Deshui Yu's avatar
Deshui Yu committed
39

40

Deshui Yu's avatar
Deshui Yu committed
41
42
43
def _restore_io():
    _in_buf.seek(0)
    _out_buf.seek(0)
44
45
    protocol._in_file = _in_buf
    protocol._out_file = _out_buf
Deshui Yu's avatar
Deshui Yu committed
46
47
48
49


class AssessorTestCase(TestCase):
    def test_assessor(self):
50
        pass
Deshui Yu's avatar
Deshui Yu committed
51
        _reverse_io()
chicm-ms's avatar
chicm-ms committed
52
53
54
        send(CommandType.ReportMetricData, '{"trial_job_id":"A","type":"PERIODICAL","sequence":0,"value":"2"}')
        send(CommandType.ReportMetricData, '{"trial_job_id":"B","type":"PERIODICAL","sequence":0,"value":"2"}')
        send(CommandType.ReportMetricData, '{"trial_job_id":"A","type":"PERIODICAL","sequence":1,"value":"3"}')
Deshui Yu's avatar
Deshui Yu committed
55
56
57
58
59
60
        send(CommandType.TrialEnd, '{"trial_job_id":"A","event":"SYS_CANCELED"}')
        send(CommandType.TrialEnd, '{"trial_job_id":"B","event":"SUCCEEDED"}')
        send(CommandType.NewTrialJob, 'null')
        _restore_io()

        assessor = NaiveAssessor()
61
        dispatcher = MsgDispatcher(None, assessor)
62
        msg_dispatcher_base._worker_fast_exit_on_terminate = False
63
64
65
66
67

        dispatcher.run()
        e = dispatcher.worker_exceptions[0]
        self.assertIs(type(e), AssertionError)
        self.assertEqual(e.args[0], 'Unsupported command: CommandType.NewTrialJob')
Deshui Yu's avatar
Deshui Yu committed
68
69
70
71
72
73
74
75
76
77
78
79

        self.assertEqual(_trials, ['A', 'B', 'A'])
        self.assertEqual(_end_trials, [('A', False), ('B', True)])

        _reverse_io()
        command, data = receive()
        self.assertIs(command, CommandType.KillTrialJob)
        self.assertEqual(data, '"A"')
        self.assertEqual(len(_out_buf.read()), 0)


if __name__ == '__main__':
80
    main()