Search in sources :

Example 26 with Supplier

use of java.util.function.Supplier in project jdk8u_jdk by JetBrains.

the class ConcurrentAssociateTest method testOnce.

private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
    ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
    CountDownLatch s = new CountDownLatch(1);
    Supplier<Runnable> sr = () -> () -> {
        try {
            s.await();
        } catch (InterruptedException e) {
        }
        for (int i = 0; i < N; i++) {
            Object o = new X();
            associator.accept(m, o);
            if (!m.containsKey(o)) {
                throw new AssociationFailure(desc + " 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 associating
    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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BiConsumer(java.util.function.BiConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with Supplier

use of java.util.function.Supplier in project riposte by Nike-Inc.

the class AsyncNettyHelperTest method supplierWithTracingAndMdc_pair_works_as_expected.

@Test
public void supplierWithTracingAndMdc_pair_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = generateTracingAndMdcInfo();
    // when
    Supplier result = AsyncNettyHelper.supplierWithTracingAndMdc(supplierMock, setupInfo);
    // 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 28 with Supplier

use of java.util.function.Supplier in project riposte by Nike-Inc.

the class AsyncNettyHelperTest method supplierWithTracingAndMdc_separate_args_works_as_expected.

@Test
public void supplierWithTracingAndMdc_separate_args_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = generateTracingAndMdcInfo();
    // when
    Supplier result = AsyncNettyHelper.supplierWithTracingAndMdc(supplierMock, setupInfo.getLeft(), setupInfo.getRight());
    // 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 29 with Supplier

use of java.util.function.Supplier in project riposte by Nike-Inc.

the class ParserTest method test_match9_works.

@Test
public void test_match9_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")).thenParse(bool).thenParse(number).thenParse(string("C")).thenParse(bool).map(match((first, second, third, fourth, fifth, sixth, seventh, eighth, nineth) -> {
        return first.toString() + "+" + second.toString() + "+" + third.toString() + "+" + fourth.toString() + "+" + fifth.toString() + "+" + sixth.toString() + "+" + seventh.toString() + "+" + eighth.toString() + "+" + nineth.toString();
    }));
    Optional<String> oResult = parser.tryParse("1Atrue2Bfalse3Ctrue");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo("1+A+true+2+B+false+3+C+true");
}
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 30 with Supplier

use of java.util.function.Supplier in project riposte by Nike-Inc.

the class ParserTest method test_match8_works.

@Test
public void test_match8_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")).thenParse(bool).thenParse(number).thenParse(string("C")).map(match((first, second, third, fourth, fifth, sixth, seventh, eighth) -> {
        return first.toString() + "+" + second.toString() + "+" + third.toString() + "+" + fourth.toString() + "+" + fifth.toString() + "+" + sixth.toString() + "+" + seventh.toString() + "+" + eighth.toString();
    }));
    Optional<String> oResult = parser.tryParse("1Atrue2Bfalse3C");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo("1+A+true+2+B+false+3+C");
}
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)

Aggregations

Supplier (java.util.function.Supplier)81 List (java.util.List)29 Test (org.junit.Test)28 Map (java.util.Map)16 Collectors (java.util.stream.Collectors)15 Function (java.util.function.Function)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 Arrays (java.util.Arrays)11 Consumer (java.util.function.Consumer)11 Assert.assertEquals (org.junit.Assert.assertEquals)10 Optional (java.util.Optional)9 Mockito.mock (org.mockito.Mockito.mock)6 Collections (java.util.Collections)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Assert.assertFalse (org.junit.Assert.assertFalse)5 Matchers.any (org.mockito.Matchers.any)5 Mockito.when (org.mockito.Mockito.when)5 Maps (com.google.common.collect.Maps)4