|
11 | 11 | import time
|
12 | 12 | import shutil
|
13 | 13 |
|
14 |
| -SERVER_1 = "100.69.207.65" |
| 14 | +SERVER_1 = "192.168.0.104" |
15 | 15 | 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" |
19 | 18 |
|
20 | 19 | # Set the following values if authentication mode is enabled on the server
|
21 | 20 | USERNAME = None
|
|
25 | 24 | # Set the following option if http requests need through by proxy
|
26 | 25 | # client.set_options(proxies={"http":"192.168.56.1:809"})
|
27 | 26 |
|
| 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 | + |
28 | 48 | class TestClient(unittest.TestCase):
|
29 | 49 | def test_get_server(self):
|
30 | 50 | self.assertEqual(client.get_server(), (SERVER_1, 8848))
|
@@ -57,81 +77,65 @@ def test_fake_watcher(self):
|
57 | 77 | d = "test"
|
58 | 78 | g = "DEFAULT_GROUP"
|
59 | 79 |
|
60 |
| - class Share: |
61 |
| - content = None |
62 |
| - count = 0 |
63 |
| - |
64 | 80 | cache_key = "+".join([d, g, NAMESPACE])
|
65 | 81 |
|
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) |
74 | 85 | time.sleep(1)
|
75 | 86 | client.notify_queue.put((cache_key, "xxx", "md51"))
|
76 | 87 | 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) |
79 | 90 |
|
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 |
82 | 93 | client.notify_queue.put((cache_key, "yyy", "md52"))
|
83 | 94 | 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) |
86 | 97 |
|
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 |
89 | 100 | client.notify_queue.put((cache_key, "not effective, no watchers", "md53"))
|
90 | 101 | 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) |
93 | 104 |
|
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) |
96 | 107 | time.sleep(1)
|
97 | 108 | client.notify_queue.put((cache_key, "zzz", "md54"))
|
98 | 109 | 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) |
101 | 112 |
|
102 |
| - Share.count = 0 |
| 113 | + ShareFakeWatcher.count = 0 |
103 | 114 | client.notify_queue.put((cache_key, "not effective, md5 no changes", "md54"))
|
104 | 115 | 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) |
107 | 118 |
|
108 | 119 | def test_long_pulling(self):
|
109 | 120 | client2 = nacos.NacosClient(SERVER_ADDRESSES, username=USERNAME, password=PASSWORD)
|
110 | 121 | d = "test1_pulling"
|
111 | 122 | g = "Group1"
|
112 | 123 | g2 = "Group2"
|
113 | 124 |
|
114 |
| - class Share: |
115 |
| - content = None |
116 |
| - |
117 |
| - def cb(x): |
118 |
| - Share.content = x["content"] |
119 |
| - print(Share.content) |
120 |
| - |
121 | 125 | client2.publish_config(d, g, "test2")
|
122 | 126 | client2.publish_config(d, g2, "test2")
|
123 | 127 | time.sleep(0.5)
|
124 | 128 | # 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) |
127 | 131 | time.sleep(0.5)
|
128 | 132 | client2.publish_config(d, g, "test")
|
129 | 133 | client2.publish_config(d, g2, "test")
|
130 | 134 | time.sleep(1)
|
131 |
| - self.assertEqual(Share.content, "test") |
| 135 | + self.assertEqual(ShareLongPulling.content, "test") |
132 | 136 | client2.publish_config(d, g2, u"test2中文")
|
133 | 137 | time.sleep(1)
|
134 |
| - self.assertEqual(Share.content, u"test2中文") |
| 138 | + self.assertEqual(ShareLongPulling.content, u"test2中文") |
135 | 139 |
|
136 | 140 | def test_get_from_failover(self):
|
137 | 141 | d = "test_fo"
|
@@ -179,7 +183,7 @@ def test_list_naming_instance_offline(self):
|
179 | 183 | self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 0)
|
180 | 184 |
|
181 | 185 | 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) |
183 | 187 | self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 1)
|
184 | 188 |
|
185 | 189 | def test_get_naming_instance(self):
|
|
0 commit comments