Skip to content

Commit

Permalink
[ISSUE alibaba#9210] Add UTs for ephemeral client verification.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixystone committed Sep 26, 2022
1 parent 81dfc58 commit a11eca2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Expand Up @@ -20,14 +20,18 @@
import com.alibaba.nacos.core.remote.Connection;
import com.alibaba.nacos.core.remote.ConnectionMeta;
import com.alibaba.nacos.naming.consistency.ephemeral.distro.v2.DistroClientVerifyInfo;
import com.alibaba.nacos.naming.constants.ClientConstants;
import com.alibaba.nacos.naming.core.v2.client.ClientAttributes;
import com.alibaba.nacos.naming.core.v2.client.impl.ConnectionBasedClient;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.env.MockEnvironment;

import java.util.Collection;

Expand Down Expand Up @@ -55,6 +59,11 @@ public class ConnectionBasedClientManagerTest {
@Mock
private ClientAttributes clientAttributes;

@BeforeClass
public static void setUpBeforeClass() {
EnvUtil.setEnvironment(new MockEnvironment());
}

@Before
public void setUp() throws Exception {
connectionBasedClientManager = new ConnectionBasedClientManager();
Expand All @@ -63,6 +72,7 @@ public void setUp() throws Exception {
when(connection.getMetaInfo()).thenReturn(connectionMeta);
when(connectionMeta.getLabel(RemoteConstants.LABEL_MODULE)).thenReturn(RemoteConstants.LABEL_MODULE_NAMING);

when(clientAttributes.getClientAttribute(ClientConstants.REVISION, 0)).thenReturn(0);
assertTrue(connectionBasedClientManager.syncClientConnected(connectionId, clientAttributes));
assertTrue(connectionBasedClientManager.verifyClient(new DistroClientVerifyInfo(connectionId, 0)));
connectionBasedClientManager.clientConnected(connection);
Expand Down
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.naming.core.v2.client.manager.impl;

import com.alibaba.nacos.naming.consistency.ephemeral.distro.v2.DistroClientVerifyInfo;
import com.alibaba.nacos.naming.constants.ClientConstants;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.v2.client.Client;
import com.alibaba.nacos.naming.core.v2.client.ClientAttributes;
Expand All @@ -31,6 +33,7 @@
import org.springframework.mock.env.MockEnvironment;

import java.util.Collection;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -67,7 +70,9 @@ public static void setUpBeforeClass() {
public void setUp() throws Exception {
ephemeralIpPortClientManager = new EphemeralIpPortClientManager(distroMapper, switchDomain);
when(client.getClientId()).thenReturn(ephemeralIpPortId);
when(client.getRevision()).thenReturn(1320L);
ephemeralIpPortClientManager.clientConnected(client);
when(attributes.getClientAttribute(ClientConstants.REVISION, 0)).thenReturn(5120);
ephemeralIpPortClientManager.syncClientConnected(syncedClientId, attributes);
}

Expand All @@ -92,4 +97,18 @@ public void testContainsEphemeralIpPortId() {
String unUsedClientId = "127.0.0.1:8888#true";
assertFalse(ephemeralIpPortClientManager.contains(unUsedClientId));
}

@Test
public void testVerifyClient0() {
assertTrue(ephemeralIpPortClientManager.verifyClient(new DistroClientVerifyInfo(ephemeralIpPortId, 0)));
assertTrue(ephemeralIpPortClientManager.verifyClient(new DistroClientVerifyInfo(syncedClientId, 0)));
}

@Test
public void testVerifyClient() {
assertFalse(ephemeralIpPortClientManager.verifyClient(new DistroClientVerifyInfo(ephemeralIpPortId, 1)));
assertTrue(ephemeralIpPortClientManager.verifyClient(new DistroClientVerifyInfo(ephemeralIpPortId, 1320)));
assertFalse(ephemeralIpPortClientManager.verifyClient(new DistroClientVerifyInfo(syncedClientId, 1)));
assertTrue(ephemeralIpPortClientManager.verifyClient(new DistroClientVerifyInfo(syncedClientId, 5120)));
}
}

0 comments on commit a11eca2

Please sign in to comment.