Search in sources :

Example 1 with StatisticsTypeFactory

use of org.apache.geode.StatisticsTypeFactory in project geode by apache.

the class AtomicStatsJUnitTest method testConcurrentGets.

/**
   * Test for bug 41340. Do two gets at the same time of a dirty stat, and make sure we get the
   * correct value for the stat.
   * 
   * @throws Throwable
   */
@Test
public void testConcurrentGets() throws Throwable {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    // props.setProperty("statistic-sample-rate", "60000");
    props.setProperty(STATISTIC_SAMPLING_ENABLED, "false");
    DistributedSystem ds = DistributedSystem.connect(props);
    String statName = "TestStats";
    String statDescription = "Tests stats";
    final String statDesc = "blah blah blah";
    StatisticsTypeFactory f = StatisticsTypeFactoryImpl.singleton();
    StatisticsType type = f.createType(statName, statDescription, new StatisticDescriptor[] { f.createIntGauge("stat", statDesc, "bottles of beer on the wall") });
    final int statId = type.nameToId("stat");
    try {
        final AtomicReference<Statistics> statsRef = new AtomicReference<Statistics>();
        final CyclicBarrier beforeIncrement = new CyclicBarrier(3);
        final CyclicBarrier afterIncrement = new CyclicBarrier(3);
        Thread thread1 = new Thread("thread1") {

            public void run() {
                try {
                    while (true) {
                        beforeIncrement.await();
                        statsRef.get().incInt(statId, 1);
                        afterIncrement.await();
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        Thread thread3 = new Thread("thread1") {

            public void run() {
                try {
                    while (true) {
                        beforeIncrement.await();
                        afterIncrement.await();
                        statsRef.get().getInt(statId);
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        thread1.start();
        thread3.start();
        for (int i = 0; i < 5000; i++) {
            Statistics stats = ds.createAtomicStatistics(type, "stats");
            statsRef.set(stats);
            beforeIncrement.await();
            afterIncrement.await();
            assertEquals("On loop " + i, 1, stats.getInt(statId));
            stats.close();
        }
    } finally {
        ds.disconnect();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) StatisticsType(org.apache.geode.StatisticsType) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) Statistics(org.apache.geode.Statistics) CyclicBarrier(java.util.concurrent.CyclicBarrier) StatisticsTypeFactory(org.apache.geode.StatisticsTypeFactory) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

Properties (java.util.Properties)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Statistics (org.apache.geode.Statistics)1 StatisticsType (org.apache.geode.StatisticsType)1 StatisticsTypeFactory (org.apache.geode.StatisticsTypeFactory)1 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)1 DistributedSystem (org.apache.geode.distributed.DistributedSystem)1 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)1 Test (org.junit.Test)1