Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
YunWZ committed Aug 11, 2022
1 parent 3586ad6 commit 3c28c27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
*/
public class ServerListManager implements ServerListFactory, Closeable {

private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate();
private final NacosRestTemplate nacosRestTemplate;

private final long refreshServerListInternal = TimeUnit.SECONDS.toMillis(30);

Expand All @@ -82,7 +82,27 @@ public ServerListManager(Properties properties) {
this(properties, null);
}

/**
* just for testing.
*
* @param properties properties
* @param namespace namespace
* @param nacosRestTemplate mock
*/
ServerListManager(Properties properties, String namespace, NacosRestTemplate nacosRestTemplate) {
this.nacosRestTemplate = nacosRestTemplate;
this.namespace = namespace;
initServerAddr(properties);
List<String> realServerList;
if (null != (realServerList = getServerList()) && !realServerList.isEmpty()) {
currentIndex.set(new Random().nextInt(realServerList.size()));
} else {
throw new NacosLoadException("serverList is empty,please check configuration");
}
}

public ServerListManager(Properties properties, String namespace) {
this.nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate();
this.namespace = namespace;
initServerAddr(properties);
if (!serverList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
Expand All @@ -45,22 +43,18 @@ public void testConstructWithAddr() {
Assert.assertEquals("127.0.0.1:8849", serverList.get(1));
}

@Ignore
@Test
public void testConstructWithEndpoint() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.ENDPOINT, "127.0.0.1");
final ServerListManager serverListManager = new ServerListManager(properties);
NacosRestTemplate mock = Mockito.mock(NacosRestTemplate.class);

HttpRestResult<Object> a = new HttpRestResult<Object>();
HttpRestResult<Object> a = new HttpRestResult<>();
a.setData("127.0.0.1:8848");
a.setCode(200);
Mockito.when(mock.get(any(), any(), any(), any())).thenReturn(a);

final Field nacosRestTemplate = ServerListManager.class.getDeclaredField("nacosRestTemplate");
nacosRestTemplate.setAccessible(true);
nacosRestTemplate.set(serverListManager, mock);
final ServerListManager serverListManager = new ServerListManager(properties, null, mock);
TimeUnit.SECONDS.sleep(31);
final List<String> serverList = serverListManager.getServerList();
Assert.assertEquals(1, serverList.size());
Expand Down

0 comments on commit 3c28c27

Please sign in to comment.