Search in sources :

Example 1 with RepeatingTestThread

use of org.apache.hadoop.test.MultithreadedTestUtil.RepeatingTestThread in project hadoop by apache.

the class TestMultithreadedTestUtil method testRepeatingThread.

@Test
public void testRepeatingThread() throws Exception {
    final AtomicInteger counter = new AtomicInteger();
    TestContext ctx = new TestContext();
    ctx.addThread(new RepeatingTestThread(ctx) {

        @Override
        public void doAnAction() throws Exception {
            counter.incrementAndGet();
        }
    });
    ctx.startThreads();
    long st = Time.now();
    ctx.waitFor(3000);
    ctx.stop();
    long et = Time.now();
    long elapsed = et - st;
    // Test should have waited just about 3 seconds
    assertTrue("Test took " + (et - st) + "ms", Math.abs(elapsed - 3000) < 500);
    // Counter should have been incremented lots of times in 3 full seconds
    assertTrue("Counter value = " + counter.get(), counter.get() > 1000);
}
Also used : RepeatingTestThread(org.apache.hadoop.test.MultithreadedTestUtil.RepeatingTestThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestContext(org.apache.hadoop.test.MultithreadedTestUtil.TestContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with RepeatingTestThread

use of org.apache.hadoop.test.MultithreadedTestUtil.RepeatingTestThread in project hadoop by apache.

the class TestHAStateTransitions method testTransitionSynchronization.

/**
   * Regression test for HDFS-2693: when doing state transitions, we need to
   * lock the FSNamesystem so that we don't end up doing any writes while it's
   * "in between" states.
   * This test case starts up several client threads which do mutation operations
   * while flipping a NN back and forth from active to standby.
   */
@Test(timeout = 120000)
public void testTransitionSynchronization() throws Exception {
    Configuration conf = new Configuration();
    final MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).nnTopology(MiniDFSNNTopology.simpleHATopology()).numDataNodes(0).build();
    try {
        cluster.waitActive();
        ReentrantReadWriteLock spyLock = NameNodeAdapter.spyOnFsLock(cluster.getNameNode(0).getNamesystem());
        Mockito.doAnswer(new GenericTestUtils.SleepAnswer(50)).when(spyLock).writeLock();
        final FileSystem fs = HATestUtil.configureFailoverFs(cluster, conf);
        TestContext ctx = new TestContext();
        for (int i = 0; i < 50; i++) {
            final int finalI = i;
            ctx.addThread(new RepeatingTestThread(ctx) {

                @Override
                public void doAnAction() throws Exception {
                    Path p = new Path("/test-" + finalI);
                    fs.mkdirs(p);
                    fs.delete(p, true);
                }
            });
        }
        ctx.addThread(new RepeatingTestThread(ctx) {

            @Override
            public void doAnAction() throws Exception {
                cluster.transitionToStandby(0);
                Thread.sleep(50);
                cluster.transitionToActive(0);
            }
        });
        ctx.startThreads();
        ctx.waitFor(20000);
        ctx.stop();
    } finally {
        cluster.shutdown();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) TestContext(org.apache.hadoop.test.MultithreadedTestUtil.TestContext) GenericTestUtils(org.apache.hadoop.test.GenericTestUtils) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IOException(java.io.IOException) RepeatingTestThread(org.apache.hadoop.test.MultithreadedTestUtil.RepeatingTestThread) FileSystem(org.apache.hadoop.fs.FileSystem) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 RepeatingTestThread (org.apache.hadoop.test.MultithreadedTestUtil.RepeatingTestThread)2 TestContext (org.apache.hadoop.test.MultithreadedTestUtil.TestContext)2 Test (org.junit.Test)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Configuration (org.apache.hadoop.conf.Configuration)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 Path (org.apache.hadoop.fs.Path)1 GenericTestUtils (org.apache.hadoop.test.GenericTestUtils)1