Repository: incubator-eagle Updated Branches: refs/heads/master ca0fae407 -> 0952732e6
[EAGLE-723] Add unit test for configbus. - Add unit test for ConfigBus. - Add unit test for ZKConfigBuilder. https://issues.apache.org/jira/browse/EAGLE-723 Author: r7raul1984 <tangji...@yhd.com> Closes #609 from r7raul1984/EAGLE-723. Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/0952732e Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/0952732e Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/0952732e Branch: refs/heads/master Commit: 0952732e6bb0e428daaedbc6c0df0e6f19e86b86 Parents: ca0fae4 Author: r7raul1984 <tangji...@yhd.com> Authored: Mon Nov 7 14:49:11 2016 +0800 Committer: Hao Chen <h...@apache.org> Committed: Mon Nov 7 14:49:11 2016 +0800 ---------------------------------------------------------------------- .../eagle/alert/config/ConfigBusConsumer.java | 16 +++-- .../eagle/alert/config/TestConfigBus.java | 63 +++++++++++++------- .../eagle/alert/config/TestZKConfigBuilder.java | 37 ++++++++++++ .../src/test/resources/application.conf | 4 ++ 4 files changed, 95 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/0952732e/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/config/ConfigBusConsumer.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/config/ConfigBusConsumer.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/config/ConfigBusConsumer.java index d5e6b4e..b63cdd5 100644 --- a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/config/ConfigBusConsumer.java +++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/config/ConfigBusConsumer.java @@ -16,6 +16,7 @@ */ package org.apache.eagle.alert.config; +import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.curator.framework.recipes.cache.NodeCache; import org.slf4j.Logger; @@ -39,10 +40,9 @@ public class ConfigBusConsumer extends ConfigBusBase { LOG.info("monitor change for zkPath " + zkPath); cache = new NodeCache(curator, zkPath); cache.getListenable().addListener(() -> { - // get node value and notify callback - ConfigValue v = getConfigValue(); - callback.onNewConfig(v); - } + ConfigValue v = getConfigValue(); + callback.onNewConfig(v); + } ); try { cache.start(); @@ -54,7 +54,13 @@ public class ConfigBusConsumer extends ConfigBusBase { public ConfigValue getConfigValue() throws Exception { byte[] value = curator.getData().forPath(zkPath); - ConfigValue v = mapper.readValue(value, ConfigValue.class); + ConfigValue v; + try { + v = mapper.readValue(value, ConfigValue.class); + } catch (JsonParseException e) { + LOG.warn("warn getConfigValue parse exception", e.getMessage()); + v = new ConfigValue(); + } return v; } } http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/0952732e/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestConfigBus.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestConfigBus.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestConfigBus.java index a68af84..5c3f35e 100644 --- a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestConfigBus.java +++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestConfigBus.java @@ -17,45 +17,68 @@ package org.apache.eagle.alert.config; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.curator.test.TestingServer; +import org.apache.curator.utils.CloseableUtils; +import org.junit.After; import org.junit.Assert; -import org.junit.Ignore; +import org.junit.Before; import org.junit.Test; public class TestConfigBus { + protected TestingServer server; + protected ConfigBusProducer producer; + protected ConfigBusConsumer consumer; + protected String topic = "spout"; + protected ZKConfig config; + final AtomicBoolean validate = new AtomicBoolean(false); + final AtomicReference<String> configValue = new AtomicReference<>(); - @Ignore - @Test - public void testConfigChange() throws Exception { - String topic = "spout"; - ZKConfig config = new ZKConfig(); - config.zkQuorum = "localhost:2181"; + @Before + public void setUp() throws Exception { + server = new TestingServer(); + config = new ZKConfig(); + config.zkQuorum = server.getConnectString(); config.zkRoot = "/alert"; config.zkRetryInterval = 1000; config.zkRetryTimes = 3; config.connectionTimeoutMs = 3000; config.zkSessionTimeoutMs = 10000; - ConfigBusProducer producer = new ConfigBusProducer(config); - final AtomicBoolean validate = new AtomicBoolean(false); - ConfigBusConsumer consumer = new ConfigBusConsumer(config, topic, value -> { - validate.set(true); + producer = new ConfigBusProducer(config); + consumer = new ConfigBusConsumer(config, topic, value -> { + validate.set(value.isValueVersionId()); + configValue.set((String) value.getValue()); System.out.println("******** get notified of config " + value); }); + } + + @Test + public void testConfigChange() throws Exception { // first change - ConfigValue value = new ConfigValue(); - value.setValueVersionId(false); - value.setValue("testvalue1"); - producer.send(topic, value); + producer.send(topic, createConfigValue(false, "testvalue1")); Thread.sleep(1000); + Assert.assertFalse(validate.get()); + Assert.assertEquals("testvalue1", configValue.get()); // second change - value.setValueVersionId(false); - value.setValue("testvalue2"); - producer.send(topic, value); - producer.close(); + producer.send(topic, createConfigValue(true, "testvalue2")); Thread.sleep(1000); + Assert.assertEquals("testvalue2", configValue.get()); + } + + @After + public void shutdown() { + CloseableUtils.closeQuietly(server); + producer.close(); consumer.close(); - Assert.assertTrue(validate.get()); + } + + private ConfigValue createConfigValue(boolean isValueVersionId, String value) { + ConfigValue configValue = new ConfigValue(); + configValue.setValueVersionId(isValueVersionId); + configValue.setValue(value); + return configValue; } } http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/0952732e/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestZKConfigBuilder.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestZKConfigBuilder.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestZKConfigBuilder.java new file mode 100644 index 0000000..64edeb7 --- /dev/null +++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/java/org/apache/eagle/alert/config/TestZKConfigBuilder.java @@ -0,0 +1,37 @@ +/* + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.eagle.alert.config; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import org.junit.Assert; +import org.junit.Test; + +public class TestZKConfigBuilder { + @Test + public void testZKConfigBuilder() { + Config config = ConfigFactory.load(); + ZKConfig zKConfig = ZKConfigBuilder.getZKConfig(config); + Assert.assertEquals("localhost:2181", zKConfig.zkQuorum); + Assert.assertEquals("/alert", zKConfig.zkRoot); + Assert.assertEquals(10000, zKConfig.zkSessionTimeoutMs); + Assert.assertEquals(10000, zKConfig.connectionTimeoutMs); + Assert.assertEquals(3, zKConfig.zkRetryTimes); + Assert.assertEquals(3000, zKConfig.zkRetryInterval); + } +} http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/0952732e/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/application.conf ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/application.conf b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/application.conf index 71735d4..b763acf 100644 --- a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/application.conf +++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/test/resources/application.conf @@ -32,4 +32,8 @@ } } } + zkConfig { + "zkQuorum": "localhost:2181" + "zkRoot": "/alert" + } } \ No newline at end of file