Skip to content

Commit

Permalink
Add config test
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahmily committed Nov 26, 2019
1 parent 2d6a2c4 commit 49e6581
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 8 deletions.
5 changes: 5 additions & 0 deletions oap-server/server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<artifactId>grpc-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
import org.yaml.snakeyaml.Yaml;

/**
* Apdex threshold configuration dictionary adapter.
* Looking up a service apdex threshold from dynamic config service.
*
* @author hongtaogao
*/
@Slf4j
public class ApdexThresholdConfig extends ConfigChangeWatcher implements ConfigurationDictionary {
Expand All @@ -54,14 +57,6 @@ public ApdexThresholdConfig(final CoreModuleProvider provider) {
}
}

@SuppressWarnings("unchecked")
private void updateConfig(final Reader contentRender) {
dictionary = (Map<String, Integer>)new Yaml().load(contentRender);
if (dictionary == null) {
dictionary = Collections.emptyMap();
}
}

@Override public Number lookup(String name) {
int t = dictionary.getOrDefault(name, -1);
if (t < 0) {
Expand Down Expand Up @@ -96,4 +91,12 @@ private synchronized void activeSetting(String config) {
rawConfig = config;
updateConfig(new StringReader(config));
}

@SuppressWarnings("unchecked")
private void updateConfig(final Reader contentRender) {
dictionary = (Map<String, Integer>)new Yaml().load(contentRender);
if (dictionary == null) {
dictionary = Collections.emptyMap();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.core.analysis;

import java.util.Set;
import org.apache.skywalking.oap.server.configuration.api.ConfigTable;
import org.apache.skywalking.oap.server.configuration.api.ConfigWatcherRegister;
import org.apache.skywalking.oap.server.core.CoreModuleProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ApdexThresholdConfigTest {

@Mock
private CoreModuleProvider provider;

@Test
public void testLookupOfBeforeInit() {
ApdexThresholdConfig config = new ApdexThresholdConfig(provider);
assertThat(config.lookup("foo"), is(500));
assertThat(config.lookup("default"), is(500));
assertThat(config.lookup("bar"), is(500));
}

@Test(timeout = 20000)
public void testLookupOfDynamicUpdate() throws InterruptedException {
ConfigWatcherRegister register = new MockConfigWatcherRegister(3);
when(provider.name()).thenReturn("default");
ApdexThresholdConfig config = new ApdexThresholdConfig(provider);
register.registerConfigChangeWatcher(config);
register.start();

while (config.lookup("foo").intValue() == 500) {
Thread.sleep(2000);
}
assertThat(config.lookup("foo"), is(200));
assertThat(config.lookup("default"), is(1000));
assertThat(config.lookup("bar"), is(1000));
}

public static class MockConfigWatcherRegister extends ConfigWatcherRegister {

public MockConfigWatcherRegister(long syncPeriod) {
super(syncPeriod);
}

@Override public ConfigTable readConfig(Set<String> keys) {
ConfigTable table = new ConfigTable();
table.add(new ConfigTable.ConfigItem("core.default.apdexThreshold", "default: 1000 \nfoo: 200"));
return table;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

# default threshold is 500ms
default: 500
# example:
# the threshold of service "tomcat" is 1s
# tomcat: 1000
# the threshold of service "springboot1" is 50ms
# springboot1: 50

0 comments on commit 49e6581

Please sign in to comment.