Search in sources :

Example 6 with DeterministicScheduler

use of org.jmock.lib.concurrent.DeterministicScheduler in project atlasdb by palantir.

the class CassandraMetricMeasurementsLoaderTest method setup.

@Before
public void setup() {
    healthMetricSupplier = mock(Supplier.class);
    cassandraMetricClient = mock(MetricsService.class);
    scheduledExecutorService = new DeterministicScheduler();
    cassandraMetricMeasurementsLoader = new CassandraMetricMeasurementsLoader(healthMetricSupplier, cassandraMetricClient, scheduledExecutorService);
    when(healthMetricSupplier.get()).thenReturn(ImmutableList.of(getCassandraMetric(METRIC_TYPE_1, METRIC_NAME_1, METRIC_ATTRIBUTE_1), getCassandraMetric(METRIC_TYPE_2, METRIC_NAME_2, METRIC_ATTRIBUTE_2)));
    when(cassandraMetricClient.getMetric(METRIC_TYPE_1, METRIC_NAME_1, METRIC_ATTRIBUTE_1, ImmutableMap.of())).thenReturn(METRIC_VALUE_1);
    when(cassandraMetricClient.getMetric(METRIC_TYPE_2, METRIC_NAME_2, METRIC_ATTRIBUTE_2, ImmutableMap.of())).thenReturn(METRIC_VALUE_2);
}
Also used : MetricsService(com.palantir.atlasdb.qos.metrics.MetricsService) Supplier(java.util.function.Supplier) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Before(org.junit.Before)

Example 7 with DeterministicScheduler

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

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 DistributedLogConfiguration());
    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 : DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Test(org.junit.Test)

Example 8 with DeterministicScheduler

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

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 DistributedLogConfiguration());
    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) ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeterministicScheduler(org.jmock.lib.concurrent.DeterministicScheduler) Test(org.junit.Test)

Example 9 with DeterministicScheduler

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

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 DistributedLogConfiguration());
    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 com.twitter.distributedlog.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 : AtomicReference(java.util.concurrent.atomic.AtomicReference) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) 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