Skip to content

Commit

Permalink
Merge pull request #194 from nacos-group/feature/v2
Browse files Browse the repository at this point in the history
Feature/v2
  • Loading branch information
CZJCC authored Dec 17, 2024
2 parents 3383bed + 80a1661 commit 809e387
Show file tree
Hide file tree
Showing 76 changed files with 5,526 additions and 24 deletions.
275 changes: 252 additions & 23 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pydantic~=2.7.4
requests~=2.32.0
setuptools~=49.2.1
psutil~=5.9.6
aiohttp~=3.10.11
aiofiles~=24.1.0
2 changes: 1 addition & 1 deletion test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,4 @@ def test_inject_auth_info_of_naming(self):


if __name__ == '__main__':
unittest.main()
unittest.main()
132 changes: 132 additions & 0 deletions test/client_v2_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import asyncio
import os
import unittest

from v2.nacos.common.client_config import GRPCConfig
from v2.nacos.common.client_config_builder import ClientConfigBuilder
from v2.nacos.naming.model.instance import Instance
from v2.nacos.naming.model.naming_param import RegisterInstanceParam, DeregisterInstanceParam, \
BatchRegisterInstanceParam, GetServiceParam, ListServiceParam, SubscribeServiceParam, ListInstanceParam
from v2.nacos.naming.nacos_naming_service import NacosNamingService

client_config = (ClientConfigBuilder()
.access_key(os.getenv('NACOS_ACCESS_KEY'))
.secret_key(os.getenv('NACOS_SECRET_KEY'))
.server_address(os.getenv('NACOS_SERVER_ADDR', 'localhost:8848'))
.log_level('INFO')
.grpc_config(GRPCConfig(grpc_timeout=5000))
.build())


class TestClientV2(unittest.IsolatedAsyncioTestCase):

async def test_register(self):
client = await NacosNamingService.create_naming_service(client_config)
assert await client.server_health()

async def cb(instance_list: list[Instance]):
print('received subscribe callback', str(instance_list))

await client.subscribe(
SubscribeServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', subscribe_callback=cb))

print('subscribe service')

response = await client.register_instance(
request=RegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, weight=1.0, cluster_name='c1', metadata={'a': 'b'},
enabled=True,
healthy=True, ephemeral=True))
self.assertEqual(response, True)

print('register instance')

await asyncio.sleep(1)

response = await client.update_instance(
request=RegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, weight=2.0, cluster_name='c1', metadata={'a': 'b'},
enabled=True,
healthy=True, ephemeral=True))
self.assertEqual(response, True)

print('update instance')

await asyncio.sleep(1)

response = await client.deregister_instance(
request=DeregisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', ip='1.1.1.1',
port=7001, cluster_name='c1', ephemeral=True)
)
self.assertEqual(response, True)

print('deregister instance')

await asyncio.sleep(1)

param1 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7001,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=True,
ephemeral=True
)
param2 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7002,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=True,
ephemeral=True
)
param3 = RegisterInstanceParam(service_name='nacos.test.1',
group_name='DEFAULT_GROUP',
ip='1.1.1.1',
port=7003,
weight=1.0,
cluster_name='c1',
metadata={'a': 'b'},
enabled=True,
healthy=False,
ephemeral=True
)
response = await client.batch_register_instances(
request=BatchRegisterInstanceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP',
instances=[param1, param2, param3]))
self.assertEqual(response, True)

print('batch register instance')
await asyncio.sleep(1)

service = await client.get_service(
GetServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', cluster_name='c1'))
print('get service', str(service))
assert service.name == 'nacos.test.1'

service_list = await client.list_services(ListServiceParam())
assert service_list.count == 1

instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=True))
assert len(instance_list) == 2

instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=False))
assert len(instance_list) == 1

instance_list = await client.list_instances(ListInstanceParam(service_name='nacos.test.1', healthy_only=None))
assert len(instance_list) == 3

await client.unsubscribe(
SubscribeServiceParam(service_name='nacos.test.1', group_name='DEFAULT_GROUP', subscribe_callback=cb))

await client.shutdown()


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

0 comments on commit 809e387

Please sign in to comment.