Search in sources :

Example 1 with DeterministicScheduler

use of org.jmock.lib.concurrent.DeterministicScheduler in project bookkeeper by apache.

the class TestConfigurationSubscription method testInitialConfigLoad.

@Test(timeout = 60000)
public void testInitialConfigLoad() throws Exception {
    PropertiesWriter writer = new PropertiesWriter();
    writer.setProperty("prop1", "1");
    writer.setProperty("prop2", "abc");
    writer.setProperty("prop3", "123.0");
    writer.setProperty("prop4", "11132");
    writer.setProperty("prop5", "true");
    writer.save();
    ScheduledExecutorService mockScheduler = new DeterministicScheduler();
    FileConfigurationBuilder builder = new PropertiesConfigurationBuilder(writer.getFile().toURI().toURL());
    ConcurrentConstConfiguration conf = new ConcurrentConstConfiguration(new CompositeConfiguration());
    List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(builder);
    ConfigurationSubscription confSub = new ConfigurationSubscription(conf, fileConfigBuilders, mockScheduler, 100, TimeUnit.MILLISECONDS);
    assertEquals(1, conf.getInt("prop1"));
    assertEquals("abc", conf.getString("prop2"));
    assertEquals(123.0, conf.getFloat("prop3"), 0);
    assertEquals(11132, conf.getInt("prop4"));
    assertEquals(true, conf.getBoolean("prop5"));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Test(org.junit.Test)

Example 2 with DeterministicScheduler

use of org.jmock.lib.concurrent.DeterministicScheduler in project distributedlog by twitter.

the class TestConfigurationSubscription method testInitialConfigLoad.

@Test(timeout = 60000)
public void testInitialConfigLoad() throws Exception {
    PropertiesWriter writer = new PropertiesWriter();
    writer.setProperty("prop1", "1");
    writer.setProperty("prop2", "abc");
    writer.setProperty("prop3", "123.0");
    writer.setProperty("prop4", "11132");
    writer.setProperty("prop5", "true");
    writer.save();
    ScheduledExecutorService mockScheduler = new DeterministicScheduler();
    FileConfigurationBuilder builder = new PropertiesConfigurationBuilder(writer.getFile().toURI().toURL());
    ConcurrentConstConfiguration conf = new ConcurrentConstConfiguration(new DistributedLogConfiguration());
    List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(builder);
    ConfigurationSubscription confSub = new ConfigurationSubscription(conf, fileConfigBuilders, mockScheduler, 100, TimeUnit.MILLISECONDS);
    assertEquals(1, conf.getInt("prop1"));
    assertEquals("abc", conf.getString("prop2"));
    assertEquals(123.0, conf.getFloat("prop3"), 0);
    assertEquals(11132, conf.getInt("prop4"));
    assertEquals(true, conf.getBoolean("prop5"));
}
Also used : DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Test(org.junit.Test)

Example 3 with DeterministicScheduler

use of org.jmock.lib.concurrent.DeterministicScheduler in project bookkeeper by apache.

the class TestConfigurationSubscription method testReloadConfiguration.

@Test(timeout = 60000)
public void testReloadConfiguration() throws Exception {
    PropertiesWriter writer = new PropertiesWriter();
    FileConfigurationBuilder builder = new PropertiesConfigurationBuilder(writer.getFile().toURI().toURL());
    ConcurrentConstConfiguration conf = new ConcurrentConstConfiguration(new CompositeConfiguration());
    DeterministicScheduler executorService = new DeterministicScheduler();
    List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(builder);
    ConfigurationSubscription confSub = new ConfigurationSubscription(conf, fileConfigBuilders, executorService, 100, TimeUnit.MILLISECONDS);
    final AtomicReference<ConcurrentBaseConfiguration> confHolder = new AtomicReference<>();
    confSub.registerListener(new org.apache.distributedlog.common.config.ConfigurationListener() {

        @Override
        public void onReload(ConcurrentBaseConfiguration conf) {
            confHolder.set(conf);
        }
    });
    assertEquals(null, conf.getProperty("prop1"));
    // add
    writer.setProperty("prop1", "1");
    writer.save();
    // ensure the file change reloading event can be triggered
    ensureConfigReloaded();
    // reload the config
    confSub.reload();
    assertNotNull(confHolder.get());
    assertTrue(conf == confHolder.get());
    assertEquals("1", conf.getProperty("prop1"));
}
Also used : CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Test(org.junit.Test)

Example 4 with DeterministicScheduler

use of org.jmock.lib.concurrent.DeterministicScheduler in project bookkeeper by apache.

the class TestConfigurationSubscription method testAddReloadBasicsConfig.

@Test(timeout = 60000)
public void testAddReloadBasicsConfig() throws Exception {
    PropertiesWriter writer = new PropertiesWriter();
    DeterministicScheduler mockScheduler = new DeterministicScheduler();
    FileConfigurationBuilder builder = new PropertiesConfigurationBuilder(writer.getFile().toURI().toURL());
    ConcurrentConstConfiguration conf = new ConcurrentConstConfiguration(new CompositeConfiguration());
    List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(builder);
    ConfigurationSubscription confSub = new ConfigurationSubscription(conf, fileConfigBuilders, mockScheduler, 100, TimeUnit.MILLISECONDS);
    assertEquals(null, conf.getProperty("prop1"));
    // add
    writer.setProperty("prop1", "1");
    writer.save();
    // ensure the file change reloading event can be triggered
    ensureConfigReloaded();
    mockScheduler.tick(100, TimeUnit.MILLISECONDS);
    assertEquals("1", conf.getProperty("prop1"));
}
Also used : CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Test(org.junit.Test)

Example 5 with DeterministicScheduler

use of org.jmock.lib.concurrent.DeterministicScheduler in project bookkeeper by apache.

the class TestConfigurationSubscription method testExceptionInConfigLoad.

@Test(timeout = 60000)
public void testExceptionInConfigLoad() throws Exception {
    PropertiesWriter writer = new PropertiesWriter();
    writer.setProperty("prop1", "1");
    writer.save();
    DeterministicScheduler mockScheduler = new DeterministicScheduler();
    FileConfigurationBuilder builder = new PropertiesConfigurationBuilder(writer.getFile().toURI().toURL());
    ConcurrentConstConfiguration conf = new ConcurrentConstConfiguration(new CompositeConfiguration());
    List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(builder);
    ConfigurationSubscription confSub = new ConfigurationSubscription(conf, fileConfigBuilders, mockScheduler, 100, TimeUnit.MILLISECONDS);
    final AtomicInteger count = new AtomicInteger(1);
    conf.addConfigurationListener(new ConfigurationListener() {

        @Override
        public void configurationChanged(ConfigurationEvent event) {
            LOG.info("config changed {}", event);
            // Throw after so we actually see the update anyway.
            if (!event.isBeforeUpdate()) {
                count.getAndIncrement();
                throw new RuntimeException("config listener threw and exception");
            }
        }
    });
    int i = 0;
    int initial = 0;
    while (count.get() == initial) {
        writer.setProperty("prop1", Integer.toString(i++));
        writer.save();
        mockScheduler.tick(100, TimeUnit.MILLISECONDS);
    }
    initial = count.get();
    while (count.get() == initial) {
        writer.setProperty("prop1", Integer.toString(i++));
        writer.save();
        mockScheduler.tick(100, TimeUnit.MILLISECONDS);
    }
}
Also used : ConfigurationEvent(org.apache.commons.configuration.event.ConfigurationEvent) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Test(org.junit.Test)

Aggregations

DeterministicScheduler (org.jmock.lib.concurrent.DeterministicScheduler)9 Test (org.junit.Test)8 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)4 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ConfigurationEvent (org.apache.commons.configuration.event.ConfigurationEvent)2 ConfigurationListener (org.apache.commons.configuration.event.ConfigurationListener)2 MetricsService (com.palantir.atlasdb.qos.metrics.MetricsService)1 Supplier (java.util.function.Supplier)1 Before (org.junit.Before)1