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