Skip to content

Commit

Permalink
Refactor testcase code(sofastack#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojie09 authored and Synex-wh committed Mar 25, 2019
1 parent bcffe78 commit 34c2f47
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 141 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# SOFARegistry

[![Build Status](https://travis-ci.com/alipay/sofa-registry.svg?branch=master)](https://travis-ci.com/alipay/sofa-registry)
![license](https://img.shields.io/badge/license-Apache--2.0-green.svg)
[![Coverage Status](https://codecov.io/gh/alipay/sofa-registry/branch/master/graph/badge.svg)](https://codecov.io/gh/alipay/sofa-registry)
![maven](https://img.shields.io/github/release/alipay/sofa-registry.svg)

SOFARegistry 是蚂蚁金服开源的一个生产级、高时效、高可用的服务注册中心。SOFARegistry 最早源自于淘宝的 ConfigServer,十年来,随着蚂蚁金服的业务发展,注册中心架构已经演进至第五代。目前 SOFARegistry 不仅全面服务于蚂蚁金服的自有业务,还随着蚂蚁金融科技服务众多合作伙伴,同时也兼容开源生态。SOFARegistry 采用 AP 架构,支持秒级时效性推送,同时采用分层架构支持无限水平扩展。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void unregisterAndRepublish() throws InterruptedException {
assertTrue(ex instanceof IllegalStateException);
}

Thread.sleep(500L);
Thread.sleep(1000L);

assertTrue(publisher.isRegistered());
assertFalse(publisher.isEnabled());
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<commons-io.version>2.4</commons-io.version>
<jetty.version>9.4.12.v20180830</jetty.version>
<main.user.dir>${user.dir}</main.user.dir>
<argLine>-Dnetwork_interface_denylist=docker0</argLine>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

/**
Expand All @@ -33,28 +36,39 @@
*/
public class NetUtil {

private static final Logger LOGGER = LoggerFactory
.getLogger(NetUtil.class);
private static final Logger LOGGER = LoggerFactory
.getLogger(NetUtil.class);

/**
* The constant LOCALHOST.
*/
public static final String LOCALHOST = "127.0.0.1";
public static final String LOCALHOST = "127.0.0.1";

/**
* The constant ANYHOST.
*/
public static final String ANYHOST = "0.0.0.0";
public static final String ANYHOST = "0.0.0.0";

/** symbol : */
public static final char COLON = ':';

private static volatile InetAddress LOCAL_ADDRESS = null;

private static final Pattern IP_PATTERN = Pattern
.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");

public static final String NETWORK_INTERFACE_IN = "network_interface_binding";
public static final char COLON = ':';

private static volatile InetAddress LOCAL_ADDRESS = null;

private static final Pattern IP_PATTERN = Pattern
.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");

public static final String NETWORK_INTERFACE_BINDING = "network_interface_binding";
public static final String NETWORK_INTERFACE_BINDING_VALUE = System
.getProperty(NETWORK_INTERFACE_BINDING);
public static final String NETWORK_INTERFACE_DENYLIST = "network_interface_denylist";
public static final List<String> NETWORK_INTERFACE_DENYLIST_VALUE = System
.getProperty(NETWORK_INTERFACE_DENYLIST) == null ? Collections
.emptyList()
: Arrays
.asList(System
.getProperty(
NETWORK_INTERFACE_DENYLIST)
.split(","));

/**
* Gen host string.
Expand Down Expand Up @@ -159,15 +173,19 @@ private static InetAddress getLocalAddress0() {
try {
NetworkInterface network = interfaces.nextElement();
boolean useNi;
String rpcNi = System.getProperty(NETWORK_INTERFACE_IN);
if (rpcNi != null && !rpcNi.isEmpty()) {
if (rpcNi.equals(network.getDisplayName())
|| rpcNi.equals(network.getName())) {
if (NETWORK_INTERFACE_BINDING_VALUE != null
&& !NETWORK_INTERFACE_BINDING_VALUE.isEmpty()) {
if (NETWORK_INTERFACE_BINDING_VALUE.equals(network.getDisplayName())
|| NETWORK_INTERFACE_BINDING_VALUE.equals(network.getName())) {
useNi = true;
} else {
continue;
}
} else {
if (NETWORK_INTERFACE_DENYLIST_VALUE.contains(network.getDisplayName())
|| NETWORK_INTERFACE_DENYLIST_VALUE.contains(network.getName())) {
continue;
}
useNi = true;
}

Expand Down
48 changes: 48 additions & 0 deletions test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,54 @@
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-server-integration</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-server-session</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-server-data</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-server-meta</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-store-api</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-store-jraft</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-remoting-api</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-remoting-bolt</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-remoting-http</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-consistency</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-common-model</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-common-util</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions test/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{ISO8601}][%p][%t][%c{0}] - %m%n</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
</appender>
<appender name="DEFAULT-SERVER-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package com.alipay.sofa.registry.test;

import com.alipay.remoting.Connection;
import com.alipay.sofa.registry.client.api.ConfigDataObserver;
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
import com.alipay.sofa.registry.client.api.SubscriberDataObserver;
import com.alipay.sofa.registry.client.api.model.ConfigData;
import com.alipay.sofa.registry.client.api.model.UserData;
import com.alipay.sofa.registry.client.provider.DefaultRegistryClient;
import com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfigBuilder;
Expand All @@ -30,16 +28,17 @@
import com.alipay.sofa.registry.client.task.WorkerThread;
import com.alipay.sofa.registry.common.model.CommonResponse;
import com.alipay.sofa.registry.common.model.sessionserver.CancelAddressRequest;
import com.alipay.sofa.registry.common.model.store.Publisher;
import com.alipay.sofa.registry.common.model.store.URL;
import com.alipay.sofa.registry.net.NetUtil;
import com.alipay.sofa.registry.remoting.Channel;
import com.alipay.sofa.registry.remoting.jersey.JerseyClient;
import com.alipay.sofa.registry.server.data.cache.DatumCache;
import com.alipay.sofa.registry.server.test.TestRegistryMain;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;

import javax.ws.rs.client.Entity;
Expand All @@ -51,52 +50,50 @@
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.junit.Assert.assertTrue;

/**
* @author xuanbei 18/12/1
*/
@SpringBootConfiguration
@SpringBootTest
public class BaseIntegrationTest {
private static final AtomicBoolean STARTED = new AtomicBoolean(false);

public static final String LOCAL_ADDRESS = NetUtil.getLocalAddress()
.getHostAddress();
public static final String LOCAL_DATACENTER = "DefaultDataCenter";
public static final String LOCAL_REGION = "DEFAULT_ZONE";
private static final AtomicBoolean STARTED = new AtomicBoolean(
false);

public static final String LOCAL_ADDRESS = NetUtil
.getLocalAddress()
.getHostAddress();
public static final String LOCAL_DATACENTER = "DefaultDataCenter";
public static final String LOCAL_REGION = "DEFAULT_ZONE";
private static final int CLIENT_OFF_MAX_WAIT_TIME = 30;
protected static ConfigurableApplicationContext metaApplicationContext;
protected static ConfigurableApplicationContext sessionApplicationContext;
protected static ConfigurableApplicationContext dataApplicationContext;

protected DefaultRegistryClient registryClient1;

protected DefaultRegistryClient registryClient2;

protected Channel sessionChannel;
protected static DefaultRegistryClient registryClient1;

protected Channel dataChannel;
protected static DefaultRegistryClient registryClient2;

protected Channel metaChannel;
protected static Channel sessionChannel;

protected volatile String dataId;
protected static Channel dataChannel;

protected volatile UserData userData;
protected static Channel metaChannel;

protected volatile ConfigData configData;
protected static volatile String dataId;
protected static volatile String value;
protected static volatile UserData userData;

@Value("${session.server.httpServerPort}")
protected int sessionPort;

@Value("${meta.server.httpServerPort}")
protected int metaPort;

@Value("${data.server.httpServerPort}")
protected int dataPort;
protected static int sessionPort = 9603;
protected static int metaPort = 9615;
protected static int dataPort = 9622;

@Value("${meta.server.raftServerPort}")
protected int raftPort;
Expand All @@ -107,8 +104,12 @@ public class BaseIntegrationTest {
@Value("${data.server.syncDataPort}")
protected int syncDataPort;

@BeforeClass
public static void beforeClass() throws Exception {
@Before
public void before() throws Exception {
startServerIfNecessary();
}

public static void startServerIfNecessary() throws Exception {
if (STARTED.compareAndSet(false, true)) {
Map<String, String> configs = new HashMap<>();
configs.put("nodes.metaNode", LOCAL_DATACENTER + ":" + LOCAL_ADDRESS);
Expand All @@ -120,11 +121,11 @@ public static void beforeClass() throws Exception {
metaApplicationContext = testRegistryMain.getMetaApplicationContext();
sessionApplicationContext = testRegistryMain.getSessionApplicationContext();
dataApplicationContext = testRegistryMain.getDataApplicationContext();
initRegistryClientAndChannel();
}
}

@Before
public void before() throws Exception {
private static void initRegistryClientAndChannel() {
if (registryClient1 == null) {
RegistryClientConfig config = DefaultRegistryClientConfigBuilder.start()
.setAppName("testApp1").setDataCenter(LOCAL_DATACENTER).setZone(LOCAL_REGION)
Expand All @@ -148,35 +149,18 @@ public void before() throws Exception {
dataChannel = JerseyClient.getInstance().connect(new URL(LOCAL_ADDRESS, dataPort));
metaChannel = JerseyClient.getInstance().connect(new URL(LOCAL_ADDRESS, metaPort));
}

dataId = null;
userData = null;
}

@After
public void after() throws Exception {
dataId = null;
userData = null;
configData = null;
}

public class MySubscriberDataObserver implements SubscriberDataObserver {
public static class MySubscriberDataObserver implements SubscriberDataObserver {
@Override
public void handleData(String dataId, UserData data) {
BaseIntegrationTest.this.dataId = dataId;
BaseIntegrationTest.this.userData = data;
BaseIntegrationTest.dataId = dataId;
BaseIntegrationTest.userData = data;
}
}

public class MyConfigDataObserver implements ConfigDataObserver {
@Override
public void handleData(String dataId, ConfigData configData) {
BaseIntegrationTest.this.dataId = dataId;
BaseIntegrationTest.this.configData = configData;
}
}

protected void clientOff() throws Exception {
protected static void clientOff() throws Exception {
startServerIfNecessary();
List<String> connectIds = new ArrayList<>();
connectIds.add(LOCAL_ADDRESS + ":" + getSourcePort(registryClient1));
connectIds.add(LOCAL_ADDRESS + ":" + getSourcePort(registryClient2));
Expand All @@ -187,10 +171,41 @@ protected void clientOff() throws Exception {
.post(Entity.entity(new CancelAddressRequest(connectIds), MediaType.APPLICATION_JSON),
CommonResponse.class);
assertTrue(response.isSuccess());
Thread.sleep(1000);
int times = 0;
while (times++ < CLIENT_OFF_MAX_WAIT_TIME) {
if (clientOffSuccess()) {
return;
}
Thread.sleep(500);
}
throw new RuntimeException("clientOff failed.");
}

protected static void clearData() throws Exception {
DatumCache.getAll().clear();
List<String> connectIds = new ArrayList<>(Arrays.asList(
NetUtil.genHost(LOCAL_ADDRESS, getSourcePort(registryClient1)),
NetUtil.genHost(LOCAL_ADDRESS, getSourcePort(registryClient2))));
for (String connectId : connectIds) {
Map<String, Publisher> publisherMap = DatumCache.getByHost(connectId);
if (publisherMap != null) {
publisherMap.clear();
}
}
}

private static boolean clientOffSuccess() {
String sessionDigestCount = sessionChannel.getWebTarget().path("digest/data/count")
.request(APPLICATION_JSON).get(String.class);
String dataDigestCount = dataChannel.getWebTarget().path("digest/datum/count")
.request(APPLICATION_JSON).get(String.class);
return sessionDigestCount
.equals("Subscriber count: 0, Publisher count: 0, Watcher count: 0")
&& (dataDigestCount.equals("CacheDigest datum cache is empty") || dataDigestCount
.contains("[Publisher] size of publisher in DefaultDataCenter is 0"));
}

protected int getSourcePort(DefaultRegistryClient registryClient) throws Exception {
protected static int getSourcePort(DefaultRegistryClient registryClient) throws Exception {
Field workerThreadField = DefaultRegistryClient.class.getDeclaredField("workerThread");
workerThreadField.setAccessible(true);
WorkerThread workerThread = (WorkerThread) workerThreadField.get(registryClient);
Expand Down
Loading

0 comments on commit 34c2f47

Please sign in to comment.