Skip to content

Commit 62ce868

Browse files
committed
本地可用的test类:修复了使用Nested Class 和 Nested Function不能被pickle的问题。测试全部通过。test_list_naming_instance_online需要配合本地服务(实际已经成功)
FIX TO ISSUE nacos-group#124 Info to nacos-group#125
1 parent 448a76d commit 62ce868

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

test/client_test.py

+49-45
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
import time
1212
import shutil
1313

14-
SERVER_1 = "100.69.207.65"
14+
SERVER_1 = "192.168.0.104"
1515
SERVER_ADDRESSES = "%s:8848, 100.69.207.66:8848" % SERVER_1
16-
SERVER_ADDRESSES = "127.0.0.1:8848"
17-
NAMESPACE = "6cface1f-2f1b-4744-a59d-fd818b91a799"
18-
NAMESPACE = ""
16+
SERVER_ADDRESSES = "192.168.0.104:8848"
17+
NAMESPACE = "28162559-3344-4bec-adc2-cc7dc8fa13f2"
1918

2019
# Set the following values if authentication mode is enabled on the server
2120
USERNAME = None
@@ -25,6 +24,27 @@
2524
# Set the following option if http requests need through by proxy
2625
# client.set_options(proxies={"http":"192.168.56.1:809"})
2726

27+
28+
class ShareFakeWatcher:
29+
content = None
30+
count = 0
31+
32+
33+
def test_cb_fake_watcher(args):
34+
print(args)
35+
ShareFakeWatcher.count += 1
36+
ShareFakeWatcher.content = args["content"]
37+
38+
39+
class ShareLongPulling:
40+
content = None
41+
42+
43+
def cb_long_pulling(x):
44+
ShareLongPulling.content = x["content"]
45+
print(ShareLongPulling.content)
46+
47+
2848
class TestClient(unittest.TestCase):
2949
def test_get_server(self):
3050
self.assertEqual(client.get_server(), (SERVER_1, 8848))
@@ -57,81 +77,65 @@ def test_fake_watcher(self):
5777
d = "test"
5878
g = "DEFAULT_GROUP"
5979

60-
class Share:
61-
content = None
62-
count = 0
63-
6480
cache_key = "+".join([d, g, NAMESPACE])
6581

66-
def test_cb(args):
67-
print(args)
68-
Share.count += 1
69-
Share.content = args["content"]
70-
71-
client.add_config_watcher(d, g, test_cb)
72-
client.add_config_watcher(d, g, test_cb)
73-
client.add_config_watcher(d, g, test_cb)
82+
client.add_config_watcher(d, g, test_cb_fake_watcher)
83+
client.add_config_watcher(d, g, test_cb_fake_watcher)
84+
client.add_config_watcher(d, g, test_cb_fake_watcher)
7485
time.sleep(1)
7586
client.notify_queue.put((cache_key, "xxx", "md51"))
7687
time.sleep(1)
77-
self.assertEqual(Share.content, "xxx")
78-
self.assertEqual(Share.count, 3)
88+
self.assertEqual(ShareFakeWatcher.content, "xxx")
89+
self.assertEqual(ShareFakeWatcher.count, 3)
7990

80-
client.remove_config_watcher(d, g, test_cb)
81-
Share.count = 0
91+
client.remove_config_watcher(d, g, test_cb_fake_watcher)
92+
ShareFakeWatcher.count = 0
8293
client.notify_queue.put((cache_key, "yyy", "md52"))
8394
time.sleep(1)
84-
self.assertEqual(Share.content, "yyy")
85-
self.assertEqual(Share.count, 2)
95+
self.assertEqual(ShareFakeWatcher.content, "yyy")
96+
self.assertEqual(ShareFakeWatcher.count, 2)
8697

87-
client.remove_config_watcher(d, g, test_cb, True)
88-
Share.count = 0
98+
client.remove_config_watcher(d, g, test_cb_fake_watcher, True)
99+
ShareFakeWatcher.count = 0
89100
client.notify_queue.put((cache_key, "not effective, no watchers", "md53"))
90101
time.sleep(1)
91-
self.assertEqual(Share.content, "yyy")
92-
self.assertEqual(Share.count, 0)
102+
self.assertEqual(ShareFakeWatcher.content, "yyy")
103+
self.assertEqual(ShareFakeWatcher.count, 0)
93104

94-
Share.count = 0
95-
client.add_config_watcher(d, g, test_cb)
105+
ShareFakeWatcher.count = 0
106+
client.add_config_watcher(d, g, test_cb_fake_watcher)
96107
time.sleep(1)
97108
client.notify_queue.put((cache_key, "zzz", "md54"))
98109
time.sleep(1)
99-
self.assertEqual(Share.content, "zzz")
100-
self.assertEqual(Share.count, 1)
110+
self.assertEqual(ShareFakeWatcher.content, "zzz")
111+
self.assertEqual(ShareFakeWatcher.count, 1)
101112

102-
Share.count = 0
113+
ShareFakeWatcher.count = 0
103114
client.notify_queue.put((cache_key, "not effective, md5 no changes", "md54"))
104115
time.sleep(1)
105-
self.assertEqual(Share.content, "zzz")
106-
self.assertEqual(Share.count, 0)
116+
self.assertEqual(ShareFakeWatcher.content, "zzz")
117+
self.assertEqual(ShareFakeWatcher.count, 0)
107118

108119
def test_long_pulling(self):
109120
client2 = nacos.NacosClient(SERVER_ADDRESSES, username=USERNAME, password=PASSWORD)
110121
d = "test1_pulling"
111122
g = "Group1"
112123
g2 = "Group2"
113124

114-
class Share:
115-
content = None
116-
117-
def cb(x):
118-
Share.content = x["content"]
119-
print(Share.content)
120-
121125
client2.publish_config(d, g, "test2")
122126
client2.publish_config(d, g2, "test2")
123127
time.sleep(0.5)
124128
# test common
125-
client2.add_config_watcher(d, g, cb)
126-
client2.add_config_watcher(d, g2, cb)
129+
client2.add_config_watcher(d, g, cb_long_pulling)
130+
client2.add_config_watcher(d, g2, cb_long_pulling)
127131
time.sleep(0.5)
128132
client2.publish_config(d, g, "test")
129133
client2.publish_config(d, g2, "test")
130134
time.sleep(1)
131-
self.assertEqual(Share.content, "test")
135+
self.assertEqual(ShareLongPulling.content, "test")
132136
client2.publish_config(d, g2, u"test2中文")
133137
time.sleep(1)
134-
self.assertEqual(Share.content, u"test2中文")
138+
self.assertEqual(ShareLongPulling.content, u"test2中文")
135139

136140
def test_get_from_failover(self):
137141
d = "test_fo"
@@ -179,7 +183,7 @@ def test_list_naming_instance_offline(self):
179183
self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 0)
180184

181185
def test_list_naming_instance_online(self):
182-
client.add_naming_instance("test.service", "1.0.0.1", 8080, "testCluster2", 0.1, "{}", True, True)
186+
client.add_naming_instance("test.service", "127.0.0.1", 9876, "testCluster2", 0.1, "{}", True, True)
183187
self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 1)
184188

185189
def test_get_naming_instance(self):

0 commit comments

Comments
 (0)