Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 85 additions & 16 deletions framework/src/test/java/org/tron/common/backup/BackupManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tron.common.backup;

import io.netty.channel.Channel;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
Expand All @@ -11,7 +12,9 @@
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.BooleanSupplier;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -35,6 +38,7 @@ public class BackupManagerTest {
private BackupManager manager;
private BackupServer backupServer;
private BiFunction<String, Boolean, InetAddress> savedLookup;
private boolean backupServerClosed;

@Before
public void setUp() throws Exception {
Expand All @@ -47,9 +51,34 @@ public void setUp() throws Exception {
}

@After
public void tearDown() {
InetUtil.dnsLookup = savedLookup;
Args.clearParam();
public void tearDown() throws Exception {
Throwable failure = null;
try {
if (!backupServerClosed && backupServer != null) {
backupServer.close();
}
} catch (Throwable t) {
failure = t;
} finally {
try {
if (manager != null) {
manager.stop();
}
assertExecutorsTerminated();
} catch (Throwable t) {
if (failure == null) {
failure = t;
} else {
failure.addSuppressed(t);
}
} finally {
InetUtil.dnsLookup = savedLookup;
Args.clearParam();
}
}
if (failure != null) {
throw new AssertionError("backup test cleanup failed", failure);
}
}

@Test
Expand Down Expand Up @@ -121,7 +150,7 @@ public void test() throws Exception {
}

@Test
public void testSendKeepAliveMessage() throws Exception {
public void testBackupServerLifecycleDuringKeepAliveInterval() throws Exception {
CommonParameter parameter = CommonParameter.getInstance();
parameter.setBackupPriority(8);
List<String> members = new ArrayList<>();
Expand All @@ -134,21 +163,21 @@ public void testSendKeepAliveMessage() throws Exception {

Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.MASTER);
backupServer.initServer();
awaitCondition("backup channel to become active", () -> getChannel(backupServer) != null
&& getChannel(backupServer).isActive());
awaitCondition("backup message handler assignment", () -> getFieldValue(manager,
"messageHandler") != null);
manager.init();

Thread.sleep(parameter.getKeepAliveInterval() + 1000);//test send KeepAliveMessage

field = manager.getClass().getDeclaredField("executorService");
field.setAccessible(true);
ScheduledExecutorService executorService = (ScheduledExecutorService) field.get(manager);
executorService.shutdown();

Field field2 = backupServer.getClass().getDeclaredField("executor");
field2.setAccessible(true);
ExecutorService executorService2 = (ExecutorService) field2.get(backupServer);
executorService2.shutdown();
long keepAliveDeadline = System.nanoTime()
+ TimeUnit.MILLISECONDS.toNanos(parameter.getKeepAliveInterval() + 1000L);
awaitCondition("keep-alive interval", () -> System.nanoTime() >= keepAliveDeadline);

Assert.assertEquals(BackupManager.BackupStatusEnum.INIT, manager.getStatus());
Channel channel = getChannel(backupServer);
backupServer.close();
backupServerClosed = true;
Assert.assertFalse("backup channel must close", channel.isOpen());
assertExecutorsTerminated();
}

// ===== domain-handling tests for init() =====
Expand Down Expand Up @@ -254,4 +283,44 @@ private void invokeRefreshMemberIps(BackupManager mgr) throws Exception {
m.setAccessible(true);
m.invoke(mgr);
}

private Channel getChannel(BackupServer server) {
try {
return getField(server, "channel");
} catch (Exception e) {
throw new AssertionError("cannot inspect backup channel", e);
}
}

private Object getFieldValue(Object target, String name) {
try {
return getField(target, name);
} catch (Exception e) {
throw new AssertionError("cannot inspect " + name, e);
}
}

private void assertExecutorsTerminated() throws Exception {
if (manager == null || backupServer == null) {
return;
}
ScheduledExecutorService managerExecutor = getField(manager, "executorService");
Assert.assertTrue("backup manager executor must terminate", managerExecutor.isTerminated());
ExecutorService serverExecutor = getField(backupServer, "executor");
if (serverExecutor != null) {
Assert.assertTrue("backup server executor must terminate", serverExecutor.isTerminated());
}
}

private void awaitCondition(String description, BooleanSupplier condition) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
while (System.nanoTime() < deadline) {
if (condition.getAsBoolean()) {
return;
}
Thread.sleep(20);
}
Assert.fail("timed out waiting for " + description);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.tron.common.backup;

import io.netty.channel.Channel;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -23,6 +29,8 @@ public class BackupServerTest {
@Rule
public Timeout globalTimeout = Timeout.seconds(60);
private BackupServer backupServer;
private BackupManager backupManager;
private boolean backupServerClosed;

@Before
public void setUp() throws Exception {
Expand All @@ -32,21 +40,91 @@ public void setUp() throws Exception {
List<String> members = new ArrayList<>();
members.add("127.0.0.2");
CommonParameter.getInstance().setBackupMembers(members);
BackupManager backupManager = new BackupManager();
backupManager = new BackupManager();
backupManager.init();
backupServer = new BackupServer(backupManager);
}

@After
public void tearDown() {
backupServer.close();
Args.clearParam();
public void tearDown() throws Exception {
Throwable failure = null;
try {
if (!backupServerClosed && backupServer != null) {
backupServer.close();
}
} catch (Throwable t) {
failure = t;
} finally {
try {
if (backupManager != null) {
backupManager.stop();
}
assertExecutorsTerminated();
} catch (Throwable t) {
if (failure == null) {
failure = t;
} else {
failure.addSuppressed(t);
}
} finally {
Args.clearParam();
}
}
if (failure != null) {
throw new AssertionError("backup test cleanup failed", failure);
}
}

@Test(timeout = 60_000)
public void test() throws InterruptedException {
public void test() throws Exception {
backupServer.initServer();
// wait for the server to start so channel is assigned before close() is called
Thread.sleep(1000);
awaitCondition("backup channel to become active", () -> getChannel() != null
&& getChannel().isActive());
Channel channel = getChannel();
Assert.assertTrue("backup channel must be active after startup", channel.isActive());

backupServer.close();
backupServerClosed = true;

Assert.assertFalse("backup channel must close", channel.isOpen());
assertExecutorsTerminated();
}

private Channel getChannel() {
try {
return getField(backupServer, "channel");
} catch (Exception e) {
throw new AssertionError("cannot inspect backup channel", e);
}
}

private void assertExecutorsTerminated() throws Exception {
if (backupManager == null || backupServer == null) {
return;
}
ExecutorService managerExecutor = getField(backupManager, "executorService");
Assert.assertTrue("backup manager executor must terminate", managerExecutor.isTerminated());
ExecutorService serverExecutor = getField(backupServer, "executor");
if (serverExecutor != null) {
Assert.assertTrue("backup server executor must terminate", serverExecutor.isTerminated());
}
}

private void awaitCondition(String description, BooleanSupplier condition) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
while (System.nanoTime() < deadline) {
if (condition.getAsBoolean()) {
return;
}
Thread.sleep(20);
}
Assert.fail("timed out waiting for " + description);
}

@SuppressWarnings("unchecked")
private <T> T getField(Object target, String name) throws Exception {
Field field = target.getClass().getDeclaredField(name);
field.setAccessible(true);
return (T) field.get(target);
}
}
Loading
Loading