You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2019/05/31 07:43:13 UTC

[skywalking] branch config-API updated: Finish mock tests.

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch config-API
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/config-API by this push:
     new dd53be3  Finish mock tests.
dd53be3 is described below

commit dd53be305b15880dee2afc4b913ae43b972b7d70
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Fri May 31 15:43:03 2019 +0800

    Finish mock tests.
---
 .../oap/server/configuration/api/ConfigTable.java  |   5 +
 .../configuration/api/ConfigWatcherRegister.java   |   9 +-
 .../api/ConfigWatcherRegisterTest.java             | 135 +++++++++++++++++++++
 3 files changed, 145 insertions(+), 4 deletions(-)

diff --git a/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigTable.java b/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigTable.java
index f4307f8..742f44f 100644
--- a/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigTable.java
+++ b/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigTable.java
@@ -39,5 +39,10 @@ public class ConfigTable {
     public static class ConfigItem {
         private String name;
         private String value;
+
+        public ConfigItem(String name, String value) {
+            this.name = name;
+            this.value = value;
+        }
     }
 }
diff --git a/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigWatcherRegister.java b/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigWatcherRegister.java
index 509a3f9..3ff2250 100644
--- a/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigWatcherRegister.java
+++ b/oap-server/server-configuration/configuration-api/src/main/java/org/apache/skywalking/oap/server/configuration/api/ConfigWatcherRegister.java
@@ -68,7 +68,7 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi
                 t -> logger.error("Sync config center error.", t)), syncPeriod, syncPeriod, TimeUnit.SECONDS);
     }
 
-    private void configSync() {
+    void configSync() {
         ConfigTable configTable = readConfig();
 
         configTable.getItems().forEach(item -> {
@@ -117,11 +117,12 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi
         @Override public String toString() {
             StringBuilder registerTableDescription = new StringBuilder();
             registerTableDescription.append("Following dynamic config items are available.").append(LINE_SEPARATOR);
+            registerTableDescription.append("---------------------------------------------").append(LINE_SEPARATOR);
             register.forEach((key, holder) -> {
                 ConfigChangeWatcher watcher = holder.getWatcher();
                 registerTableDescription.append("key:").append(key)
-                    .append("    module:").append(watcher.getModule())
-                    .append("    provider:").append(watcher.getProvider())
+                    .append("    module:").append(watcher.getModule().name())
+                    .append("    provider:").append(watcher.getProvider().name())
                     .append("    value(current):").append(watcher.value())
                     .append(LINE_SEPARATOR);
             });
@@ -136,7 +137,7 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi
 
         public WatcherHolder(ConfigChangeWatcher watcher) {
             this.watcher = watcher;
-            this.key = String.join("-", watcher.getModule().name(), watcher.getProvider().name(), watcher.getItemName());
+            this.key = String.join(".", watcher.getModule().name(), watcher.getProvider().name(), watcher.getItemName());
         }
     }
 }
diff --git a/oap-server/server-configuration/configuration-api/src/test/java/org/apache/skywalking/oap/server/configuration/api/ConfigWatcherRegisterTest.java b/oap-server/server-configuration/configuration-api/src/test/java/org/apache/skywalking/oap/server/configuration/api/ConfigWatcherRegisterTest.java
new file mode 100644
index 0000000..2cb9a8d
--- /dev/null
+++ b/oap-server/server-configuration/configuration-api/src/test/java/org/apache/skywalking/oap/server/configuration/api/ConfigWatcherRegisterTest.java
@@ -0,0 +1,135 @@
+/*
+ * 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.skywalking.oap.server.configuration.api;
+
+import org.apache.skywalking.oap.server.library.module.*;
+import org.junit.*;
+import org.powermock.reflect.Whitebox;
+
+/**
+ * @author wusheng
+ */
+public class ConfigWatcherRegisterTest {
+    private ConfigWatcherRegister register;
+
+    @Before
+    public void setup() {
+        register = new MockConfigWatcherRegister();
+    }
+
+    @After
+    public void tearDown() {
+        register = null;
+    }
+
+    @Test
+    public void testInit() {
+        final String[] newValue = new String[1];
+
+        register.registerConfigChangeWatcher(new ConfigChangeWatcher(new MockModule(), new MockProvider(), "prop2") {
+            @Override public void notify(ConfigChangeEvent value) {
+                newValue[0] = value.getNewValue();
+            }
+
+            @Override public String value() {
+                return null;
+            }
+        });
+
+        register.configSync();
+
+        Assert.assertEquals("abc2", newValue[0]);
+    }
+
+    @Test
+    public void testRegisterTableLog() {
+        register.registerConfigChangeWatcher(new ConfigChangeWatcher(new MockModule(), new MockProvider(), "prop2") {
+            @Override public void notify(ConfigChangeEvent value) {
+            }
+
+            @Override public String value() {
+                return null;
+            }
+        });
+
+        register.configSync();
+        ConfigWatcherRegister.Register registerTable = Whitebox.getInternalState(this.register, "register");
+
+        String expected = "Following dynamic config items are available." + ConfigWatcherRegister.LINE_SEPARATOR +
+            "---------------------------------------------" + ConfigWatcherRegister.LINE_SEPARATOR +
+            "key:MockModule.provider.prop2    module:MockModule    provider:provider    value(current):null" + ConfigWatcherRegister.LINE_SEPARATOR;
+
+        Assert.assertEquals(expected, registerTable.toString());
+    }
+
+    public static class MockConfigWatcherRegister extends ConfigWatcherRegister {
+
+        @Override public ConfigTable readConfig() {
+            ConfigTable.ConfigItem item1 = new ConfigTable.ConfigItem("module.provider.prop1", "abc");
+            ConfigTable.ConfigItem item2 = new ConfigTable.ConfigItem("MockModule.provider.prop2", "abc2");
+
+            ConfigTable table = new ConfigTable();
+            table.add(item1);
+            table.add(item2);
+            return table;
+        }
+    }
+
+    public static class MockModule extends ModuleDefine {
+
+        public MockModule() {
+            super("MockModule");
+        }
+
+        @Override public Class[] services() {
+            return new Class[0];
+        }
+    }
+
+    public static class MockProvider extends ModuleProvider {
+
+        @Override public String name() {
+            return "provider";
+        }
+
+        @Override public Class<? extends ModuleDefine> module() {
+            return MockModule.class;
+        }
+
+        @Override public ModuleConfig createConfigBeanIfAbsent() {
+            return null;
+        }
+
+        @Override public void prepare() throws ServiceNotProvidedException, ModuleStartException {
+
+        }
+
+        @Override public void start() throws ServiceNotProvidedException, ModuleStartException {
+
+        }
+
+        @Override public void notifyAfterCompleted() throws ServiceNotProvidedException, ModuleStartException {
+
+        }
+
+        @Override public String[] requiredModules() {
+            return new String[0];
+        }
+    }
+}