mbaedke commented on code in PR #2457: URL: https://github.com/apache/jackrabbit-oak/pull/2457#discussion_r2287443160
########## oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/function/SuppliersTest.java: ########## @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.jackrabbit.oak.commons.function; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.apache.jackrabbit.oak.commons.function.Suppliers.memoize; +import static org.junit.Assert.assertNull; + +public class SuppliersTest { + + @Test + public void computeOnce() { + AtomicInteger count = new AtomicInteger(0); + + Supplier<Integer> mem = Suppliers.memoize(count::incrementAndGet); + + assertEquals(0, count.get()); + int c = mem.get(); + assertEquals(1, c); + c = mem.get(); + assertEquals(1, c); + } + + @Test + public void nullSupplier() { + AtomicInteger count = new AtomicInteger(0); + + Supplier<Object> mem = Suppliers.memoize(new Supplier<Object>() { + @Override + public Integer get() { + count.incrementAndGet(); + return null; + } + }); + + assertEquals(0, count.get()); + Object o = mem.get(); + assertNull(o); + assertEquals(1, count.get()); + o = mem.get(); + assertEquals(1, count.get()); + + } + + @Test + public void concurrentSupplierAccess() throws InterruptedException { + List<Thread> threads = new ArrayList<>(); + int threadCount = 1000; + for (int k = 0; k < threadCount; k++) { + threads.add(new Thread(() -> { + synchronized (concurrencyTestMonitor) { + // the empty synchronized block is deliberate. + } + AtomicInteger result = memoizeTestSupplier.get(); + if (result == null || result.get() != 42) { + concurrencyTestFailed = true; + } + })); + } + Thread waitForAll = new Thread(() -> { + for (int k = 0; k < threadCount; k++) { + try { + threads.get(k).join(); + } catch (InterruptedException ignored) {} + } + }); + synchronized (concurrencyTestMonitor) { + for (int k = 0; k < threadCount; k++) { + threads.get(k).start(); + } + } + waitForAll.start(); + waitForAll.join(); Review Comment: @jsedding: An empty synchronized block is commonly used to let multiple threads start from the same point in time. The test does not work without that. There are certainly other solutions. I am extremely old school when it comes to elementary concurrent programming (in particular in testing). I find it extremely readable, but apparently not everyone shares this opinion. It's a test. It works. I don't want to spend too much time on it. But I'll look into it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: oak-dev-unsubscr...@jackrabbit.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org