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());
}
}
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());
}
}
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();
}
Aggregations