From 55b9b8578c3333d806b178cd95a2d40cf97574d9 Mon Sep 17 00:00:00 2001 From: franz1981 Date: Tue, 20 Apr 2021 16:32:20 +0200 Subject: [PATCH] CURATOR-594: TestingZooKeeperMain isn't setting tickTime, if configured --- .../apache/curator/test/TestingServer.java | 4 ++ .../curator/test/TestingZooKeeperMain.java | 6 +++ .../curator/test/TestingZooKeeperServer.java | 4 ++ .../curator/test/TestTestingServer.java | 51 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 curator-test/src/test/java/org/apache/curator/test/TestTestingServer.java diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingServer.java b/curator-test/src/main/java/org/apache/curator/test/TestingServer.java index 5228c8e837..9d24243575 100644 --- a/curator-test/src/main/java/org/apache/curator/test/TestingServer.java +++ b/curator-test/src/main/java/org/apache/curator/test/TestingServer.java @@ -31,6 +31,10 @@ public class TestingServer implements Closeable private final TestingZooKeeperServer testingZooKeeperServer; private final InstanceSpec spec; + TestingZooKeeperServer getTestingZooKeeperServer() { + return testingZooKeeperServer; + } + /** * Create the server using a random port * diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java index cdf59d07be..b1a873a16b 100644 --- a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java +++ b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperMain.java @@ -97,6 +97,10 @@ public void kill() } } + TestZooKeeperServer getZkServer() { + return zkServer; + } + @Override public void runFromConfig(QuorumPeerConfig config) throws Exception { @@ -271,6 +275,8 @@ public TestZooKeeperServer(FileTxnSnapLog txnLog, ServerConfig config) { this.txnLog = txnLog; this.setTxnLogFactory(txnLog); + // tickTime would affect min and max session timeout: should be set first + this.setTickTime(config.getTickTime()); this.setMinSessionTimeout(config.getMinSessionTimeout()); this.setMaxSessionTimeout(config.getMaxSessionTimeout()); } diff --git a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java index 58cf8d4e20..e80566ad63 100644 --- a/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java +++ b/curator-test/src/main/java/org/apache/curator/test/TestingZooKeeperServer.java @@ -40,6 +40,10 @@ public class TestingZooKeeperServer implements Closeable private volatile ZooKeeperMainFace main; private final AtomicReference state = new AtomicReference(State.LATENT); + ZooKeeperMainFace getMain() { + return main; + } + private enum State { LATENT, STARTED, STOPPED, CLOSED diff --git a/curator-test/src/test/java/org/apache/curator/test/TestTestingServer.java b/curator-test/src/test/java/org/apache/curator/test/TestTestingServer.java new file mode 100644 index 0000000000..95937debf8 --- /dev/null +++ b/curator-test/src/test/java/org/apache/curator/test/TestTestingServer.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.curator.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; + +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.jupiter.api.Test; + +public class TestTestingServer { + + @Test + public void setCustomTickTimeTest() throws Exception { + final int defaultZkTickTime = ZooKeeperServer.DEFAULT_TICK_TIME; + final int customTickMs; + if (defaultZkTickTime > 0) { + customTickMs = defaultZkTickTime + (defaultZkTickTime == Integer.MAX_VALUE ? -1 : +1); + } else { + customTickMs = 100; + } + final InstanceSpec spec = new InstanceSpec(null, -1, -1, -1, true, -1, customTickMs, -1); + final int zkTickTime; + try (TestingServer testingServer = new TestingServer(spec, true)) { + final File tmpDir = testingServer.getTempDirectory(); + tmpDir.deleteOnExit(); + TestingZooKeeperMain main = (TestingZooKeeperMain) testingServer.getTestingZooKeeperServer().getMain(); + zkTickTime = main.getZkServer().getTickTime(); + } + assertEquals(customTickMs, zkTickTime); + } +}