forked from eclipse/ConfigJSR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…anges This is just the basic approach for now
- Loading branch information
Showing
4 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
tck/src/main/java/org/eclipse/configjsr/DynamicConfigSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2016-2017 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed 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.eclipse.configjsr; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.config.Config; | ||
import javax.config.spi.ConfigSource; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.configjsr.dynamic.DynamicChangeConfigSource; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.testng.Arquillian; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Mark Struberg</a> | ||
*/ | ||
public class DynamicConfigSourceTest extends Arquillian { | ||
|
||
private @Inject Config config; | ||
|
||
@Deployment | ||
public static WebArchive deploy() { | ||
JavaArchive testJar = ShrinkWrap | ||
.create(JavaArchive.class, "dynamicValuesTest.jar") | ||
.addClass(DynamicConfigSourceTest.class) | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") | ||
.addAsServiceProvider(ConfigSource.class, DynamicChangeConfigSource.class) | ||
.as(JavaArchive.class); | ||
|
||
|
||
WebArchive war = ShrinkWrap | ||
.create(WebArchive.class, "dynamicValuesTest.war") | ||
.addAsLibrary(testJar); | ||
return war; | ||
} | ||
|
||
|
||
@Test | ||
public void testBgCount() throws Exception { | ||
Integer value = config.getValue(DynamicChangeConfigSource.TEST_ATTRIBUTE, Integer.class); | ||
Thread.sleep(20L); | ||
Integer value2 = config.getValue(DynamicChangeConfigSource.TEST_ATTRIBUTE, Integer.class); | ||
assertTrue(value2 > value); | ||
} | ||
|
||
@Test | ||
public void testBgCallback() throws Exception { | ||
Integer value = config.getValue(DynamicChangeConfigSource.TEST_ATTRIBUTE, Integer.class); | ||
Map<String, Integer> vals = new ConcurrentHashMap<>(); | ||
|
||
|
||
config.registerConfigChangedListener(s -> s.forEach(k -> vals.put(k, config.getValue(k, Integer.class)))); | ||
vals.clear(); | ||
|
||
Thread.sleep(12L); | ||
assertEquals(1, vals.size()); | ||
|
||
int i1 = vals.get(DynamicChangeConfigSource.TEST_ATTRIBUTE); | ||
|
||
Thread.sleep(15L); | ||
assertEquals(1, vals.size()); | ||
|
||
int i2 = vals.get(DynamicChangeConfigSource.TEST_ATTRIBUTE); | ||
assertTrue(i2 > i1); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
tck/src/main/java/org/eclipse/configjsr/dynamic/DynamicChangeConfigSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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.eclipse.configjsr.dynamic; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.Consumer; | ||
|
||
import javax.config.spi.ConfigSource; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Mark Struberg</a> | ||
*/ | ||
public class DynamicChangeConfigSource implements ConfigSource, Closeable { | ||
public static final String TEST_ATTRIBUTE = "tck.config.test.javaconfig.dynymic.testattrib"; | ||
|
||
private Consumer<Set<String>> reportAttributeChange; | ||
private AtomicInteger i = new AtomicInteger(0); | ||
private Map<String, String> properties = new HashMap<>(); | ||
private Thread worker; | ||
|
||
public DynamicChangeConfigSource() { | ||
// start a new backgroundthread. | ||
worker = new Thread() { | ||
@Override | ||
public void run() { | ||
while (i.incrementAndGet() < 10_000) { | ||
reportAttributeChange.accept(Collections.singleton(TEST_ATTRIBUTE)); | ||
try { | ||
Thread.sleep(10L); | ||
} | ||
catch (InterruptedException e) { | ||
return; | ||
} | ||
} | ||
} | ||
}; | ||
worker.start(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
i.set(10_001); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getProperties() { | ||
properties.put(TEST_ATTRIBUTE, Integer.toString(i.get())); | ||
return properties; | ||
} | ||
|
||
@Override | ||
public String getValue(String propertyName) { | ||
return getProperties().get(propertyName); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.getClass().getSimpleName(); | ||
} | ||
|
||
@Override | ||
public void setOnAttributeChange(Consumer<Set<String>> reportAttributeChange) { | ||
this.reportAttributeChange = reportAttributeChange; | ||
} | ||
|
||
} |