Search in sources :

Example 1 with CommandsListener

use of com.ctrip.xpipe.redis.core.store.CommandsListener in project x-pipe by ctripcorp.

the class DefaultCommandDelayTest method testFlush.

@Test
public void testFlush() {
    long offset = 0;
    for (int i = 0; i < testCount; i++) {
        offset++;
        CommandsListener commandsListener = mock(CommandsListener.class);
        commandDelay.beginWrite();
        commandDelay.endWrite(offset);
        commandDelay.beginSend(commandsListener, offset);
        sleep((delayLogLimitMicro / 1000) + 2);
        commandDelay.flushSucceed(commandsListener, offset);
        Assert.assertEquals(i + 1, logCount.get());
        commandDelay.flushSucceed(mock(CommandsListener.class), offset);
        Assert.assertEquals(i + 1, logCount.get());
    }
}
Also used : CommandsListener(com.ctrip.xpipe.redis.core.store.CommandsListener) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest) Test(org.junit.Test)

Example 2 with CommandsListener

use of com.ctrip.xpipe.redis.core.store.CommandsListener 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 3 with CommandsListener

use of com.ctrip.xpipe.redis.core.store.CommandsListener in project x-pipe by ctripcorp.

the class DefaultCommandStoreTest method readCommandStoreTilNoMessage.

private String readCommandStoreTilNoMessage(final long offset, final DefaultCommandStore commandStore, int expectedLength) throws InterruptedException {
    final StringBuilder result = new StringBuilder();
    final Semaphore semaphore = new Semaphore(-expectedLength + 1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                commandStore.addCommandsListener(offset, new CommandsListener() {

                    @Override
                    public ChannelFuture onCommand(ReferenceFileRegion referenceFileRegion) {
                        logger.debug("[onCommand]{}", referenceFileRegion);
                        result.append(readFileChannelInfoMessageAsString(referenceFileRegion));
                        semaphore.release((int) referenceFileRegion.count());
                        return null;
                    }

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

                    @Override
                    public void beforeCommand() {
                    }
                });
            } catch (IOException e) {
                logger.error("[run]", e);
            }
        }
    }).start();
    semaphore.tryAcquire(10, TimeUnit.SECONDS);
    return result.toString();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) CommandsListener(com.ctrip.xpipe.redis.core.store.CommandsListener) ReferenceFileRegion(com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)

Aggregations

CommandsListener (com.ctrip.xpipe.redis.core.store.CommandsListener)3 ReferenceFileRegion (com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)2 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)2 IOException (java.io.IOException)2 Semaphore (java.util.concurrent.Semaphore)2 Test (org.junit.Test)2 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1