org.apache.commons.configuration2.sync.ReadWriteSynchronizer Java Examples

The following examples show how to use org.apache.commons.configuration2.sync.ReadWriteSynchronizer. 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: TestINIConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests concurrent access to the global section.
 */
@Test
public void testGetSectionGloabalMultiThreaded()
        throws ConfigurationException, InterruptedException
{
    final INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
    config.setSynchronizer(new ReadWriteSynchronizer());
    final int threadCount = 10;
    final GlobalSectionTestThread[] threads = new GlobalSectionTestThread[threadCount];
    for (int i = 0; i < threadCount; i++)
    {
        threads[i] = new GlobalSectionTestThread(config);
        threads[i].start();
    }
    for (int i = 0; i < threadCount; i++)
    {
        threads[i].join();
        assertFalse("Exception occurred", threads[i].error);
    }
}
 
Example #2
Source File: TestDynamicCombinedConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a configuration can be updated.
 */
@Test
public void testUpdateConfiguration() throws ConfigurationException
{
    System.getProperties().remove("Id");
    final CombinedConfigurationBuilder builder =
            new CombinedConfigurationBuilder();
    builder.configure(parameters.fileBased().setFile(MULTI_TENENT_FILE)
            .setSynchronizer(new ReadWriteSynchronizer()));
    final CombinedConfiguration config = builder.getConfiguration();
    config.getConfiguration(1).setProperty("rowsPerPage", "25");
    assertEquals("Value not changed", "25", config.getString("rowsPerPage"));
}
 
Example #3
Source File: TestDynamicCombinedConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentGetAndReload() throws Exception
{
    System.getProperties().remove("Id");
    final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
    builder.configure(parameters.fileBased().setFile(MULTI_TENENT_FILE)
            .setSynchronizer(new ReadWriteSynchronizer()));
    final CombinedConfiguration config = builder.getConfiguration();

    assertEquals("Wrong value", "50", config.getString("rowsPerPage"));
    final Thread testThreads[] = new Thread[THREAD_COUNT];
    final int failures[] = new int[THREAD_COUNT];

    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i] = new ReloadThread(builder, failures, i, LOOP_COUNT, false, null, "50");
        testThreads[i].start();
    }

    int totalFailures = 0;
    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i].join();
        totalFailures += failures[i];
    }
    assertEquals(totalFailures + " failures Occurred", 0, totalFailures);
}
 
Example #4
Source File: TestDynamicCombinedConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentGetAndReload2() throws Exception
{
    System.getProperties().remove("Id");
    final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
    builder.configure(parameters.fileBased().setFile(MULTI_TENENT_FILE)
            .setSynchronizer(new ReadWriteSynchronizer()));
    final CombinedConfiguration config = builder.getConfiguration();

    assertEquals(config.getString("rowsPerPage"), "50");

    final Thread testThreads[] = new Thread[THREAD_COUNT];
    final int failures[] = new int[THREAD_COUNT];
    System.setProperty("Id", "2002");
    assertEquals("Wrong value", "25", config.getString("rowsPerPage"));
    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i] = new ReloadThread(builder, failures, i, LOOP_COUNT, false, null, "25");
        testThreads[i].start();
    }

    int totalFailures = 0;
    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i].join();
        totalFailures += failures[i];
    }
    System.getProperties().remove("Id");
    assertEquals(totalFailures + " failures Occurred", 0, totalFailures);
}
 
Example #5
Source File: TestDynamicCombinedConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrentGetAndReloadMultipleClients() throws Exception
{
    System.getProperties().remove("Id");
    final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
    builder.configure(parameters.fileBased().setFile(MULTI_TENENT_FILE)
            .setSynchronizer(new ReadWriteSynchronizer()));
    final CombinedConfiguration config = builder.getConfiguration();

    assertEquals(config.getString("rowsPerPage"), "50");

    final Thread testThreads[] = new Thread[THREAD_COUNT];
    final int failures[] = new int[THREAD_COUNT];
    final String[] ids = new String[] {null, "2002", "3001", "3002", "3003"};
    final String[] expected = new String[] {"50", "25", "15", "25", "50"};
    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i] = new ReloadThread(builder, failures, i, LOOP_COUNT, true, ids[i], expected[i]);
        testThreads[i].start();
    }

    int totalFailures = 0;
    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i].join();
        totalFailures += failures[i];
    }
    System.getProperties().remove("Id");
    if (totalFailures != 0)
    {
        System.out.println("Failures:");
        for (int i = 0; i < testThreads.length; ++i)
        {
            System.out.println("Thread " + i + " " + failures[i]);
        }
    }
    assertEquals(totalFailures + " failures Occurred", 0, totalFailures);
}
 
Example #6
Source File: TestBasicBuilderParameters.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether properties can be inherited from another parameters map.
 */
