Skip to content

Commit

Permalink
ZOOKEEPER-3037 - refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalmar committed Apr 11, 2019
1 parent 97d2c61 commit 7d0baaa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
package org.apache.zookeeper;

import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;

Expand Down Expand Up @@ -60,6 +63,26 @@ public void testTooManyArguments() {
serverConfig.parse(args);
}

@Test
public void testJvmPauseMonitorConfigured() {
final Long sleepTime = 444L;
final Long warnTH = 5555L;
final Long infoTH = 555L;

QuorumPeerConfig qpConfig = mock(QuorumPeerConfig.class);
when(qpConfig.isJvmPauseMonitorToRun()).thenReturn(true);
when(qpConfig.getJvmPauseSleepTimeMs()).thenReturn(sleepTime);
when(qpConfig.getJvmPauseWarnThresholdMs()).thenReturn(warnTH);
when(qpConfig.getJvmPauseInfoThresholdMs()).thenReturn(infoTH);

serverConfig.readFrom(qpConfig);

assertEquals(sleepTime, Long.valueOf(serverConfig.getJvmPauseSleepTimeMs()));
assertEquals(warnTH, Long.valueOf(serverConfig.getJvmPauseWarnThresholdMs()));
assertEquals(infoTH, Long.valueOf(serverConfig.getJvmPauseInfoThresholdMs()));
assertTrue(serverConfig.isJvmPauseMonitorToRun());
}

