Search in sources :

Example 86 with Supplier

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());
}
Also used : Supplier(java.util.function.Supplier) Deque(java.util.Deque) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 87 with Supplier

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");
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Example 88 with Supplier

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");
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Example 89 with Supplier

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;
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Supplier(java.util.function.Supplier) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 90 with Supplier

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);
        }
    }
}
Also used : Iterator(java.util.Iterator) TestData(java.util.stream.TestData) StreamTestDataProvider(java.util.stream.StreamTestDataProvider) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) UnaryOperator(java.util.function.UnaryOperator) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Stream(java.util.stream.Stream) LambdaTestHelpers(java.util.stream.LambdaTestHelpers) OpTestCase(java.util.stream.OpTestCase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertTrue(org.testng.Assert.assertTrue) Comparator(java.util.Comparator) Spliterator(java.util.Spliterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Supplier(java.util.function.Supplier) Stream(java.util.stream.Stream) UnaryOperator(java.util.function.UnaryOperator) Test(org.testng.annotations.Test)

Aggregations

Supplier (java.util.function.Supplier)104 Test (org.junit.Test)43 List (java.util.List)41 ArrayList (java.util.ArrayList)28 Map (java.util.Map)24 Collectors (java.util.stream.Collectors)23 HashMap (java.util.HashMap)21 Function (java.util.function.Function)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 IOException (java.io.IOException)17 Arrays (java.util.Arrays)16 TimeUnit (java.util.concurrent.TimeUnit)14 Consumer (java.util.function.Consumer)14 Assert (org.junit.Assert)14 Collections (java.util.Collections)13 CompletableFuture (java.util.concurrent.CompletableFuture)13 Rule (org.junit.Rule)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Cleanup (lombok.Cleanup)12 Timeout (org.junit.rules.Timeout)11