@Test
public void testInheritFrom()
{
    final BeanHelper beanHelper = new BeanHelper();
    final ConfigurationDecoder decoder =
            EasyMock.createMock(ConfigurationDecoder.class);
    final ConversionHandler conversionHandler = new DefaultConversionHandler();
    final ListDelimiterHandler listDelimiterHandler =
            new DefaultListDelimiterHandler('#');
    final ConfigurationLogger logger = new ConfigurationLogger("test");
    final Synchronizer synchronizer = new ReadWriteSynchronizer();
    params.setBeanHelper(beanHelper).setConfigurationDecoder(decoder)
            .setConversionHandler(conversionHandler)
            .setListDelimiterHandler(listDelimiterHandler).setLogger(logger)
            .setSynchronizer(synchronizer).setThrowExceptionOnMissing(true);
    final BasicBuilderParameters p2 = new BasicBuilderParameters();

    p2.inheritFrom(params.getParameters());
    final Map<String, Object> parameters = p2.getParameters();
    assertEquals("Bean helper not set", beanHelper,
            parameters.get("config-BeanHelper"));
    assertEquals("Decoder not set", decoder,
            parameters.get("configurationDecoder"));
    assertEquals("Conversion handler not set", conversionHandler,
            parameters.get("conversionHandler"));
    assertEquals("Delimiter handler not set", listDelimiterHandler,
            parameters.get("listDelimiterHandler"));
    assertEquals("Logger not set", logger, parameters.get("logger"));
    assertEquals("Synchronizer not set", synchronizer,
            parameters.get("synchronizer"));
    assertEquals("Exception flag not set", Boolean.TRUE,
            parameters.get("throwExceptionOnMissing"));
}
 
Example #7
Source File: TestCombinedConfiguration.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Tests concurrent read and write access on a combined configuration. There
 * are multiple reader threads and a single writer thread. It is checked
 * that no inconsistencies occur.
 */
@Test
public void testConcurrentAccess() throws ConfigurationException,
        InterruptedException
{
    // populate the test combined configuration
    setUpSourceTest();
    final XMLConfiguration xmlConf = new XMLConfiguration();
    new FileHandler(xmlConf).load(ConfigurationAssert
            .getTestFile("test.xml"));
    config.addConfiguration(xmlConf);
    final PropertiesConfiguration propConf = new PropertiesConfiguration();
    new FileHandler(propConf).load(ConfigurationAssert
            .getTestFile("test.properties"));
    for (int i = 0; i < 8; i++)
    {
        config.addConfiguration(new BaseHierarchicalConfiguration());
    }
    config.getConfiguration(0).addProperty(KEY_CONCURRENT, TEST_NAME);

    // Set a single synchronizer for all involved configurations
    final Synchronizer sync = new ReadWriteSynchronizer();
    config.setSynchronizer(sync);
    for (final Configuration c : config.getConfigurations())
    {
        c.setSynchronizer(sync);
    }

    // setup test threads
    final int numberOfReaders = 3;
    final int readCount = 5000;
    final int writeCount = 3000;
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger errorCount = new AtomicInteger();
    final Collection<Thread> threads = new ArrayList<>(numberOfReaders + 1);
    final Thread writeThread =
            new WriteThread(config, latch, errorCount, writeCount);
    writeThread.start();
    threads.add(writeThread);
    for (int i = 0; i < numberOfReaders; i++)
    {
        final Thread readThread =
                new ReadThread(config, latch, errorCount, readCount);
        readThread.start();
        threads.add(readThread);
    }

    // perform test
    latch.countDown();
    for (final Thread t : threads)
    {
        t.join();
    }
    assertEquals("Got errors", 0, errorCount.get());
}
 
Example #8
Source File: TestReloadingCombinedConfigurationBuilderFileBased.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Tests concurrent access to a reloading builder for combined
 * configurations.
 */
@Test
public void testConcurrentGetAndReload() throws Exception
{
    final int threadCount = 4;
    final int loopCount = 100;
    final ReloadingDetectorFactory detectorFactory =
            (handler, params) -> new RandomReloadingDetector();
    final BaseHierarchicalConfiguration defConf = new BaseHierarchicalConfiguration();
    defConf.addProperty("header.result.nodeCombiner[@config-class]",
            MergeCombiner.class.getName());
    defConf.addProperty("header.result.expressionEngine[@config-class]",
            XPathExpressionEngine.class.getName());
    addReloadSource(defConf, "configA.xml");
    addReloadSource(defConf, "configB.xml");
    final Synchronizer sync = new ReadWriteSynchronizer();
    builder.configure(parameters
            .combined()
            .setDefinitionBuilder(new ConstantConfigurationBuilder(defConf))
            .setSynchronizer(sync)
            .registerChildDefaultsHandler(
                    BasicBuilderProperties.class,
                    new CopyObjectDefaultHandler(
                            new BasicBuilderParameters()
                                    .setSynchronizer(sync)))
            .registerChildDefaultsHandler(
                    FileBasedBuilderProperties.class,
                    new CopyObjectDefaultHandler(
                            new FileBasedBuilderParametersImpl()
                                    .setReloadingDetectorFactory(detectorFactory))));

    assertEquals("Wrong initial value", "100", builder.getConfiguration()
            .getString("/property[@name='config']/@value"));

    final Thread testThreads[] = new Thread[threadCount];
    final int failures[] = new int[threadCount];

    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i] = new ReloadThread(builder, failures, i, loopCount);
        testThreads[i].start();
    }

    int totalFailures = 0;
    for (int i = 0; i < testThreads.length; ++i)
    {
        testThreads[i].join();
        totalFailures += failures[i];
    }
    assertTrue(totalFailures + " failures Occurred", totalFailures == 0);
}