org.jmock.lib.concurrent.DeterministicScheduler Java Examples

The following examples show how to use org.jmock.lib.concurrent.DeterministicScheduler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastForwardHttpReporterTest.java    From semantic-metrics with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    registry = new SemanticMetricRegistry();
    fixedClock = new Clock.Fixed(0L);

    commonTags = of("foo", "bar");
    executorService = new DeterministicScheduler();

    reporter = FastForwardHttpReporter
        .forRegistry(registry, httpClient)
        .schedule(REPORTING_PERIOD, TimeUnit.MILLISECONDS)
        .prefix(MetricId.build("prefix").tagged(commonTags))
        .clock(fixedClock)
        .executorService(executorService)
        .build();
}
 
Example #2
Source File: TestConfigurationSubscription.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@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"));

}
 
Example #3
Source File: TestConfigurationSubscription.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #4
Source File: TestConfigurationSubscription.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@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"));

}
 
Example #5
Source File: TestConfigurationSubscription.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #6
Source File: FastForwardReporterTest.java    From semantic-metrics with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    registry = new SemanticMetricRegistry();
    fastForward = mock(FastForward.class);
    executorService = new DeterministicScheduler();
    reporter = FastForwardReporter
        .forRegistry(registry)
        .schedule(TimeUnit.MILLISECONDS, REPORTING_PERIOD)
        .fastForward(fastForward)
        .executorService(executorService)
        .build();

    registry.counter(MetricId.build("hi"));
}
 
Example #7
Source File: TestConfigurationSubscription.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example #8
Source File: TestConfigurationSubscription.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@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);
    }
}