Search in sources :

Example 11 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class DefaultRedisSlaveTest method testFuture.

@Test
public void testFuture() {
    SettableFuture<Boolean> objectSettableFuture = SettableFuture.create();
    AtomicInteger listenerCount = new AtomicInteger(0);
    AtomicInteger notifyCount = new AtomicInteger();
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            while (!Thread.interrupted()) {
                listenerCount.incrementAndGet();
                objectSettableFuture.addListener(new Runnable() {

                    @Override
                    public void run() {
                        notifyCount.incrementAndGet();
                    }
                }, MoreExecutors.directExecutor());
            }
            logger.info("exit thread");
        }
    });
    sleep(10);
    objectSettableFuture.set(true);
    executors.shutdownNow();
    sleep(10);
    logger.info("{}, {}", listenerCount, notifyCount);
    Assert.assertEquals(listenerCount.get(), notifyCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 12 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class SimpleSendMessage method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    CountDownLatch latch = new CountDownLatch(concurrent);
    for (int i = 0; i < concurrent; i++) {
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            public void doRun() throws InterruptedException {
                try {
                    try (Jedis jedis = new Jedis(master.getHostString(), master.getPort())) {
                        while (true) {
                            long index = increase();
                            if (index < 0) {
                                logger.info("[doStart][index < 0, break]{}", index);
                                break;
                            }
                            long keyIndex = getKeyIndex(index);
                            if (expire > 0) {
                                jedis.setex(String.valueOf(keyIndex), expire, message);
                            } else {
                                jedis.set(String.valueOf(keyIndex), message);
                            }
                            if (sleepMilli > 0) {
                                TimeUnit.MILLISECONDS.sleep(sleepMilli);
                            }
                        }
                    }
                    logger.info("[doStart][finish]");
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    latch.await();
    System.exit(0);
}
Also used : Jedis(redis.clients.jedis.Jedis) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class DefaultRdbStoreEofMarkTest method testRdbStoreEofMarkSend.

@Test
public void testRdbStoreEofMarkSend() throws IOException, InterruptedException {
    String data = randomString(1024);
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            rdbStore.writeRdb(Unpooled.wrappedBuffer(data.getBytes()));
            rdbStore.writeRdb(Unpooled.wrappedBuffer(eofMark.getBytes()));
            sleep(10);
            rdbStore.truncateEndRdb(eofMark.length());
            sleep(10);
        }
    });
    String rdbFileData = readRdbFileTilEnd(rdbStore);
    Assert.assertEquals(data, rdbFileData);
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) IOException(java.io.IOException) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest) Test(org.junit.Test)

Example 14 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class DefaultCommandStoreTest method testGetAsSoonAsMessageWritten.

@Test
public void testGetAsSoonAsMessageWritten() throws IOException, InterruptedException {
    final StringBuilder sb = new StringBuilder();
    final Semaphore semaphore = new Semaphore(0);
    executors.execute(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            commandStore.addCommandsListener(0, new CommandsListener() {

                @Override
                public ChannelFuture onCommand(ReferenceFileRegion referenceFileRegion) {
                    sb.append(readFileChannelInfoMessageAsString(referenceFileRegion));
                    semaphore.release();
                    return null;
                }

                @Override
                public boolean isOpen() {
                    return true;
                }

                @Override
                public void beforeCommand() {
                }
            });
        }
    });
    StringBuilder expected = new StringBuilder();
    for (int i = 0; i < (1 << 10); i++) {
        byte random = (byte) randomInt('a', 'z');
        semaphore.drainPermits();
        expected.append((char) random);
        commandStore.appendCommands(Unpooled.wrappedBuffer(new byte[] { random }));
        Assert.assertTrue(semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
        logger.debug("{}", sb);
        Assert.assertEquals(expected.toString(), sb.toString());
    }
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CommandsListener(com.ctrip.xpipe.redis.core.store.CommandsListener) ReferenceFileRegion(com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 15 with AbstractExceptionLogTask

use of com.ctrip.xpipe.concurrent.AbstractExceptionLogTask in project x-pipe by ctripcorp.

the class DefaultCommandStoreTest method testInterruptClose.

@Test
public void testInterruptClose() throws InterruptedException {
    Thread thread = new Thread(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            while (true) {
                commandStore.totalLength();
                sleep(10);
            }
        }
    });
    thread.start();
    thread.interrupt();
    thread.join(100);
    // should not fail
    commandStore.totalLength();
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Aggregations

AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)46 IOException (java.io.IOException)19 Test (org.junit.Test)18 CountDownLatch (java.util.concurrent.CountDownLatch)9 PostConstruct (javax.annotation.PostConstruct)9 ExecutionException (java.util.concurrent.ExecutionException)8 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)6 AbstractTest (com.ctrip.xpipe.AbstractTest)5 MigrationShard (com.ctrip.xpipe.redis.console.migration.model.MigrationShard)5 LinkedList (java.util.LinkedList)5 KeeperMeta (com.ctrip.xpipe.redis.core.entity.KeeperMeta)3 FileChannel (java.nio.channels.FileChannel)3 Semaphore (java.util.concurrent.Semaphore)3 TimeoutException (java.util.concurrent.TimeoutException)3 ReferenceFileRegion (com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)2 RedisMeta (com.ctrip.xpipe.redis.core.entity.RedisMeta)2 RedisKeeperRuntimeException (com.ctrip.xpipe.redis.keeper.exception.RedisKeeperRuntimeException)2 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)2 Server (com.ctrip.xpipe.simpleserver.Server)2 List (java.util.List)2