From 7d0baaa46f7da5cfbcde6936f02662932d823486 Mon Sep 17 00:00:00 2001 From: Norbert Kalmar Date: Thu, 11 Apr 2019 16:00:13 +0200 Subject: [PATCH] ZOOKEEPER-3037 - refactor unit tests --- .../apache/zookeeper/ServerConfigTest.java | 23 ++++ .../server/quorum/QuorumPeerConfigTest.java | 28 ++++- .../server/util/JvmPauseMonitorTest.java | 107 +++--------------- 3 files changed, 61 insertions(+), 97 deletions(-) diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ServerConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ServerConfigTest.java index 27faa745b6f..e542670481a 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/ServerConfigTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ServerConfigTest.java @@ -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; @@ -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); diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java index f480f070da4..02e2cf3d0f7 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java @@ -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; @@ -32,6 +28,8 @@ import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; import org.junit.Test; +import static org.junit.Assert.*; + public class QuorumPeerConfigTest { /** @@ -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()); diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/JvmPauseMonitorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/JvmPauseMonitorTest.java index 8e62d4df3b2..fba65f483e4 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/JvmPauseMonitorTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/JvmPauseMonitorTest.java @@ -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(); } }