use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonRemoteServiceTest method testInvocationWithServiceName.
@Test
public void testInvocationWithServiceName() {
RedissonClient server = createInstance();
RedissonClient client = createInstance();
server.getRemoteService("MyServiceNamespace").register(RemoteInterface.class, new RemoteImpl());
RemoteInterface serviceRemoteInterface = client.getRemoteService("MyServiceNamespace").get(RemoteInterface.class);
RemoteInterface otherServiceRemoteInterface = client.getRemoteService("MyOtherServiceNamespace").get(RemoteInterface.class);
RemoteInterface defaultServiceRemoteInterface = client.getRemoteService().get(RemoteInterface.class);
assertThat(serviceRemoteInterface.resultMethod(21L)).isEqualTo(42L);
try {
otherServiceRemoteInterface.resultMethod(21L);
Assert.fail("Invoking a service in an unregistered custom services namespace should throw");
} catch (Exception e) {
assertThat(e).isInstanceOf(RemoteServiceAckTimeoutException.class);
}
try {
defaultServiceRemoteInterface.resultMethod(21L);
Assert.fail("Invoking a service in the unregistered default services namespace should throw");
} catch (Exception e) {
assertThat(e).isInstanceOf(RemoteServiceAckTimeoutException.class);
}
client.shutdown();
server.shutdown();
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonRemoteServiceTest method testExecutorsAmountConcurrency.
@Test
public void testExecutorsAmountConcurrency() throws InterruptedException {
// Redisson server and client
final RedissonClient server = createInstance();
final RedissonClient client = createInstance();
final int serverAmount = 1;
final int clientAmount = 10;
// to store the current call concurrency count
final AtomicInteger concurrency = new AtomicInteger(0);
// a flag to indicate the the allowed concurrency was exceeded
final AtomicBoolean concurrencyIsExceeded = new AtomicBoolean(false);
// the server: register a service with an overrided timeoutMethod method that:
// - incr the concurrency
// - check if concurrency is greater than what was allowed, and if yes set the concurrencyOfOneIsExceeded flag
// - wait 2s
// - decr the concurrency
server.getRemoteService().register(RemoteInterface.class, new RemoteImpl() {
@Override
public void timeoutMethod() throws InterruptedException {
try {
if (concurrency.incrementAndGet() > serverAmount) {
concurrencyIsExceeded.compareAndSet(false, true);
}
super.timeoutMethod();
} finally {
concurrency.decrementAndGet();
}
}
}, serverAmount);
// a latch to force the client threads to execute simultaneously
// (as far as practicable, this is hard to predict)
final CountDownLatch readyLatch = new CountDownLatch(1);
// the client: starts a couple of threads that will:
// - await for the ready latch
// - then call timeoutMethod
java.util.concurrent.Future[] clientFutures = new java.util.concurrent.Future[clientAmount];
ExecutorService executor = Executors.newFixedThreadPool(clientAmount);
for (int i = 0; i < clientAmount; i++) {
clientFutures[i] = executor.submit(new Runnable() {
@Override
public void run() {
try {
RemoteInterface ri = client.getRemoteService().get(RemoteInterface.class, clientAmount * 3, TimeUnit.SECONDS, clientAmount * 3, TimeUnit.SECONDS);
readyLatch.await();
ri.timeoutMethod();
} catch (InterruptedException e) {
// ignore
}
}
});
}
// open the latch to wake the threads
readyLatch.countDown();
// await for the client threads to terminate
for (java.util.concurrent.Future clientFuture : clientFutures) {
try {
clientFuture.get();
} catch (ExecutionException e) {
// ignore
}
}
executor.shutdown();
executor.awaitTermination(clientAmount * 3, TimeUnit.SECONDS);
// shutdown the server and the client
server.shutdown();
client.shutdown();
// do the concurrencyIsExceeded flag was set ?
// if yes, that would indicate that the server exceeded its expected concurrency
assertThat(concurrencyIsExceeded.get()).isEqualTo(false);
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonRemoteServiceTest method testNoAckWithResultInvocations.
@Test
public void testNoAckWithResultInvocations() throws InterruptedException {
RedissonClient server = createInstance();
RedissonClient client = createInstance();
try {
server.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
// no ack but an execution timeout of 1 second
RemoteInvocationOptions options = RemoteInvocationOptions.defaults().noAck().expectResultWithin(1, TimeUnit.SECONDS);
RemoteInterface service = client.getRemoteService().get(RemoteInterface.class, options);
service.voidMethod("noAck", 100L);
assertThat(service.resultMethod(21L)).isEqualTo(42);
try {
service.errorMethod();
Assert.fail();
} catch (IOException e) {
assertThat(e.getMessage()).isEqualTo("Checking error throw");
}
try {
service.errorMethodWithCause();
Assert.fail();
} catch (Exception e) {
assertThat(e.getCause()).isInstanceOf(ArithmeticException.class);
assertThat(e.getCause().getMessage()).isEqualTo("/ by zero");
}
try {
service.timeoutMethod();
Assert.fail("noAck option should still wait for the server to return a response and throw if the execution timeout is exceeded");
} catch (Exception e) {
assertThat(e).isInstanceOf(RemoteServiceTimeoutException.class);
}
} finally {
client.shutdown();
server.shutdown();
}
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonRedLockTest method testLockFailed.
@Test
public void testLockFailed() throws IOException, InterruptedException {
RedisProcess redis1 = redisTestMultilockInstance();
RedisProcess redis2 = redisTestMultilockInstance();
RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
RLock lock1 = client1.getLock("lock1");
RLock lock2 = client1.getLock("lock2");
RLock lock3 = client2.getLock("lock3");
Thread t1 = new Thread() {
public void run() {
lock2.lock();
lock3.lock();
}
;
};
t1.start();
t1.join();
Thread t = new Thread() {
public void run() {
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
lock.lock();
}
;
};
t.start();
t.join(1000);
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
Assert.assertFalse(lock.tryLock());
client1.shutdown();
client2.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
assertThat(redis2.stop()).isEqualTo(0);
}
use of org.redisson.api.RedissonClient in project redisson by redisson.
the class RedissonRedLockTest method testConnectionFailed.
@Test
public void testConnectionFailed() throws IOException, InterruptedException {
RedisProcess redis1 = redisTestMultilockInstance();
RedisProcess redis2 = redisTestMultilockInstance();
RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
RLock lock1 = client1.getLock("lock1");
RLock lock2 = client1.getLock("lock2");
assertThat(redis2.stop()).isEqualTo(0);
RLock lock3 = client2.getLock("lock3");
Thread t = new Thread() {
public void run() {
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
lock.lock();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
lock.unlock();
}
;
};
t.start();
t.join(1000);
RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
lock.lock();
lock.unlock();
client1.shutdown();
client2.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
}
Aggregations