diff --git a/address/pom.xml b/address/pom.xml
index 5247f1252..0b01afd36 100644
--- a/address/pom.xml
+++ b/address/pom.xml
@@ -51,16 +51,6 @@
-
- org.mockito
- mockito-all
- test
-
-
- org.hamcrest
- hamcrest-all
- test
-
diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java
index f4b22d7e2..ea96e5b53 100644
--- a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java
+++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java
@@ -60,9 +60,9 @@ public class ServerListManager implements ServerListFactory, Closeable {
private final AtomicInteger currentIndex = new AtomicInteger();
- private List serversFromEndpoint = new ArrayList();
+ private final List serverList = new ArrayList<>();
- private List serverList = new ArrayList();
+ private List serversFromEndpoint = new ArrayList<>();
private ScheduledExecutorService refreshServerListExecutor;
@@ -85,12 +85,9 @@ public class ServerListManager implements ServerListFactory, Closeable {
this.serversFromEndpoint = getServerListFromEndpoint();
refreshServerListExecutor = new ScheduledThreadPoolExecutor(1,
new NameThreadFactory("com.alibaba.nacos.client.naming.server.list.refresher"));
- refreshServerListExecutor.scheduleWithFixedDelay(new Runnable() {
- @Override
- public void run() {
- refreshServerListIfNeed();
- }
- }, 0, refreshServerListInternal, TimeUnit.MILLISECONDS);
+ refreshServerListExecutor
+ .scheduleWithFixedDelay(this::refreshServerListIfNeed, 0, refreshServerListInternal,
+ TimeUnit.MILLISECONDS);
} else {
String serverListFromProps = properties.getProperty(PropertyKeyConst.SERVER_ADDR);
if (StringUtils.isNotEmpty(serverListFromProps)) {
@@ -138,7 +135,7 @@ public class ServerListManager implements ServerListFactory, Closeable {
if (CollectionUtils.isEmpty(list)) {
throw new Exception("Can not acquire Nacos list");
}
- if (!CollectionUtils.isEqualCollection(list, serversFromEndpoint)) {
+ if (null == serversFromEndpoint || !CollectionUtils.isEqualCollection(list, serversFromEndpoint)) {
NAMING_LOGGER.info("[SERVER-LIST] server list is updated: " + list);
}
serversFromEndpoint = list;
@@ -156,6 +153,7 @@ public class ServerListManager implements ServerListFactory, Closeable {
return nacosDomain;
}
+ @Override
public List getServerList() {
return serverList.isEmpty() ? serversFromEndpoint : serverList;
}
diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java
index 4e618e2d5..fbacb48f2 100644
--- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java
+++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java
@@ -19,6 +19,7 @@ package com.alibaba.nacos.client.naming.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
/**
* Chooser.
diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java
deleted file mode 100644
index 67bc82954..000000000
--- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java
+++ /dev/null
@@ -1,223 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.alibaba.nacos.client.naming.utils;
-
-import java.util.Random;
-
-/**
- *
JVMRandom is a wrapper that supports all possible Random methods via the {@link
- * java.lang.Math#random()} method and its system-wide {@link Random} object.
- *
- * It does this to allow for a Random class in which the seed is shared between all members of the class - a better name
- * would have been SharedSeedRandom.
- *
- * N.B. the current implementation overrides the methods {@link Random#nextInt(int)} and {@link
- * Random#nextLong()} to produce positive numbers ranging from 0 (inclusive) to MAX_VALUE (exclusive).
- *
- * @author unknown
- * @version $Id: JVMRandom.java 911986 2010-02-19 21:19:05Z niallp $
- * @since 2.0
- */
-public final class JvmRandom extends Random {
-
- /**
- * Required for serialization support.
- *
- * @see java.io.Serializable
- */
- private static final long serialVersionUID = 1L;
-
- private static final Random SHARED_RANDOM = new Random();
-
- /**
- * Ensures that only the parent constructor can call reseed.
- */
- private boolean constructed = false;
-
- /**
- * Constructs a new instance.
- */
- public JvmRandom() {
- this.constructed = true;
- }
-
- /**
- * Unsupported in 2.0.
- *
- * @param seed ignored
- * @throws UnsupportedOperationException unsupported operation exception
- */
- @Override
- public synchronized void setSeed(long seed) {
- if (this.constructed) {
- throw new UnsupportedOperationException();
- }
- }
-
- /**
- * Unsupported in 2.0.
- *
- * @return Nothing, this method always throws an UnsupportedOperationException.
- * @throws UnsupportedOperationException unsupported operation exception
- */
- @Override
- public synchronized double nextGaussian() {
- throw new UnsupportedOperationException();
- }
-
- /**
- * Unsupported in 2.0.
- *
- * @param byteArray ignored
- * @throws UnsupportedOperationException unsupported operation exception
- */
- @Override
- public void nextBytes(byte[] byteArray) {
- throw new UnsupportedOperationException();
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed int value from the Math.random() sequence.
Identical
- * to nextInt(Integer.MAX_VALUE)
N.B. All values are >= 0.
- *
- * @return the random int
- */
- @Override
- public int nextInt() {
- return nextInt(Integer.MAX_VALUE);
- }
-
- /**
- *
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified
- * value (exclusive), from the Math.random() sequence.
- *
- * @param n the specified exclusive max-value
- * @return the random int
- * @throws IllegalArgumentException when n <= 0
- */
- @Override
- public int nextInt(int n) {
- return SHARED_RANDOM.nextInt(n);
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed long value from the Math.random() sequence.
- * Identical
- * to nextLong(Long.MAX_VALUE)
N.B. All values are >= 0.
- *
- * @return the random long
- */
- @Override
- public long nextLong() {
- return nextLong(Long.MAX_VALUE);
- }
-
- /**
- *
Returns a pseudorandom, uniformly distributed long value between 0 (inclusive) and the specified
- * value (exclusive), from the Math.random() sequence.
- *
- * @param n the specified exclusive max-value
- * @return the random long
- * @throws IllegalArgumentException when n <= 0
- */
- public static long nextLong(long n) {
- if (n <= 0) {
- throw new IllegalArgumentException("Upper bound for nextInt must be positive");
- }
- // Code adapted from Harmony Random#nextInt(int)
- // n is power of 2
- if ((n & -n) == n) {
- // dropping lower order bits improves behaviour for low values of n
- return next63bits() >> 63 - bitsRequired(n - 1);
- }
- // Not a power of two
- long val;
- long bits;
- // reject some values to improve distribution
- do {
- bits = next63bits();
- val = bits % n;
- } while (bits - val + (n - 1) < 0);
- return val;
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed boolean value from the Math.random() sequence.
- *
- * @return the random boolean
- */
- @Override
- public boolean nextBoolean() {
- return SHARED_RANDOM.nextBoolean();
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed float value between 0.0 and
- * 1.0
- * from the Math.random() sequence.
- *
- * @return the random float
- */
- @Override
- public float nextFloat() {
- return SHARED_RANDOM.nextFloat();
- }
-
- /**
- *
Synonymous to the Math.random() call.
- *
- * @return the random double
- */
- @Override
- public double nextDouble() {
- return SHARED_RANDOM.nextDouble();
- }
-
- /**
- * Get the next unsigned random long.
- *
- * @return unsigned random long
- */
- private static long next63bits() {
- // drop the sign bit to leave 63 random bits
- return SHARED_RANDOM.nextLong() & 0x7fffffffffffffffL;
- }
-
- /**
- * Count the number of bits required to represent a long number.
- *
- * @param num long number
- * @return number of bits required
- */
- private static int bitsRequired(long num) {
- // Derived from Hacker's Delight, Figure 5-9
- long y = num;
- int n = 0;
- while (true) {
- // 64 = number of bits in a long
- if (num < 0) {
- return 64 - n;
- }
- if (y == 0) {
- return n;
- }
- n++;
- num = num << 1;
- y = y >> 1;
- }
- }
-}
diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/RandomUtils.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/RandomUtils.java
deleted file mode 100644
index f991d41a0..000000000
--- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/RandomUtils.java
+++ /dev/null
@@ -1,171 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.alibaba.nacos.client.naming.utils;
-
-import java.util.Random;
-
-/**
- * RandomUtils is a wrapper that supports all possible {@link java.util.Random} methods via the {@link
- * java.lang.Math#random()} method and its system-wide Random object.
- *
- * @author Gary D. Gregory
- * @version $Id: RandomUtils.java 906320 2010-02-04 01:41:10Z sebb $
- * @since 2.0
- */
-public class RandomUtils {
-
- /**
- * An instance of {@link JvmRandom}.
- */
- private static final Random JVM_RANDOM = new JvmRandom();
-
- // should be possible for JVM_RANDOM?
- // public static void nextBytes(byte[]) {
- // public synchronized double nextGaussian();
- // }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed int value from the Math.random() sequence.
N.B.
- * All values are >= 0.
- *
- * @return the random int
- */
- public static int nextInt() {
- return nextInt(JVM_RANDOM);
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed int value from the given random
- * sequence.
- *
- * @param random the Random sequence generator.
- * @return the random int
- */
- public static int nextInt(Random random) {
- return random.nextInt();
- }
-
- /**
- *
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified
- * value (exclusive), from the Math.random() sequence.
- *
- * @param n the specified exclusive max-value
- * @return the random int
- */
- public static int nextInt(int n) {
- return nextInt(JVM_RANDOM, n);
- }
-
- /**
- *
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified
- * value (exclusive), from the given Random sequence.
- *
- * @param random the Random sequence generator.
- * @param n the specified exclusive max-value
- * @return the random int
- */
- public static int nextInt(Random random, int n) {
- // check this cannot return 'n'
- return random.nextInt(n);
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed long value from the Math.random() sequence.
N.B.
- * All values are >= 0.
- *
- * @return the random long
- */
- public static long nextLong() {
- return nextLong(JVM_RANDOM);
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed long value from the given Random sequence.
- *
- * @param random the Random sequence generator.
- * @return the random long
- */
- public static long nextLong(Random random) {
- return random.nextLong();
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed boolean value from the Math.random() sequence.
- *
- * @return the random boolean
- */
- public static boolean nextBoolean() {
- return nextBoolean(JVM_RANDOM);
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed boolean value from the given random sequence.
- *
- * @param random the Random sequence generator.
- * @return the random boolean
- */
- public static boolean nextBoolean(Random random) {
- return random.nextBoolean();
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed float value between 0.0 and
- * 1.0
- * from the Math.random() sequence.
- *
- * @return the random float
- */
- public static float nextFloat() {
- return nextFloat(JVM_RANDOM);
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed float value between 0.0 and
- * 1.0
- * from the given Random sequence.
- *
- * @param random the Random sequence generator.
- * @return the random float
- */
- public static float nextFloat(Random random) {
- return random.nextFloat();
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed float value between 0.0 and
- * 1.0
- * from the Math.random() sequence.
- *
- * @return the random double
- */
- public static double nextDouble() {
- return nextDouble(JVM_RANDOM);
- }
-
- /**
- *
Returns the next pseudorandom, uniformly distributed float value between 0.0 and
- * 1.0
- * from the given Random sequence.
- *
- * @param random the Random sequence generator.
- * @return the random double
- */
- public static double nextDouble(Random random) {
- return random.nextDouble();
- }
-
-}
diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/ThreadLocalRandom.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/ThreadLocalRandom.java
deleted file mode 100644
index ff280d00f..000000000
--- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/ThreadLocalRandom.java
+++ /dev/null
@@ -1,279 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.alibaba.nacos.client.naming.utils;
-
-import java.security.SecureRandom;
-import java.util.Random;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
-
-/**
- * A random number generator isolated to the current thread. Like the global {@link java.util.Random} generator used by
- * the {@link java.lang.Math} class, a {@code ThreadLocalRandom} is initialized with an internally generated seed that
- * may not otherwise be modified. When applicable, use of {@code ThreadLocalRandom} rather than shared {@code Random}
- * objects in concurrent programs will typically encounter much less overhead and contention. Use of {@code
- * ThreadLocalRandom} is particularly appropriate when multiple tasks (for example, each a {@link
- * io.netty.util.internal.chmv8.ForkJoinTask}) use random numbers in parallel in thread pools.
- *
- * Usages of this class should typically be of the form: {@code ThreadLocalRandom.current().nextX(...)} (where {@code X}
- * is {@code Int}, {@code Long}, etc). When all usages are of this form, it is never possible to accidently share a
- * {@code ThreadLocalRandom} across multiple threads.
- *
- *
- * This class also provides additional commonly used bounded random generation methods.
- *
- * //since 1.7 //author Doug Lea
- */
-@SuppressWarnings("all")
-public class ThreadLocalRandom extends Random {
-
- private static final AtomicLong seedUniquifier = new AtomicLong();
-
- private static volatile long initialSeedUniquifier;
-
- public static void setInitialSeedUniquifier(long initialSeedUniquifier) {
- ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
- }
-
- public static synchronized long getInitialSeedUniquifier() {
- // Use the value set via the setter.
- long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
- // Otherwise, generate one.
- if (initialSeedUniquifier == 0) {
- // Try to generate a real random number from /dev/random.
- // Get from a different thread to avoid blocking indefinitely on a machine without much entrophy.
- final BlockingQueue queue = new LinkedBlockingQueue();
- Thread generatorThread = new Thread("initialSeedUniquifierGenerator") {
- @Override
- public void run() {
- SecureRandom random = new SecureRandom(); // Get the real random seed from /dev/random
- queue.add(random.nextLong());
- }
- };
- generatorThread.start();
-
- // Get the random seed from the thread with timeout.
- final long timeoutSeconds = 3;
- final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds);
- for (; ; ) {
- long waitTime = deadLine - System.nanoTime();
- if (waitTime <= 0) {
- break;
- }
-
- try {
- Long result = queue.poll(waitTime, TimeUnit.NANOSECONDS);
- if (result != null) {
- initialSeedUniquifier = result;
- break;
- }
- } catch (InterruptedException ignore) {
- // Ignore
- }
- }
-
- // Just in case the initialSeedUniquifier is zero or some other constant
- initialSeedUniquifier ^= 0x3255ecdc33bae119L; // just a meaningless random number
- initialSeedUniquifier ^= Long.reverse(System.nanoTime());
-
- ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
- }
-
- return initialSeedUniquifier;
- }
-
- private static long newSeed() {
- for (; ; ) {
- final long current = seedUniquifier.get();
- final long actualCurrent = current != 0 ? current : getInitialSeedUniquifier();
-
- // L'Ecuyer, "Tables of Linear Congruential Generators of Different Sizes and Good Lattice Structure", 1999
- final long next = actualCurrent * 181783497276652981L;
-
- if (seedUniquifier.compareAndSet(current, next)) {
- return next ^ System.nanoTime();
- }
- }
- }
-
- // same constants as Random, but must be redeclared because private
- private static final long multiplier = 0x5DEECE66DL;
-
- private static final long addend = 0xBL;
-
- private static final long mask = (1L << 48) - 1;
-
- /**
- * The random seed. We can't use super.seed.
- */
- private long rnd;
-
- /**
- * Initialization flag to permit calls to setSeed to succeed only while executing the Random constructor. We can't
- * allow others since it would cause setting seed in one part of a program to unintentionally impact other usages by
- * the thread.
- */
- boolean initialized = false;
-
- // Padding to help avoid memory contention among seed updates in
- // different TLRs in the common case that they are located near
- // each other.
- private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
-
- /**
- * Constructor called only by localRandom.initialValue.
- */
- ThreadLocalRandom() {
- super(newSeed());
- initialized = true;
- }
-
- /**
- * The actual ThreadLocal
- */
- private static final ThreadLocal localRandom = new ThreadLocal() {
- @Override
- protected ThreadLocalRandom initialValue() {
- return new ThreadLocalRandom();
- }
- };
-
- /**
- * Returns the current thread's {@code ThreadLocalRandom}.
- *
- * @return the current thread's {@code ThreadLocalRandom}
- */
- public static ThreadLocalRandom current() {
- return localRandom.get();
- }
-
- /**
- * Throws {@code UnsupportedOperationException}. Setting seeds in this generator is not supported.
- *
- * @throws UnsupportedOperationException always
- */
- @Override
- public void setSeed(long seed) {
- if (initialized) {
- throw new UnsupportedOperationException();
- }
- rnd = (seed ^ multiplier) & mask;
- }
-
- @Override
- protected int next(int bits) {
- rnd = (rnd * multiplier + addend) & mask;
- return (int) (rnd >>> (48 - bits));
- }
-
- /**
- * Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound
- * (exclusive).
- *
- * @param least the least value returned
- * @param bound the upper bound (exclusive)
- * @return the next value
- * @throws IllegalArgumentException if least greater than or equal to bound
- */
- public int nextInt(int least, int bound) {
- if (least >= bound) {
- throw new IllegalArgumentException();
- }
- return nextInt(bound - least) + least;
- }
-
- /**
- * Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).
- *
- * @param n the bound on the random number to be returned. Must be positive.
- * @return the next value
- * @throws IllegalArgumentException if n is not positive
- */
- public long nextLong(long n) {
- if (n <= 0) {
- throw new IllegalArgumentException("n must be positive");
- }
-
- // Divide n by two until small enough for nextInt. On each
- // iteration (at most 31 of them but usually much less),
- // randomly choose both whether to include high bit in result
- // (offset) and whether to continue with the lower vs upper
- // half (which makes a difference only if odd).
- long offset = 0;
- while (n >= Integer.MAX_VALUE) {
- int bits = next(2);
- long half = n >>> 1;
- long nextn = ((bits & 2) == 0) ? half : n - half;
- if ((bits & 1) == 0) {
- offset += n - nextn;
- }
- n = nextn;
- }
- return offset + nextInt((int) n);
- }
-
- /**
- * Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound
- * (exclusive).
- *
- * @param least the least value returned
- * @param bound the upper bound (exclusive)
- * @return the next value
- * @throws IllegalArgumentException if least greater than or equal to bound
- */
- public long nextLong(long least, long bound) {
- if (least >= bound) {
- throw new IllegalArgumentException();
- }
- return nextLong(bound - least) + least;
- }
-
- /**
- * Returns a pseudorandom, uniformly distributed {@code double} value between 0 (inclusive) and the specified value
- * (exclusive).
- *
- * @param n the bound on the random number to be returned. Must be positive.
- * @return the next value
- * @throws IllegalArgumentException if n is not positive
- */
- public double nextDouble(double n) {
- if (n <= 0) {
- throw new IllegalArgumentException("n must be positive");
- }
- return nextDouble() * n;
- }
-
- /**
- * Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound
- * (exclusive).
- *
- * @param least the least value returned
- * @param bound the upper bound (exclusive)
- * @return the next value
- * @throws IllegalArgumentException if least greater than or equal to bound
- */
- public double nextDouble(double least, double bound) {
- if (least >= bound) {
- throw new IllegalArgumentException();
- }
- return nextDouble() * (bound - least) + least;
- }
-
- private static final long serialVersionUID = -5851777807851030925L;
-}
diff --git a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java
deleted file mode 100644
index 9da690595..000000000
--- a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright 1999-2018 Alibaba Group Holding Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.alibaba.nacos.client;
-
-import com.alibaba.nacos.api.exception.NacosException;
-import com.alibaba.nacos.client.naming.beat.BeatInfo;
-import com.alibaba.nacos.client.naming.beat.BeatReactor;
-import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.lang.reflect.Field;
-import java.util.HashMap;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-
-@RunWith(MockitoJUnitRunner.class)
-public class BeatReactorTest {
-
- @Mock
- private NamingHttpClientProxy namingHttpClientProxy;
-
- @Test
- public void test() throws NoSuchFieldException, IllegalAccessException, InterruptedException, NacosException {
- BeatInfo beatInfo = new BeatInfo();
- beatInfo.setServiceName("test");
- beatInfo.setIp("11.11.11.11");
- beatInfo.setPort(1234);
- beatInfo.setCluster("clusterName");
- beatInfo.setWeight(1);
- beatInfo.setMetadata(new HashMap());
- beatInfo.setScheduled(false);
- beatInfo.setPeriod(1000L);
-
- BeatReactor beatReactor = new BeatReactor(namingHttpClientProxy);
- beatReactor.addBeatInfo("testService", beatInfo);
-
- Assert.assertEquals(1, getActiveThread(beatReactor));
- Thread.sleep(1100L);
- beatReactor.removeBeatInfo("testService", beatInfo.getIp(), beatInfo.getPort());
- Thread.sleep(3100L);
- Assert.assertEquals(0, getActiveThread(beatReactor));
- }
-
- private int getActiveThread(BeatReactor beatReactor) throws NoSuchFieldException, IllegalAccessException {
- Field field = BeatReactor.class.getDeclaredField("executorService");
- field.setAccessible(true);
- ScheduledThreadPoolExecutor scheduledExecutorService = (ScheduledThreadPoolExecutor) field.get(beatReactor);
- return scheduledExecutorService.getQueue().size();
- }
-
-}
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java
new file mode 100644
index 000000000..993b85b1f
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.client.naming.backups;
+
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
+import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.lang.reflect.Field;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.concurrent.ScheduledExecutorService;
+
+public class FailoverReactorTest {
+
+ @Test
+ public void testInit() throws NacosException, NoSuchFieldException, IllegalAccessException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>());
+ FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp");
+ Field executorService = FailoverReactor.class.getDeclaredField("executorService");
+ executorService.setAccessible(true);
+ ScheduledExecutorService o = (ScheduledExecutorService) executorService.get(failoverReactor);
+ Assert.assertFalse(o.isShutdown());
+ failoverReactor.shutdown();
+ Assert.assertTrue(o.isShutdown());
+ }
+
+ @Test
+ public void testAddDay() throws NacosException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>());
+ FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp");
+ Date date = new Date();
+ Date actual = failoverReactor.addDay(date, 1);
+ Assert.assertEquals(date.getTime() + 24 * 60 * 60 * 1000, actual.getTime());
+ failoverReactor.shutdown();
+ }
+
+ @Test
+ public void testIsFailoverSwitch() throws NacosException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>());
+ FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp");
+ Assert.assertFalse(failoverReactor.isFailoverSwitch());
+ failoverReactor.shutdown();
+
+ }
+
+ @Test
+ public void testGetService() throws NacosException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>());
+ FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp");
+ ServiceInfo info = failoverReactor.getService("aa@@bb");
+ Assert.assertEquals(new ServiceInfo("aa@@bb").toString(), info.toString());
+ failoverReactor.shutdown();
+
+ }
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/beat/BeatInfoTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/beat/BeatInfoTest.java
new file mode 100644
index 000000000..923a76ab9
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/beat/BeatInfoTest.java
@@ -0,0 +1,89 @@
+/*
+ *
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.alibaba.nacos.client.naming.beat;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class BeatInfoTest {
+
+ @Test
+ public void testGetterAndSetter() {
+ BeatInfo info = new BeatInfo();
+
+ String ip = "1.1.1.1";
+ info.setIp(ip);
+ int port = 10000;
+ info.setPort(port);
+
+ double weight = 1.0;
+ info.setWeight(weight);
+ String serviceName = "serviceName";
+ info.setServiceName(serviceName);
+ String cluster = "cluster1";
+ info.setCluster(cluster);
+ Map meta = new HashMap<>();
+ meta.put("a", "b");
+ info.setMetadata(meta);
+ long period = 100;
+ info.setPeriod(period);
+ info.setScheduled(true);
+ info.setStopped(true);
+
+ Assert.assertEquals(ip, info.getIp());
+ Assert.assertEquals(port, info.getPort());
+ Assert.assertEquals(weight, info.getWeight(), 0.1);
+ Assert.assertEquals(serviceName, info.getServiceName());
+ Assert.assertEquals(meta, info.getMetadata());
+ Assert.assertEquals(period, info.getPeriod());
+ Assert.assertTrue(info.isScheduled());
+ Assert.assertTrue(info.isStopped());
+ }
+
+ @Test
+ public void testToString() {
+ BeatInfo info = new BeatInfo();
+
+ String ip = "1.1.1.1";
+ info.setIp(ip);
+ int port = 10000;
+ info.setPort(port);
+
+ double weight = 1.0;
+ info.setWeight(weight);
+ String serviceName = "serviceName";
+ info.setServiceName(serviceName);
+ String cluster = "cluster1";
+ info.setCluster(cluster);
+ Map meta = new HashMap<>();
+ meta.put("a", "b");
+ info.setMetadata(meta);
+ long period = 100;
+ info.setPeriod(period);
+ info.setScheduled(true);
+ info.setStopped(true);
+ String expect = "BeatInfo{port=10000, ip='1.1.1.1', " + "weight=1.0, serviceName='serviceName',"
+ + " cluster='cluster1', metadata={a=b}," + " scheduled=true, period=100, stopped=true}";
+ Assert.assertEquals(expect, info.toString());
+ }
+
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/beat/BeatReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/beat/BeatReactorTest.java
new file mode 100644
index 000000000..357f10c3d
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/beat/BeatReactorTest.java
@@ -0,0 +1,313 @@
+/*
+ *
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.alibaba.nacos.client.naming.beat;
+
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alibaba.nacos.api.common.Constants;
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import com.alibaba.nacos.api.naming.utils.NamingUtils;
+import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+public class BeatReactorTest {
+
+ @Test
+ public void testConstruct() throws NoSuchFieldException, IllegalAccessException {
+ Properties properties = new Properties();
+ String threadSize = "10";
+ properties.put(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT, threadSize);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ BeatReactor beatReactor = new BeatReactor(proxy, properties);
+ Field field = BeatReactor.class.getDeclaredField("executorService");
+ field.setAccessible(true);
+ ScheduledThreadPoolExecutor scheduledExecutorService = (ScheduledThreadPoolExecutor) field.get(beatReactor);
+ Assert.assertEquals(Integer.valueOf(threadSize).intValue(), scheduledExecutorService.getCorePoolSize());
+
+ }
+
+ @Test
+ public void testAddBeatInfo() throws NacosException, InterruptedException {
+ BeatInfo beatInfo = new BeatInfo();
+ beatInfo.setServiceName("test");
+ beatInfo.setIp("11.11.11.11");
+ beatInfo.setPort(1234);
+ beatInfo.setCluster("clusterName");
+ beatInfo.setWeight(1);
+ beatInfo.setMetadata(new HashMap());
+ beatInfo.setScheduled(false);
+ beatInfo.setPeriod(10L);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ String serviceName = "serviceName1";
+
+ beatReactor.addBeatInfo(serviceName, beatInfo);
+ TimeUnit.MILLISECONDS.sleep(15);
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
+
+ }
+
+ @Test
+ public void testRemoveBeatInfo() throws InterruptedException, NacosException {
+ BeatInfo beatInfo = new BeatInfo();
+ beatInfo.setServiceName("test");
+ String ip = "11.11.11.11";
+ beatInfo.setIp(ip);
+ int port = 1234;
+ beatInfo.setPort(port);
+ beatInfo.setCluster("clusterName");
+ beatInfo.setWeight(1);
+ beatInfo.setMetadata(new HashMap());
+ beatInfo.setScheduled(false);
+ beatInfo.setPeriod(10L);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ String serviceName = "serviceName1";
+
+ beatReactor.addBeatInfo(serviceName, beatInfo);
+ TimeUnit.MILLISECONDS.sleep(15);
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
+ beatReactor.removeBeatInfo(serviceName, ip, port);
+ Assert.assertTrue(beatInfo.isStopped());
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
+ TimeUnit.MILLISECONDS.sleep(10);
+
+ }
+
+ @Test
+ public void testBuildBeatInfo1() {
+ String ip = "11.11.11.11";
+ int port = 1234;
+ double weight = 1.0;
+ String serviceName = "service@@group1";
+ String clusterName = "cluster1";
+
+ Map meta = new HashMap<>();
+ Instance instance = new Instance();
+ instance.setServiceName(serviceName);
+ instance.setIp(ip);
+ instance.setPort(port);
+ instance.setWeight(weight);
+ instance.setMetadata(meta);
+ instance.setClusterName(clusterName);
+
+ BeatInfo expectInfo = new BeatInfo();
+ expectInfo.setServiceName(serviceName);
+ expectInfo.setIp(ip);
+ expectInfo.setPort(port);
+ expectInfo.setCluster(clusterName);
+ expectInfo.setWeight(weight);
+ expectInfo.setMetadata(meta);
+ expectInfo.setScheduled(false);
+ expectInfo.setPeriod(Constants.DEFAULT_HEART_BEAT_INTERVAL);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ assertBeatInfoEquals(expectInfo, beatReactor.buildBeatInfo(instance));
+ }
+
+ @Test
+ public void testBuildBeatInfo2() {
+ String ip = "11.11.11.11";
+ int port = 1234;
+ double weight = 1.0;
+ String serviceName = "service";
+ String clusterName = "cluster1";
+
+ Map meta = new HashMap<>();
+ Instance instance = new Instance();
+ instance.setServiceName(serviceName);
+ instance.setIp(ip);
+ instance.setPort(port);
+ instance.setWeight(weight);
+ instance.setMetadata(meta);
+ instance.setClusterName(clusterName);
+
+ String groupedService = "group1@@service";
+
+ BeatInfo expectInfo = new BeatInfo();
+ expectInfo.setServiceName(groupedService);
+ expectInfo.setIp(ip);
+ expectInfo.setPort(port);
+ expectInfo.setCluster(clusterName);
+ expectInfo.setWeight(weight);
+ expectInfo.setMetadata(meta);
+ expectInfo.setScheduled(false);
+ expectInfo.setPeriod(Constants.DEFAULT_HEART_BEAT_INTERVAL);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ assertBeatInfoEquals(expectInfo, beatReactor.buildBeatInfo(groupedService, instance));
+ }
+
+ void assertBeatInfoEquals(BeatInfo expect, BeatInfo actual) {
+ Assert.assertEquals(expect.getCluster(), actual.getCluster());
+ Assert.assertEquals(expect.getIp(), actual.getIp());
+ Assert.assertEquals(expect.getMetadata(), actual.getMetadata());
+ Assert.assertEquals(expect.getPeriod(), actual.getPeriod());
+ Assert.assertEquals(expect.getPort(), actual.getPort());
+ Assert.assertEquals(expect.getServiceName(), actual.getServiceName());
+ Assert.assertEquals(expect.getWeight(), actual.getWeight(), 0.1);
+ Assert.assertEquals(expect.isStopped(), actual.isStopped());
+ Assert.assertEquals(expect.isScheduled(), actual.isScheduled());
+ }
+
+ @Test
+ public void testBuildKey() {
+ String ip = "11.11.11.11";
+ int port = 1234;
+ String serviceName = "serviceName1";
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ Assert.assertEquals(
+ serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port,
+ beatReactor.buildKey(serviceName, ip, port));
+ }
+
+ @Test
+ public void testShutdown() throws NacosException, NoSuchFieldException, IllegalAccessException {
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ Field field = BeatReactor.class.getDeclaredField("executorService");
+ field.setAccessible(true);
+ ScheduledThreadPoolExecutor scheduledExecutorService = (ScheduledThreadPoolExecutor) field.get(beatReactor);
+
+ Assert.assertFalse(scheduledExecutorService.isShutdown());
+ beatReactor.shutdown();
+ Assert.assertTrue(scheduledExecutorService.isShutdown());
+ }
+
+ @Test
+ public void testLightBeatFromResponse() throws InterruptedException, NacosException, JsonProcessingException {
+ BeatInfo beatInfo = new BeatInfo();
+ beatInfo.setServiceName("test");
+ beatInfo.setIp("11.11.11.11");
+ beatInfo.setPort(1234);
+ beatInfo.setCluster("clusterName");
+ beatInfo.setWeight(1);
+ beatInfo.setMetadata(new HashMap());
+ beatInfo.setScheduled(false);
+ beatInfo.setPeriod(10L);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ String jsonString = "{\"lightBeatEnabled\":true,\"clientBeatInterval\":10}";
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode actualObj = mapper.readTree(jsonString);
+
+ Mockito.when(proxy.sendBeat(beatInfo, false)).thenReturn(actualObj);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ String serviceName = "serviceName1";
+
+ beatReactor.addBeatInfo(serviceName, beatInfo);
+ TimeUnit.MILLISECONDS.sleep(12);
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
+ TimeUnit.MILLISECONDS.sleep(12);
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, true);
+
+ }
+
+ @Test
+ public void testIntervalFromResponse() throws JsonProcessingException, NacosException, InterruptedException {
+ BeatInfo beatInfo = new BeatInfo();
+ beatInfo.setServiceName("test");
+ beatInfo.setIp("11.11.11.11");
+ beatInfo.setPort(1234);
+ beatInfo.setCluster("clusterName");
+ beatInfo.setWeight(1);
+ beatInfo.setMetadata(new HashMap());
+ beatInfo.setScheduled(false);
+ beatInfo.setPeriod(10L);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ String jsonString = "{\"clientBeatInterval\":20}";
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode actualObj = mapper.readTree(jsonString);
+
+ Mockito.when(proxy.sendBeat(beatInfo, false)).thenReturn(actualObj);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ String serviceName = "serviceName1";
+
+ beatReactor.addBeatInfo(serviceName, beatInfo);
+ TimeUnit.MILLISECONDS.sleep(12);
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
+ TimeUnit.MILLISECONDS.sleep(20);
+ Mockito.verify(proxy, Mockito.times(2)).sendBeat(beatInfo, false);
+
+ }
+
+ @Test
+ public void testNotFoundFromResponse() throws JsonProcessingException, NacosException, InterruptedException {
+ BeatInfo beatInfo = new BeatInfo();
+ beatInfo.setServiceName("test");
+ beatInfo.setIp("11.11.11.11");
+ beatInfo.setPort(1234);
+ beatInfo.setCluster("clusterName");
+ beatInfo.setWeight(1);
+ beatInfo.setMetadata(new HashMap());
+ beatInfo.setScheduled(false);
+ beatInfo.setPeriod(10L);
+
+ NamingHttpClientProxy proxy = Mockito.mock(NamingHttpClientProxy.class);
+ String jsonString = "{\"clientBeatInterval\":10,\"code\":20404}";
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode actualObj = mapper.readTree(jsonString);
+
+ Mockito.when(proxy.sendBeat(beatInfo, false)).thenReturn(actualObj);
+
+ Mockito.when(proxy.sendBeat(beatInfo, false)).thenReturn(actualObj);
+ BeatReactor beatReactor = new BeatReactor(proxy);
+ String serviceName = "serviceName1";
+
+ beatReactor.addBeatInfo(serviceName, beatInfo);
+ TimeUnit.MILLISECONDS.sleep(11);
+ Mockito.verify(proxy, Mockito.times(1)).sendBeat(beatInfo, false);
+
+ Instance instance = new Instance();
+ instance.setPort(beatInfo.getPort());
+ instance.setIp(beatInfo.getIp());
+ instance.setWeight(beatInfo.getWeight());
+ instance.setMetadata(beatInfo.getMetadata());
+ instance.setClusterName(beatInfo.getCluster());
+ instance.setServiceName(beatInfo.getServiceName());
+ instance.setInstanceId(null);
+ instance.setEphemeral(true);
+
+ Mockito.verify(proxy, Mockito.times(1))
+ .registerService(beatInfo.getServiceName(), NamingUtils.getGroupName(beatInfo.getServiceName()),
+ instance);
+ }
+
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ConcurrentDiskUtilTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ConcurrentDiskUtilTest.java
new file mode 100644
index 000000000..17c9c0245
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ConcurrentDiskUtilTest.java
@@ -0,0 +1,60 @@
+/*
+ *
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.alibaba.nacos.client.naming.cache;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
+public class ConcurrentDiskUtilTest {
+
+ @Test
+ public void testReadAndWrite() throws IOException {
+ File tempFile = File.createTempFile("aaa", "bbb");
+ String fileName = tempFile.getAbsolutePath();
+ String content = "hello";
+ String charset = "UTF-8";
+ ConcurrentDiskUtil.writeFileContent(fileName, content, charset);
+ String actualContent = ConcurrentDiskUtil.getFileContent(fileName, charset);
+ Assert.assertEquals(content, actualContent);
+ }
+
+ @Test
+ public void testReadAndWrite2() throws IOException {
+ File tempFile = File.createTempFile("aaa", "bbb");
+ String content = "hello";
+ String charset = "UTF-8";
+ ConcurrentDiskUtil.writeFileContent(tempFile, content, charset);
+ String actualContent = ConcurrentDiskUtil.getFileContent(tempFile, charset);
+ Assert.assertEquals(content, actualContent);
+ }
+
+ @Test
+ public void testByteBufferToString() throws IOException {
+ String msg = "test buff to string";
+ ByteBuffer buff = ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8));
+ String actual = ConcurrentDiskUtil.byteBufferToString(buff, "UTF-8");
+ Assert.assertEquals(msg, actual);
+
+ }
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/cache/DiskCacheTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/cache/DiskCacheTest.java
index acd059be4..c3e3087f1 100644
--- a/client/src/test/java/com/alibaba/nacos/client/naming/cache/DiskCacheTest.java
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/cache/DiskCacheTest.java
@@ -19,6 +19,7 @@ package com.alibaba.nacos.client.naming.cache;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import org.junit.After;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -39,7 +40,7 @@ public class DiskCacheTest {
private Instance instance;
@Before
- public void setUp() throws Exception {
+ public void setUp() {
System.out.println(CACHE_DIR);
serviceInfo = new ServiceInfo("G@@testName", "testClusters");
instance = new Instance();
@@ -94,4 +95,10 @@ public class DiskCacheTest {
assertEquals(actual.getPort(), expected.getPort());
assertEquals(actual.getMetadata(), expected.getMetadata());
}
+
+ @Test
+ public void testGetLineSeparator() {
+ String lineSeparator = DiskCache.getLineSeparator();
+ Assert.assertTrue(lineSeparator.length() > 0);
+ }
}
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java
new file mode 100644
index 000000000..7d02749a6
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java
@@ -0,0 +1,151 @@
+/*
+ *
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.alibaba.nacos.client.naming.cache;
+
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
+import com.alibaba.nacos.client.naming.backups.FailoverReactor;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ScheduledExecutorService;
+
+public class ServiceInfoHolderTest {
+
+ @Test
+ public void testGetServiceInfoMap() {
+ Properties prop = new Properties();
+ ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
+ Assert.assertEquals(0, holder.getServiceInfoMap().size());
+
+ }
+
+ @Test
+ public void testProcessServiceInfo() {
+ ServiceInfo info = new ServiceInfo("a@@b@@c");
+ Instance instance1 = createInstance("1.1.1.1", 1);
+ Instance instance2 = createInstance("1.1.1.2", 2);
+ List hosts = new ArrayList<>();
+ hosts.add(instance1);
+ hosts.add(instance2);
+ info.setHosts(hosts);
+
+ Properties prop = new Properties();
+ ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
+
+ ServiceInfo actual1 = holder.processServiceInfo(info);
+ Assert.assertEquals(info, actual1);
+
+ Instance newInstance1 = createInstance("1.1.1.1", 1);
+ newInstance1.setWeight(2.0);
+ Instance instance3 = createInstance("1.1.1.3", 3);
+ List hosts2 = new ArrayList<>();
+ hosts2.add(newInstance1);
+ hosts2.add(instance3);
+ ServiceInfo info2 = new ServiceInfo("a@@b@@c");
+ info2.setHosts(hosts2);
+
+ ServiceInfo actual2 = holder.processServiceInfo(info2);
+ Assert.assertEquals(info2, actual2);
+ }
+
+ private Instance createInstance(String ip, int port) {
+ Instance instance = new Instance();
+ instance.setIp(ip);
+ instance.setPort(port);
+ return instance;
+ }
+
+ @Test
+ public void testProcessServiceInfo2() {
+ Properties prop = new Properties();
+ ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
+ String json = "{\"groupName\":\"a\",\"name\":\"b\",\"clusters\":\"c\"}";
+
+ ServiceInfo actual = holder.processServiceInfo(json);
+ ServiceInfo expect = new ServiceInfo("a@@b@@c");
+ expect.setJsonFromServer(json);
+ Assert.assertEquals(expect.getKey(), actual.getKey());
+ }
+
+ @Test
+ public void testProcessServiceInfoWithPushEmpty() {
+ ServiceInfo oldInfo = new ServiceInfo("a@@b@@c");
+ Instance instance1 = createInstance("1.1.1.1", 1);
+ Instance instance2 = createInstance("1.1.1.2", 2);
+ List hosts = new ArrayList<>();
+ hosts.add(instance1);
+ hosts.add(instance2);
+ oldInfo.setHosts(hosts);
+
+ Properties prop = new Properties();
+ prop.setProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION, "true");
+ ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
+ holder.processServiceInfo(oldInfo);
+
+ ServiceInfo newInfo = new ServiceInfo("a@@b@@c");
+
+ final ServiceInfo actual = holder.processServiceInfo(newInfo);
+
+ Assert.assertEquals(oldInfo.getKey(), actual.getKey());
+ Assert.assertEquals(2, actual.getHosts().size());
+ }
+
+ @Test
+ public void testGetServiceInfo() {
+ ServiceInfo info = new ServiceInfo("a@@b@@c");
+ Instance instance1 = createInstance("1.1.1.1", 1);
+ List hosts = new ArrayList<>();
+ hosts.add(instance1);
+ info.setHosts(hosts);
+
+ Properties prop = new Properties();
+ ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
+
+ ServiceInfo expect = holder.processServiceInfo(info);
+ String serviceName = "b";
+ String groupName = "a";
+ String clusters = "c";
+ ServiceInfo actual = holder.getServiceInfo(serviceName, groupName, clusters);
+ Assert.assertEquals(expect.getKey(), actual.getKey());
+ Assert.assertEquals(expect.getHosts().size(), actual.getHosts().size());
+ Assert.assertEquals(expect.getHosts().get(0), actual.getHosts().get(0));
+ }
+
+ @Test
+ public void testShutdown() throws NacosException, NoSuchFieldException, IllegalAccessException {
+ Properties prop = new Properties();
+ ServiceInfoHolder holder = new ServiceInfoHolder("aa", prop);
+ Field field = ServiceInfoHolder.class.getDeclaredField("failoverReactor");
+ field.setAccessible(true);
+ FailoverReactor reactor = (FailoverReactor) field.get(holder);
+ Field executorService = FailoverReactor.class.getDeclaredField("executorService");
+ executorService.setAccessible(true);
+ ScheduledExecutorService pool = (ScheduledExecutorService) executorService.get(reactor);
+ Assert.assertFalse(pool.isShutdown());
+ holder.shutdown();
+ Assert.assertTrue(pool.isShutdown());
+ }
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/core/BalancerTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/core/BalancerTest.java
new file mode 100644
index 000000000..cc2801d2e
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/core/BalancerTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.client.naming.core;
+
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class BalancerTest {
+
+ @Rule
+ public final ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void testGetHostByRandomWeightNull() {
+ Assert.assertNull(Balancer.getHostByRandomWeight(null));
+ Assert.assertNull(Balancer.getHostByRandomWeight(new ArrayList<>()));
+ }
+
+ @Test
+ public void testGetHostByRandomWeight() {
+ List list = new ArrayList<>();
+ Instance instance1 = new Instance();
+ list.add(instance1);
+ final Instance actual = Balancer.getHostByRandomWeight(list);
+ Assert.assertEquals(instance1, actual);
+ }
+
+ @Test
+ public void testSelectHost() {
+ List hosts = new ArrayList<>();
+ Instance instance1 = new Instance();
+ hosts.add(instance1);
+ ServiceInfo serviceInfo = new ServiceInfo();
+ serviceInfo.setHosts(hosts);
+
+ final Instance actual = Balancer.RandomByWeight.selectHost(serviceInfo);
+ Assert.assertEquals(instance1, actual);
+ }
+
+ @Test
+ public void testSelectHostEmpty() {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("no host to srv for serviceInfo: null");
+ ServiceInfo serviceInfo = new ServiceInfo();
+
+ Balancer.RandomByWeight.selectHost(serviceInfo);
+ }
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/core/ProtectModeTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/core/ProtectModeTest.java
new file mode 100644
index 000000000..3954d84c8
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/core/ProtectModeTest.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.client.naming.core;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ProtectModeTest {
+
+ @Test
+ public void testProtectThresholdDefault() {
+ final ProtectMode protectMode = new ProtectMode();
+ Assert.assertEquals(0.8f, protectMode.getProtectThreshold(), 0.01f);
+ }
+
+ @Test
+ public void testSetProtectThreshold() {
+ final ProtectMode protectMode = new ProtectMode();
+ float expect = 0.7f;
+ protectMode.setProtectThreshold(expect);
+ Assert.assertEquals(expect, protectMode.getProtectThreshold(), 0.01f);
+ }
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/core/PushReceiverTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/core/PushReceiverTest.java
new file mode 100644
index 000000000..91997a73b
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/core/PushReceiverTest.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.client.naming.core;
+
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder;
+import com.alibaba.nacos.common.utils.IoUtils;
+import com.alibaba.nacos.common.utils.JacksonUtils;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class PushReceiverTest {
+
+ @Test
+ public void testTestRunDomAndService() throws InterruptedException, IOException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ final PushReceiver pushReceiver = new PushReceiver(holder);
+ final ExecutorService executorService = Executors.newFixedThreadPool(1);
+ executorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ pushReceiver.run();
+ }
+ });
+ TimeUnit.MILLISECONDS.sleep(10);
+
+ PushReceiver.PushPacket pack1 = new PushReceiver.PushPacket();
+ pack1.type = "dom";
+ pack1.data = "pack1";
+ pack1.lastRefTime = 1;
+ final String res1 = udpClientRun(pack1, pushReceiver);
+ Assert.assertEquals("{\"type\": \"push-ack\", \"lastRefTime\":\"1\", \"data\":\"\"}", res1);
+ verify(holder, times(1)).processServiceInfo(pack1.data);
+
+ PushReceiver.PushPacket pack2 = new PushReceiver.PushPacket();
+ pack2.type = "service";
+ pack2.data = "pack2";
+ pack2.lastRefTime = 2;
+ final String res2 = udpClientRun(pack2, pushReceiver);
+ Assert.assertEquals("{\"type\": \"push-ack\", \"lastRefTime\":\"2\", \"data\":\"\"}", res2);
+ verify(holder, times(1)).processServiceInfo(pack2.data);
+
+ }
+
+ private String udpClientRun(PushReceiver.PushPacket pack, PushReceiver pushReceiver) throws IOException {
+ final int udpPort = pushReceiver.getUdpPort();
+ String json = JacksonUtils.toJson(pack);
+ final byte[] bytes = IoUtils.tryCompress(json, "UTF-8");
+ final DatagramSocket datagramSocket = new DatagramSocket();
+ datagramSocket.send(new DatagramPacket(bytes, bytes.length, InetAddress.getByName("localhost"), udpPort));
+ byte[] buffer = new byte[20480];
+ final DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
+ datagramSocket.receive(datagramPacket);
+ final byte[] data = datagramPacket.getData();
+ String res = new String(data, StandardCharsets.UTF_8);
+ return res.trim();
+ }
+
+ @Test
+ public void testTestRunWithDump() throws InterruptedException, IOException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ final PushReceiver pushReceiver = new PushReceiver(holder);
+ final ExecutorService executorService = Executors.newFixedThreadPool(1);
+ executorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ pushReceiver.run();
+ }
+ });
+ TimeUnit.MILLISECONDS.sleep(10);
+
+ PushReceiver.PushPacket pack1 = new PushReceiver.PushPacket();
+ pack1.type = "dump";
+ pack1.data = "pack1";
+ pack1.lastRefTime = 1;
+ final String res1 = udpClientRun(pack1, pushReceiver);
+ Assert.assertEquals("{\"type\": \"dump-ack\", \"lastRefTime\": \"1\", \"data\":\"{}\"}", res1);
+ verify(holder, times(1)).getServiceInfoMap();
+
+ }
+
+ @Test
+ public void testTestRunWithUnknown() throws InterruptedException, IOException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ final PushReceiver pushReceiver = new PushReceiver(holder);
+ final ExecutorService executorService = Executors.newFixedThreadPool(1);
+ executorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ pushReceiver.run();
+ }
+ });
+ TimeUnit.MILLISECONDS.sleep(10);
+
+ PushReceiver.PushPacket pack1 = new PushReceiver.PushPacket();
+ pack1.type = "unknown";
+ pack1.data = "pack1";
+ pack1.lastRefTime = 1;
+ final String res1 = udpClientRun(pack1, pushReceiver);
+ Assert.assertEquals("{\"type\": \"unknown-ack\", \"lastRefTime\":\"1\", \"data\":\"\"}", res1);
+ }
+
+ @Test
+ public void testShutdown() throws NacosException, NoSuchFieldException, IllegalAccessException {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ final PushReceiver pushReceiver = new PushReceiver(holder);
+
+ pushReceiver.shutdown();
+
+ final Field closed = PushReceiver.class.getDeclaredField("closed");
+ closed.setAccessible(true);
+ final boolean o = (boolean) closed.get(pushReceiver);
+ Assert.assertTrue(o);
+
+ }
+
+ @Test
+ public void testGetUdpPort() {
+ ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class);
+ final PushReceiver pushReceiver = new PushReceiver(holder);
+ final int udpPort = pushReceiver.getUdpPort();
+ System.out.println("udpPort = " + udpPort);
+ Assert.assertTrue(udpPort > 0);
+ }
+}
\ No newline at end of file
diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/core/ServerListManagerTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/core/ServerListManagerTest.java
new file mode 100644
index 000000000..02a8a89c3
--- /dev/null
+++ b/client/src/test/java/com/alibaba/nacos/client/naming/core/ServerListManagerTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.client.naming.core;
+
+import com.alibaba.nacos.api.PropertyKeyConst;
+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;
+
+import static org.mockito.ArgumentMatchers.any;
+
+public class ServerListManagerTest {
+
+ @Test
+ public void testConstructWithAddr() {
+ Properties properties = new Properties();
+ properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848,127.0.0.1:8849");
+ final ServerListManager serverListManager = new ServerListManager(properties);
+ final List serverList = serverListManager.getServerList();
+ Assert.assertEquals(2, serverList.size());
+ Assert.assertEquals("127.0.0.1:8848", serverList.get(0));
+ 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