From 62ce868c374d15941881ddf43c38f8808f5407f0 Mon Sep 17 00:00:00 2001 From: runzhi214 Date: Wed, 7 Dec 2022 17:25:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=8F=AF=E7=94=A8=E7=9A=84te?= =?UTF-8?q?st=E7=B1=BB=EF=BC=9A=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=BD=BF?= =?UTF-8?q?=E7=94=A8Nested=20Class=20=E5=92=8C=20Nested=20Function?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E8=A2=ABpickle=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82=E6=B5=8B=E8=AF=95=E5=85=A8=E9=83=A8=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E3=80=82test=5Flist=5Fnaming=5Finstance=5Fonline=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E9=85=8D=E5=90=88=E6=9C=AC=E5=9C=B0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=EF=BC=88=E5=AE=9E=E9=99=85=E5=B7=B2=E7=BB=8F=E6=88=90=E5=8A=9F?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FIX TO ISSUE https://github.com/nacos-group/nacos-sdk-python/issues/124 Info to https://github.com/nacos-group/nacos-sdk-python/pull/125 --- test/client_test.py | 94 +++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/test/client_test.py b/test/client_test.py index 3d25daa..8dc5fbe 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -11,11 +11,10 @@ import time import shutil -SERVER_1 = "100.69.207.65" +SERVER_1 = "192.168.0.104" SERVER_ADDRESSES = "%s:8848, 100.69.207.66:8848" % SERVER_1 -SERVER_ADDRESSES = "127.0.0.1:8848" -NAMESPACE = "6cface1f-2f1b-4744-a59d-fd818b91a799" -NAMESPACE = "" +SERVER_ADDRESSES = "192.168.0.104:8848" +NAMESPACE = "28162559-3344-4bec-adc2-cc7dc8fa13f2" # Set the following values if authentication mode is enabled on the server USERNAME = None @@ -25,6 +24,27 @@ # Set the following option if http requests need through by proxy # client.set_options(proxies={"http":"192.168.56.1:809"}) + +class ShareFakeWatcher: + content = None + count = 0 + + +def test_cb_fake_watcher(args): + print(args) + ShareFakeWatcher.count += 1 + ShareFakeWatcher.content = args["content"] + + +class ShareLongPulling: + content = None + + +def cb_long_pulling(x): + ShareLongPulling.content = x["content"] + print(ShareLongPulling.content) + + class TestClient(unittest.TestCase): def test_get_server(self): self.assertEqual(client.get_server(), (SERVER_1, 8848)) @@ -57,53 +77,44 @@ def test_fake_watcher(self): d = "test" g = "DEFAULT_GROUP" - class Share: - content = None - count = 0 - cache_key = "+".join([d, g, NAMESPACE]) - def test_cb(args): - print(args) - Share.count += 1 - Share.content = args["content"] - - client.add_config_watcher(d, g, test_cb) - client.add_config_watcher(d, g, test_cb) - client.add_config_watcher(d, g, test_cb) + client.add_config_watcher(d, g, test_cb_fake_watcher) + client.add_config_watcher(d, g, test_cb_fake_watcher) + client.add_config_watcher(d, g, test_cb_fake_watcher) time.sleep(1) client.notify_queue.put((cache_key, "xxx", "md51")) time.sleep(1) - self.assertEqual(Share.content, "xxx") - self.assertEqual(Share.count, 3) + self.assertEqual(ShareFakeWatcher.content, "xxx") + self.assertEqual(ShareFakeWatcher.count, 3) - client.remove_config_watcher(d, g, test_cb) - Share.count = 0 + client.remove_config_watcher(d, g, test_cb_fake_watcher) + ShareFakeWatcher.count = 0 client.notify_queue.put((cache_key, "yyy", "md52")) time.sleep(1) - self.assertEqual(Share.content, "yyy") - self.assertEqual(Share.count, 2) + self.assertEqual(ShareFakeWatcher.content, "yyy") + self.assertEqual(ShareFakeWatcher.count, 2) - client.remove_config_watcher(d, g, test_cb, True) - Share.count = 0 + client.remove_config_watcher(d, g, test_cb_fake_watcher, True) + ShareFakeWatcher.count = 0 client.notify_queue.put((cache_key, "not effective, no watchers", "md53")) time.sleep(1) - self.assertEqual(Share.content, "yyy") - self.assertEqual(Share.count, 0) + self.assertEqual(ShareFakeWatcher.content, "yyy") + self.assertEqual(ShareFakeWatcher.count, 0) - Share.count = 0 - client.add_config_watcher(d, g, test_cb) + ShareFakeWatcher.count = 0 + client.add_config_watcher(d, g, test_cb_fake_watcher) time.sleep(1) client.notify_queue.put((cache_key, "zzz", "md54")) time.sleep(1) - self.assertEqual(Share.content, "zzz") - self.assertEqual(Share.count, 1) + self.assertEqual(ShareFakeWatcher.content, "zzz") + self.assertEqual(ShareFakeWatcher.count, 1) - Share.count = 0 + ShareFakeWatcher.count = 0 client.notify_queue.put((cache_key, "not effective, md5 no changes", "md54")) time.sleep(1) - self.assertEqual(Share.content, "zzz") - self.assertEqual(Share.count, 0) + self.assertEqual(ShareFakeWatcher.content, "zzz") + self.assertEqual(ShareFakeWatcher.count, 0) def test_long_pulling(self): client2 = nacos.NacosClient(SERVER_ADDRESSES, username=USERNAME, password=PASSWORD) @@ -111,27 +122,20 @@ def test_long_pulling(self): g = "Group1" g2 = "Group2" - class Share: - content = None - - def cb(x): - Share.content = x["content"] - print(Share.content) - client2.publish_config(d, g, "test2") client2.publish_config(d, g2, "test2") time.sleep(0.5) # test common - client2.add_config_watcher(d, g, cb) - client2.add_config_watcher(d, g2, cb) + client2.add_config_watcher(d, g, cb_long_pulling) + client2.add_config_watcher(d, g2, cb_long_pulling) time.sleep(0.5) client2.publish_config(d, g, "test") client2.publish_config(d, g2, "test") time.sleep(1) - self.assertEqual(Share.content, "test") + self.assertEqual(ShareLongPulling.content, "test") client2.publish_config(d, g2, u"test2中文") time.sleep(1) - self.assertEqual(Share.content, u"test2中文") + self.assertEqual(ShareLongPulling.content, u"test2中文") def test_get_from_failover(self): d = "test_fo" @@ -179,7 +183,7 @@ def test_list_naming_instance_offline(self): self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 0) def test_list_naming_instance_online(self): - client.add_naming_instance("test.service", "1.0.0.1", 8080, "testCluster2", 0.1, "{}", True, True) + client.add_naming_instance("test.service", "127.0.0.1", 9876, "testCluster2", 0.1, "{}", True, True) self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 1) def test_get_naming_instance(self):