-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Constantin Muraru
committed
Apr 15, 2016
1 parent
60c0a8c
commit e84bc6d
Showing
5 changed files
with
128 additions
and
0 deletions.
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
105 changes: 105 additions & 0 deletions
105
...pesafe/src/test/java/com/netflix/archaius/typesafe/dynamic/DynamicTypesafeConfigTest.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,105 @@ | ||
package com.netflix.archaius.typesafe.dynamic; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Scopes; | ||
import com.netflix.archaius.api.Config; | ||
import com.netflix.archaius.api.exceptions.ConfigException; | ||
import com.netflix.archaius.api.inject.RemoteLayer; | ||
import com.netflix.archaius.guice.ArchaiusModule; | ||
import com.typesafe.config.ConfigFactory; | ||
import org.junit.Test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class DynamicTypesafeConfigTest { | ||
|
||
@Test | ||
public void test() throws ConfigException { | ||
final TypesafeClientConfig config = new DefaultTypesafeClientConfig.Builder() | ||
.withConfigFilePath(getResourceUrl("dynamic/reference.conf")) | ||
.withRefreshIntervalMs(100) | ||
.build(); | ||
|
||
Injector injector = Guice.createInjector( | ||
new ArchaiusModule() { | ||
@Override | ||
protected void configureArchaius() { | ||
bind(TypesafeClientConfig.class).toInstance(config); | ||
bind(Config.class) | ||
.annotatedWith(RemoteLayer.class) | ||
.toProvider(TypesafeConfigProvider.class) | ||
.in(Scopes.SINGLETON); | ||
} | ||
}); | ||
|
||
SampleAppConfig appConfig = injector.getInstance(SampleAppConfig.class); | ||
|
||
assertEquals("app name", appConfig.name); | ||
assertEquals(true, appConfig.flag); | ||
assertEquals(111, appConfig.number.intValue()); | ||
} | ||
|
||
@Test | ||
public void testDynamicConfigChange() throws ConfigException, InterruptedException { | ||
String[] configPath = new String[] {"dynamic/reference.conf"}; | ||
|
||
final TypesafeClientConfig config = new TypesafeClientConfig() { | ||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getRefreshRateMs() { | ||
return 200; | ||
} | ||
|
||
@Override | ||
public String getTypesafeConfigPath() { | ||
return configPath[0]; | ||
} | ||
|
||
@Override | ||
public Supplier<com.typesafe.config.Config> getTypesafeConfigSupplier() { | ||
return () -> { | ||
String typesafeConfigPath = getTypesafeConfigPath(); | ||
System.out.println("Load from: " + typesafeConfigPath); | ||
return ConfigFactory.load(typesafeConfigPath); | ||
}; | ||
} | ||
}; | ||
|
||
Injector injector = Guice.createInjector( | ||
new ArchaiusModule() { | ||
@Override | ||
protected void configureArchaius() { | ||
bind(TypesafeClientConfig.class).toInstance(config); | ||
bind(Config.class) | ||
.annotatedWith(RemoteLayer.class) | ||
.toProvider(TypesafeConfigProvider.class) | ||
.in(Scopes.SINGLETON); | ||
} | ||
}); | ||
|
||
SampleAppConfig appConfig = injector.getInstance(SampleAppConfig.class); | ||
assertEquals("app name", appConfig.name); | ||
assertEquals(true, appConfig.flag); | ||
assertEquals(111, appConfig.number.intValue()); | ||
|
||
// Simulate configuration update at runtime. | ||
configPath[0] = "dynamic/updated.conf"; | ||
Thread.sleep(400); | ||
|
||
SampleAppConfig updatedAppConfig = injector.getInstance(SampleAppConfig.class); | ||
assertEquals("updated app name", updatedAppConfig.name); | ||
assertEquals(false, updatedAppConfig.flag); | ||
assertEquals(222, updatedAppConfig.number.intValue()); | ||
} | ||
|
||
private String getResourceUrl(String resourceName) { | ||
return "file://" + getClass().getClassLoader().getResource(resourceName).getPath(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
archaius2-typesafe/src/test/java/com/netflix/archaius/typesafe/dynamic/SampleAppConfig.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,11 @@ | ||
package com.netflix.archaius.typesafe.dynamic; | ||
|
||
import com.netflix.archaius.api.annotations.Configuration; | ||
|
||
@Configuration(prefix = "app", allowFields = true) | ||
public class SampleAppConfig { | ||
|
||
public String name; | ||
public Boolean flag; | ||
public Integer number; | ||
} |
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,5 @@ | ||
app { | ||
name: "app name" | ||
flag: true | ||
number: 111 | ||
} |
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,5 @@ | ||
app { | ||
name: "updated app name" | ||
flag: false | ||
number: 222 | ||
} |