Search in sources :

Example 1 with TestCommand

use of com.ctrip.xpipe.command.TestCommand in project x-pipe by ctripcorp.

the class KeyedOneThreadTaskExecutorTest method testHang.

@Test
public void testHang() throws TimeoutException, IOException {
    int threadCount = 10;
    int taskCount = threadCount;
    ExecutorService executorService = null;
    try {
        executorService = Executors.newFixedThreadPool(threadCount, XpipeThreadFactory.create("test-hang"));
        keyed = new KeyedOneThreadTaskExecutor<>(executorService);
        AtomicInteger completeCount = new AtomicInteger();
        for (int i = 0; i < taskCount; i++) {
            int finalI = i;
            ParallelCommandChain parallelCommandChain = new ParallelCommandChain(executorService);
            parallelCommandChain.add(new TestCommand("success:" + i, sleepInterval));
            parallelCommandChain.future().addListener(new CommandFutureListener<Object>() {

                @Override
                public void operationComplete(CommandFuture<Object> commandFuture) throws Exception {
                    logger.info("[operationComplete]{}", finalI);
                    completeCount.incrementAndGet();
                }
            });
            keyed.execute(String.valueOf(i), parallelCommandChain);
        }
        waitConditionUntilTimeOut(() -> completeCount.get() == taskCount);
    } finally {
        executorService.shutdownNow();
    }
}
Also used : ParallelCommandChain(com.ctrip.xpipe.command.ParallelCommandChain) TestCommand(com.ctrip.xpipe.command.TestCommand) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 2 with TestCommand

use of com.ctrip.xpipe.command.TestCommand in project x-pipe by ctripcorp.

the class KeyedOneThreadTaskExecutorTest method testDifferentKey.

@Test
public void testDifferentKey() {
    TestCommand command1 = new TestCommand("success", sleepInterval);
    TestCommand command2 = new TestCommand("success", sleepInterval);
    keyed.execute("key1", command1);
    keyed.execute("key2", command2);
    sleep(sleepInterval / 2);
    Assert.assertTrue(command1.isBeginExecute());
    Assert.assertTrue(command2.isBeginExecute());
}
Also used : TestCommand(com.ctrip.xpipe.command.TestCommand) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 3 with TestCommand

use of com.ctrip.xpipe.command.TestCommand in project x-pipe by ctripcorp.

the class KeyedOneThreadTaskExecutorTest method testSameKey.

@Test
public void testSameKey() {
    TestCommand command1 = new TestCommand("success", sleepInterval);
    TestCommand command2 = new TestCommand("success", sleepInterval);
    keyed.execute("key1", command1);
    keyed.execute("key1", command2);
    sleep(sleepInterval / 2);
    Assert.assertTrue(command1.isBeginExecute());
    Assert.assertFalse(command2.isBeginExecute());
}
Also used : TestCommand(com.ctrip.xpipe.command.TestCommand) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

AbstractTest (com.ctrip.xpipe.AbstractTest)3 TestCommand (com.ctrip.xpipe.command.TestCommand)3 Test (org.junit.Test)3 ParallelCommandChain (com.ctrip.xpipe.command.ParallelCommandChain)1 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1