use of java.util.function.Supplier in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method supplierWithTracingAndMdc_ctx_works_as_expected.
@Test
public void supplierWithTracingAndMdc_ctx_works_as_expected() {
// given
Pair<Deque<Span>, Map<String, String>> setupInfo = setupStateWithTracingAndMdcInfo();
// when
Supplier result = AsyncNettyHelper.supplierWithTracingAndMdc(supplierMock, ctxMock);
// then
verifySupplierWithTracingAndMdcSupport(result, supplierMock, setupInfo.getLeft(), setupInfo.getRight());
}
use of java.util.function.Supplier in project riposte by Nike-Inc.
the class ParserTest method test_match4_works.
@Test
public void test_match4_works() throws ParserFailure {
final Pattern numberPattern = Pattern.compile("([0-9])");
final Pattern booleanPattern = Pattern.compile("(true|false)");
final Supplier<Parser<Integer>> number = () -> regex(numberPattern).map(m -> new Integer(m.group(1)));
final Supplier<Parser<Boolean>> bool = () -> regex(booleanPattern).map(m -> new Boolean(m.group(1)));
Parser<String> parser = number.get().thenParse(string("A")).thenParse(bool).thenParse(number).map(match((first, second, third, fourth) -> {
return first.toString() + "+" + second.toString() + "+" + third.toString() + "+" + fourth.toString();
}));
Optional<String> oResult = parser.tryParse("1Atrue2");
assertThat(oResult.isPresent()).isTrue();
assertThat(oResult.get()).isNotNull();
assertThat(oResult.get()).isEqualTo("1+A+true+2");
}
use of java.util.function.Supplier in project riposte by Nike-Inc.
the class ParserTest method test_match5_works.
@Test
public void test_match5_works() throws ParserFailure {
final Pattern numberPattern = Pattern.compile("([0-9])");
final Pattern booleanPattern = Pattern.compile("(true|false)");
final Supplier<Parser<Integer>> number = () -> regex(numberPattern).map(m -> new Integer(m.group(1)));
final Supplier<Parser<Boolean>> bool = () -> regex(booleanPattern).map(m -> new Boolean(m.group(1)));
Parser<String> parser = number.get().thenParse(string("A")).thenParse(bool).thenParse(number).thenParse(string("B")).map(match((first, second, third, fourth, fifth) -> {
return first.toString() + "+" + second.toString() + "+" + third.toString() + "+" + fourth.toString() + "+" + fifth.toString();
}));
Optional<String> oResult = parser.tryParse("1Atrue2B");
assertThat(oResult.isPresent()).isTrue();
assertThat(oResult.get()).isNotNull();
assertThat(oResult.get()).isEqualTo("1+A+true+2+B");
}
use of java.util.function.Supplier in project jdk8u_jdk by JetBrains.
the class ConcurrentContainsKeyTest method testOnce.
private static void testOnce(Object[] content, ConcurrentHashMap<Object, Object> m) {
CountDownLatch s = new CountDownLatch(1);
Supplier<Runnable> sr = () -> () -> {
try {
s.await();
} catch (InterruptedException e) {
}
for (int i = 0; i < R * N; i++) {
Object o = content[i % content.length];
if (!m.containsKey(o)) {
throw new AssociationFailure("CHM.containsKey failed: entry does not exist");
}
}
};
int ps = Runtime.getRuntime().availableProcessors();
Stream<CompletableFuture> runners = IntStream.range(0, ps).mapToObj(i -> sr.get()).map(CompletableFuture::runAsync);
CompletableFuture all = CompletableFuture.allOf(runners.toArray(CompletableFuture[]::new));
// Trigger the runners to start checking key membership
s.countDown();
try {
all.join();
} catch (CompletionException e) {
Throwable t = e.getCause();
if (t instanceof AssociationFailure) {
throw (AssociationFailure) t;
} else {
throw e;
}
}
}
use of java.util.function.Supplier in project jdk8u_jdk by JetBrains.
the class SequentialOpTest method testLazy.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class, groups = { "serialization-hostile" })
public void testLazy(String name, TestData.OfRef<Integer> data) {
Function<Integer, Integer> id = LambdaTestHelpers.identity();
AtomicInteger counter = new AtomicInteger();
Supplier<Stream<Integer>>[] suppliers = new Supplier[] { () -> data.stream(), () -> data.parallelStream() };
UnaryOperator<Stream<Integer>>[] configs = new UnaryOperator[] { (UnaryOperator<Stream<Integer>>) s -> s.peek(e -> {
counter.incrementAndGet();
}), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
counter.incrementAndGet();
}).sequential().map(id), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
counter.incrementAndGet();
}).parallel().map(id), (UnaryOperator<Stream<Integer>>) s -> s.sequential().map(id).peek(e -> {
counter.incrementAndGet();
}).map(id), (UnaryOperator<Stream<Integer>>) s -> s.parallel().map(id).peek(e -> {
counter.incrementAndGet();
}).map(id) };
for (int i = 0; i < suppliers.length; i++) {
setContext("supplierIndex", i);
Supplier<Stream<Integer>> supp = suppliers[i];
for (int j = 0; j < configs.length; j++) {
setContext("configIndex", j);
UnaryOperator<Stream<Integer>> config = configs[j];
counter.set(0);
Stream<Integer> stream = config.apply(supp.get());
assertEquals(0, counter.get());
Iterator<Integer> iterator = stream.iterator();
assertEquals(0, counter.get());
if (iterator.hasNext())
iterator.next();
assertTrue(data.size() == 0 || counter.get() > 0);
counter.set(0);
stream = config.apply(supp.get());
Spliterator<Integer> spliterator = stream.spliterator();
assertEquals(0, counter.get());
spliterator.forEachRemaining(e -> {
});
assertTrue(data.size() == 0 || counter.get() > 0);
}
}
}
Aggregations