boolean checkEquality(String a, String b) {
assertNotNull(a);
assertNotNull(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package org.apache.zookeeper.server.quorum;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -32,6 +28,8 @@
import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
import org.junit.Test;

import static org.junit.Assert.*;

public class QuorumPeerConfigTest {

/**
Expand Down Expand Up @@ -148,6 +146,28 @@ public void testClientAddrFromClientPort()
assertEquals(quorumPeerConfig.getClientPortAddress(), qs.clientAddr);
}

@Test
public void testJvmPauseMonitorConfigured()
throws IOException, ConfigException {
final Long sleepTime = 444L;
final Long warnTH = 5555L;
final Long infoTH = 555L;

QuorumPeerConfig quorumPeerConfig = new MockQuorumPeerConfig(1);
Properties zkProp = getDefaultZKProperties();
zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath());
zkProp.setProperty("jvm.pause.monitor", "true");
zkProp.setProperty("jvm.pause.sleep.time.ms", sleepTime.toString());
zkProp.setProperty("jvm.pause.warn-threshold.ms", warnTH.toString());
zkProp.setProperty("jvm.pause.info-threshold.ms", infoTH.toString());
quorumPeerConfig.parseProperties(zkProp);

assertEquals(sleepTime, Long.valueOf(quorumPeerConfig.getJvmPauseSleepTimeMs()));
assertEquals(warnTH, Long.valueOf(quorumPeerConfig.getJvmPauseWarnThresholdMs()));
assertEquals(infoTH, Long.valueOf(quorumPeerConfig.getJvmPauseInfoThresholdMs()));
assertTrue(quorumPeerConfig.isJvmPauseMonitorToRun());
}

private Properties getDefaultZKProperties() {
Properties zkProp = new Properties();
zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,116 +18,37 @@

package org.apache.zookeeper.server.util;

import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class JvmPauseMonitorTest {

@Test
public void testJvmPauseMonitorInit() {
final Long sleepTime = 444L;
final Long warnTH = 5555L;
final Long infoTH = 555L;

ServerConfig serverConfig = new ServerConfig();
QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();

Assert.assertEquals(false, serverConfig.isJvmPauseMonitorToRun());
Assert.assertEquals(false, quorumPeerConfig.isJvmPauseMonitorToRun());

Properties zkProp = new Properties();
zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath());
zkProp.setProperty("jvm.pause.monitor", "true");
zkProp.setProperty("jvm.pause.sleep.time.ms", sleepTime.toString());
zkProp.setProperty("jvm.pause.warn-threshold.ms", warnTH.toString());
zkProp.setProperty("jvm.pause.info-threshold.ms", infoTH.toString());
try {
quorumPeerConfig.parseProperties(zkProp);
} catch (IOException | QuorumPeerConfig.ConfigException e) {
Assert.fail("Exception while reading config for JvmPauseMonitor");
}
serverConfig.readFrom(quorumPeerConfig);

Assert.assertEquals(true, serverConfig.isJvmPauseMonitorToRun());
Assert.assertEquals(true, quorumPeerConfig.isJvmPauseMonitorToRun());

JvmPauseMonitor pauseMonitor = new JvmPauseMonitor(serverConfig);
Assert.assertFalse(pauseMonitor.isStarted());
pauseMonitor.serviceStart();
Assert.assertTrue(pauseMonitor.isStarted());

Assert.assertEquals(sleepTime, Long.valueOf(pauseMonitor.sleepTimeMs));
Assert.assertEquals(warnTH, Long.valueOf(pauseMonitor.warnThresholdMs));
Assert.assertEquals(infoTH, Long.valueOf(pauseMonitor.infoThresholdMs));
}

@Test
public void testJvmPauseMonitorExceedInfoThreshold() throws InterruptedException {
@Test(timeout=5000)
public void testJvmPauseMonitorExceedThreshold() throws InterruptedException {
final Long sleepTime = 100L;
final Long warnTH = -1L;
final Long infoTH = -1L;

ServerConfig serverConfig = new ServerConfig();
QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();

Properties zkProp = new Properties();
zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath());
zkProp.setProperty("jvm.pause.monitor", "true");
zkProp.setProperty("jvm.pause.sleep.time.ms", sleepTime.toString());
zkProp.setProperty("jvm.pause.info-threshold.ms", infoTH.toString());
try {
quorumPeerConfig.parseProperties(zkProp);
} catch (IOException | QuorumPeerConfig.ConfigException e) {
Assert.fail("Exception while reading config for JvmPauseMonitor");
}
serverConfig.readFrom(quorumPeerConfig);
QuorumPeerConfig qpConfig = mock(QuorumPeerConfig.class);
when(qpConfig.getJvmPauseSleepTimeMs()).thenReturn(sleepTime);
when(qpConfig.getJvmPauseWarnThresholdMs()).thenReturn(warnTH);
when(qpConfig.getJvmPauseInfoThresholdMs()).thenReturn(infoTH);

JvmPauseMonitor pauseMonitor = new JvmPauseMonitor(serverConfig);
JvmPauseMonitor pauseMonitor = new JvmPauseMonitor(qpConfig);
pauseMonitor.serviceStart();

Assert.assertEquals(sleepTime, Long.valueOf(pauseMonitor.sleepTimeMs));
Assert.assertEquals(warnTH, Long.valueOf(pauseMonitor.warnThresholdMs));
Assert.assertEquals(infoTH, Long.valueOf(pauseMonitor.infoThresholdMs));

Thread.sleep(1000);

Assert.assertNotEquals(0L, pauseMonitor.getNumGcInfoThresholdExceeded());
Assert.assertEquals(0L, pauseMonitor.getNumGcWarnThresholdExceeded());
}

@Test
public void testJvmPauseMonitorExceedWarnThreshold() throws InterruptedException {
final Long sleepTime = 100L;
final Long warnTH = -1L;

ServerConfig serverConfig = new ServerConfig();
QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();

Properties zkProp = new Properties();
zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath());
zkProp.setProperty("jvm.pause.monitor", "true");
zkProp.setProperty("jvm.pause.sleep.time.ms", sleepTime.toString());
zkProp.setProperty("jvm.pause.warn-threshold.ms", warnTH.toString());
try {
quorumPeerConfig.parseProperties(zkProp);
} catch (IOException | QuorumPeerConfig.ConfigException e) {
Assert.fail("Exception while reading config for JvmPauseMonitor");
while(pauseMonitor.getNumGcInfoThresholdExceeded() == 0 && pauseMonitor.getNumGcWarnThresholdExceeded() == 0) {
Thread.sleep(200);
}
serverConfig.readFrom(quorumPeerConfig);

JvmPauseMonitor pauseMonitor = new JvmPauseMonitor(serverConfig);
pauseMonitor.serviceStart();

Assert.assertEquals(sleepTime, Long.valueOf(pauseMonitor.sleepTimeMs));
Assert.assertEquals(warnTH, Long.valueOf(pauseMonitor.warnThresholdMs));

Thread.sleep(1000);

Assert.assertEquals(0L, pauseMonitor.getNumGcInfoThresholdExceeded());
Assert.assertNotEquals(0L, pauseMonitor.getNumGcWarnThresholdExceeded());
pauseMonitor.serviceStop();
}
}

0 comments on commit 7d0baaa

Please sign in to comment.