Merge branch 'master' into BAEL-1711-move-code-snippets

This commit is contained in:
Rajat Garg
2018-05-01 15:00:52 +05:30
committed by GitHub
588 changed files with 12440 additions and 11609 deletions
@@ -0,0 +1,86 @@
package com.baeldung.jctools;
import org.jctools.queues.SpscArrayQueue;
import org.jctools.queues.SpscChunkedArrayQueue;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.IntConsumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
public class JCToolsUnitTest {
@Test
public void givenMultipleProducers_whenSpscQueueUsed_thenNoWarningOccurs() throws InterruptedException {
SpscArrayQueue<Integer> queue = new SpscArrayQueue<Integer>(2);
Thread producer1 = new Thread(() -> {
queue.offer(1);
});
producer1.start();
producer1.join();
Thread producer2 = new Thread(() -> {
queue.offer(2);
});
producer2.start();
producer2.join();
Set<Integer> fromQueue = new HashSet<>();
Thread consumer = new Thread(() -> queue.drain(fromQueue::add));
consumer.start();
consumer.join();
assertThat(fromQueue).containsOnly(1, 2);
}
@Test
public void whenQueueIsFull_thenNoMoreElementsCanBeAdded() throws InterruptedException {
SpscChunkedArrayQueue<Integer> queue = new SpscChunkedArrayQueue<>(8, 16);
assertThat(queue.capacity()).isEqualTo(16);
CountDownLatch startConsuming = new CountDownLatch(1);
CountDownLatch awakeProducer = new CountDownLatch(1);
AtomicReference<Throwable> error = new AtomicReference<>();
Thread producer = new Thread(() -> {
IntStream.range(0, queue.capacity()).forEach(i -> {
assertThat(queue.offer(i)).isTrue();
});
assertThat(queue.offer(queue.capacity())).isFalse();
startConsuming.countDown();
try {
awakeProducer.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
assertThat(queue.offer(queue.capacity())).isTrue();
});
producer.setUncaughtExceptionHandler((t, e) -> {
error.set(e);
startConsuming.countDown();
});
producer.start();
startConsuming.await();
if (error.get() != null) {
fail("Producer's assertion failed", error.get());
}
Set<Integer> fromQueue = new HashSet<>();
queue.drain(fromQueue::add);
awakeProducer.countDown();
producer.join();
queue.drain(fromQueue::add);
assertThat(fromQueue).containsAll(IntStream.range(0, 17).boxed().collect(Collectors.toSet()));
}
}
@@ -52,16 +52,12 @@ public class EmbeddedChannelUnitTest {
"/calculate?a=10&b=5");
wrongHttpRequest.headers().add("Operator", "Add");
Throwable thrownException = catchThrowable(() -> {
// send invalid HTTP request to server and expect and error
channel.pipeline().fireChannelRead(wrongHttpRequest);
channel.checkException();
Assertions.failBecauseExceptionWasNotThrown(UnsupportedOperationException.class);
});
assertThat(thrownException)
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("HTTP method not supported");
assertThatThrownBy(() -> {
// send invalid HTTP request to server and expect and error
channel.pipeline().fireChannelRead(wrongHttpRequest);
channel.checkException();
}).isInstanceOf(UnsupportedOperationException.class)
.hasMessage("HTTP method not supported");
FullHttpResponse errorHttpResponse = channel.readOutbound();
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
@@ -77,15 +73,11 @@ public class EmbeddedChannelUnitTest {
"/calculate?a=10&b=5");
wrongHttpRequest.headers().add("Operator", "Invalid_operation");
Throwable thrownException = catchThrowable(() -> {
// send invalid HTTP request to server and expect and error
channel.writeInbound(wrongHttpRequest);
Assertions.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
});
// the HttpMessageHandler does not handle the exception and throws it down the
// pipeline
assertThat(thrownException).isInstanceOf(IllegalArgumentException.class).hasMessage("Operation not defined");
// the HttpMessageHandler does not handle the exception and throws it down the pipeline
assertThatThrownBy(() -> {
channel.writeInbound(wrongHttpRequest);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Operation not defined");
// the outbound message is a HTTP response with the status code 500
FullHttpResponse errorHttpResponse = channel.readOutbound();