cluster_test.py 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import parl
from parl.remote.master import Master
from parl.remote.worker import Worker
import time
import threading
from parl.remote.client import disconnect
B
Bo Zhou 已提交
22 23
from parl.remote import exceptions
import subprocess
24
from parl.utils import logger
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


@parl.remote_class
class Actor(object):
    def __init__(self, arg1=None, arg2=None):
        self.arg1 = arg1
        self.arg2 = arg2

    def get_arg1(self):
        return self.arg1

    def get_arg2(self):
        return self.arg2

    def set_arg1(self, value):
        self.arg1 = value

    def set_arg2(self, value):
        self.arg2 = value

    def add_one(self, value):
        value += 1
        return value

    def add(self, x, y):
        time.sleep(3)
        return x + y

    def will_raise_exception_func(self):
        x = 1 / 0


B
Bo Zhou 已提交
57 58 59 60 61
class TestCluster(unittest.TestCase):
    def tearDown(self):
        disconnect()

    def test_actor_exception(self):
62 63
        logger.info("running:test_actor_exception")
        master = Master(port=8235)
64 65
        th = threading.Thread(target=master.run)
        th.start()
66
        time.sleep(3)
67
        worker1 = Worker('localhost:8235', 1)
68 69 70 71
        for _ in range(3):
            if master.cpu_num == 1:
                break
            time.sleep(10)
B
Bo Zhou 已提交
72
        self.assertEqual(1, master.cpu_num)
73 74 75
        logger.info("running:test_actor_exception: 0")
        parl.connect('localhost:8235')
        logger.info("running:test_actor_exception: 1")
76

B
Bo Zhou 已提交
77 78
        with self.assertRaises(exceptions.RemoteError):
            actor = Actor(abcd='a bug')
79
        logger.info("running:test_actor_exception: 2")
B
Bo Zhou 已提交
80 81

        actor2 = Actor()
82 83 84 85
        for _ in range(3):
            if master.cpu_num == 0:
                break
            time.sleep(10)
B
Bo Zhou 已提交
86 87 88 89 90 91
        self.assertEqual(actor2.add_one(1), 2)
        self.assertEqual(0, master.cpu_num)

        master.exit()
        worker1.exit()

92 93 94
    def test_actor_exception_2(self):
        logger.info("running: test_actor_exception_2")
        master = Master(port=8236)
B
Bo Zhou 已提交
95 96
        th = threading.Thread(target=master.run)
        th.start()
97
        time.sleep(3)
98
        worker1 = Worker('localhost:8236', 1)
B
Bo Zhou 已提交
99
        self.assertEqual(1, master.cpu_num)
100
        parl.connect('localhost:8236')
B
Bo Zhou 已提交
101 102 103 104 105 106
        actor = Actor()
        try:
            actor.will_raise_exception_func()
        except:
            pass
        actor2 = Actor()
107 108 109 110
        for _ in range(5):
            if master.cpu_num == 0:
                break
            time.sleep(10)
B
Bo Zhou 已提交
111 112 113 114 115 116 117 118
        self.assertEqual(actor2.add_one(1), 2)
        self.assertEqual(0, master.cpu_num)
        del actor
        del actor2
        worker1.exit()
        master.exit()

    def test_reset_actor(self):
119
        logger.info("running: test_reset_actor")
B
Bo Zhou 已提交
120
        # start the master
121
        master = Master(port=8237)
B
Bo Zhou 已提交
122 123
        th = threading.Thread(target=master.run)
        th.start()
124
        time.sleep(3)
B
Bo Zhou 已提交
125

126 127
        worker1 = Worker('localhost:8237', 4)
        parl.connect('localhost:8237')
128
        for _ in range(10):
129 130 131
            actor = Actor()
            ret = actor.add_one(1)
            self.assertEqual(ret, 2)
B
Bo Zhou 已提交
132
        del actor
133 134 135 136 137 138

        for _ in range(10):
            if master.cpu_num == 4:
                break
            time.sleep(10)

B
Bo Zhou 已提交
139
        self.assertEqual(master.cpu_num, 4)
140 141 142 143
        worker1.exit()
        master.exit()

    def test_add_worker(self):
144 145
        logger.info("running: test_add_worker")
        master = Master(port=8234)
146 147 148
        th = threading.Thread(target=master.run)
        th.start()
        time.sleep(1)
149

150
        worker1 = Worker('localhost:8234', 4)
151 152 153 154
        for _ in range(3):
            if master.cpu_num == 4:
                break
            time.sleep(10)
155
        self.assertEqual(master.cpu_num, 4)
156

157
        worker2 = Worker('localhost:8234', 4)
158 159 160 161
        for _ in range(3):
            if master.cpu_num == 8:
                break
            time.sleep(10)
162 163 164
        self.assertEqual(master.cpu_num, 8)

        worker2.exit()
165 166 167 168 169

        for _ in range(10):
            if master.cpu_num == 4:
                break
            time.sleep(10)
170 171 172
        self.assertEqual(master.cpu_num, 4)

        master.exit()
B
Bo Zhou 已提交
173
        worker1.exit()
174 175 176 177


if __name__ == '__main__':
    unittest.main()