|  | 
|  | 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | 
|  | 2 | +# | 
|  | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +# you may not use this file except in compliance with the License. | 
|  | 5 | +# You may obtain a copy of the License at | 
|  | 6 | +# | 
|  | 7 | +# http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +# | 
|  | 9 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +# See the License for the specific language governing permissions and | 
|  | 13 | +# limitations under the License. | 
|  | 14 | +import time | 
|  | 15 | +import unittest | 
|  | 16 | +from edl.tests.unittests import etcd_test_base | 
|  | 17 | +from edl.utils import cluster as edl_cluster | 
|  | 18 | +from edl.utils import constants | 
|  | 19 | +from edl.utils import exceptions | 
|  | 20 | +from edl.utils import cluster_watcher | 
|  | 21 | +from edl.utils import pod as edl_pod | 
|  | 22 | + | 
|  | 23 | + | 
|  | 24 | +class TestWatcher(etcd_test_base.EtcdTestBase): | 
|  | 25 | + def setUp(self): | 
|  | 26 | + super(TestWatcher, self).setUp("test_watcher") | 
|  | 27 | + | 
|  | 28 | + def test_watcher_stage_changed(self): | 
|  | 29 | + cluster = edl_cluster.Cluster() | 
|  | 30 | + cluster._stage = "0" | 
|  | 31 | + print("cluster 0 ids:", cluster.to_json(), cluster.get_pods_ids_list()) | 
|  | 32 | + self._etcd.set_server_permanent(constants.ETCD_CLUSTER, | 
|  | 33 | + constants.ETCD_CLUSTER, | 
|  | 34 | + cluster.to_json()) | 
|  | 35 | + watcher = cluster_watcher.Watcher(self._job_env, cluster) | 
|  | 36 | + | 
|  | 37 | + cluster._stage = "1" | 
|  | 38 | + print("cluster 1 ids:", cluster.to_json(), cluster.get_pods_ids_list()) | 
|  | 39 | + self._etcd.set_server_permanent(constants.ETCD_CLUSTER, | 
|  | 40 | + constants.ETCD_CLUSTER, | 
|  | 41 | + cluster.to_json()) | 
|  | 42 | + time.sleep(constants.ETCD_TTL) | 
|  | 43 | + self.assertTrue(watcher.changed) | 
|  | 44 | + | 
|  | 45 | + def test_watch_valid(self): | 
|  | 46 | + try: | 
|  | 47 | + cluster = edl_cluster.Cluster() | 
|  | 48 | + self._etcd.set_server_permanent(constants.ETCD_CLUSTER, | 
|  | 49 | + constants.ETCD_CLUSTER, | 
|  | 50 | + cluster.to_json()) | 
|  | 51 | + watcher = cluster_watcher.Watcher(self._job_env, cluster) | 
|  | 52 | + self._etcd.remove_server(constants.ETCD_CLUSTER, | 
|  | 53 | + constants.ETCD_CLUSTER) | 
|  | 54 | + time.sleep(constants.ETCD_TTL) | 
|  | 55 | + except exceptions.EdlTableError as e: | 
|  | 56 | + pass | 
|  | 57 | + | 
|  | 58 | + def test_watcher_ids_changed(self): | 
|  | 59 | + cluster = edl_cluster.Cluster() | 
|  | 60 | + print("cluster 0 ids:", cluster.to_json(), cluster.get_pods_ids_list()) | 
|  | 61 | + self._etcd.set_server_permanent(constants.ETCD_CLUSTER, | 
|  | 62 | + constants.ETCD_CLUSTER, | 
|  | 63 | + cluster.to_json()) | 
|  | 64 | + watcher = cluster_watcher.Watcher(self._job_env, cluster) | 
|  | 65 | + | 
|  | 66 | + pod = edl_pod.Pod() | 
|  | 67 | + cluster._pods.append(pod) | 
|  | 68 | + print("cluster 1 ids:", cluster.to_json(), cluster.get_pods_ids_list()) | 
|  | 69 | + self._etcd.set_server_permanent(constants.ETCD_CLUSTER, | 
|  | 70 | + constants.ETCD_CLUSTER, | 
|  | 71 | + cluster.to_json()) | 
|  | 72 | + time.sleep(constants.ETCD_TTL) | 
|  | 73 | + self.assertTrue(watcher.changed) | 
|  | 74 | + | 
|  | 75 | + | 
|  | 76 | +if __name__ == '__main__': | 
|  | 77 | + unittest.main() | 
0 commit comments