diff --git a/src/test/java/io/netty/buffer/api/BufferTest.java b/src/test/java/io/netty/buffer/api/BufferTest.java index b10e262..e218f0a 100644 --- a/src/test/java/io/netty/buffer/api/BufferTest.java +++ b/src/test/java/io/netty/buffer/api/BufferTest.java @@ -59,15 +59,43 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; public class BufferTest { - private static volatile Fixture[] fixtures; private static ExecutorService executor; + private static final Memoize ALL_COMBINATIONS = new Memoize<>( + () -> fixtureCombinations().toArray(Fixture[]::new)); + private static final Memoize NON_SLICED = new Memoize<>( + () -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> !f.isSlice()).toArray(Fixture[]::new)); + private static final Memoize NON_COMPOSITE = new Memoize<>( + () -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> !f.isComposite()).toArray(Fixture[]::new)); + private static final Memoize HEAP_ALLOCS = new Memoize<>( + () -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isHeap()).toArray(Fixture[]::new)); + private static final Memoize DIRECT_ALLOCS = new Memoize<>( + () -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isDirect()).toArray(Fixture[]::new)); + private static final Memoize POOLED_ALLOCS = new Memoize<>( + () -> Arrays.stream(ALL_COMBINATIONS.get()).filter(f -> f.isPooled()).toArray(Fixture[]::new)); + static Fixture[] allocators() { - Fixture[] fxs = fixtures; - if (fxs != null) { - return fxs; - } - return fixtures = fixtureCombinations().toArray(Fixture[]::new); + return ALL_COMBINATIONS.get(); + } + + static Fixture[] nonSliceAllocators() { + return NON_SLICED.get(); + } + + static Fixture[] nonCompositeAllocators() { + return NON_COMPOSITE.get(); + } + + static Fixture[] heapAllocators() { + return HEAP_ALLOCS.get(); + } + + static Fixture[] directAllocators() { + return DIRECT_ALLOCS.get(); + } + + static Fixture[] pooledAllocators() { + return POOLED_ALLOCS.get(); } static List initialAllocators() { @@ -78,35 +106,7 @@ public class BufferTest { new Fixture("pooledDirect", BufferAllocator::pooledDirect, POOLED, DIRECT, CLEANER)); } - static Stream nonSliceAllocators() { - return fixtureCombinations().filter(f -> !f.isSlice()); - } - - static Stream nonCompositeAllocators() { - return fixtureCombinations().filter(f -> !f.isComposite()); - } - - static Stream heapAllocators() { - return fixtureCombinations().filter(Fixture::isHeap); - } - - static Stream directAllocators() { - return fixtureCombinations().filter(Fixture::isDirect); - } - - static Stream directPooledAllocators() { - return fixtureCombinations().filter(f -> f.isDirect() && f.isCleaner() && f.isPooled()); - } - - static Stream pooledAllocators() { - return fixtureCombinations().filter(Fixture::isPooled); - } - private static Stream fixtureCombinations() { - Fixture[] fxs = fixtures; - if (fxs != null) { - return Arrays.stream(fxs); - } List initFixtures = initialAllocators(); Builder builder = Stream.builder(); initFixtures.forEach(builder); diff --git a/src/test/java/io/netty/buffer/api/Memoize.java b/src/test/java/io/netty/buffer/api/Memoize.java new file mode 100644 index 0000000..bc5db8d --- /dev/null +++ b/src/test/java/io/netty/buffer/api/Memoize.java @@ -0,0 +1,36 @@ +/* + * Copyright 2021 The Netty Project + * + * The Netty Project 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: + * + * https://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 io.netty.buffer.api; + +import java.util.function.Supplier; + +final class Memoize implements Supplier { + private final Supplier supplier; + private volatile T memo; + + Memoize(Supplier supplier) { + this.supplier = supplier; + } + + @Override + public T get() { + T val = memo; + if (val == null) { + memo = val = supplier.get(); + } + return val; + } +} diff --git a/src/test/resources/junit-platform.properties b/src/test/resources/junit-platform.properties new file mode 100644 index 0000000..080b321 --- /dev/null +++ b/src/test/resources/junit-platform.properties @@ -0,0 +1,18 @@ +# Copyright 2021 The Netty Project +# +# The Netty Project 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: +# +# https://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. +junit.jupiter.execution.parallel.enabled = true +junit.jupiter.execution.parallel.mode.default = concurrent +junit.jupiter.testinstance.lifecycle.default = per_class +junit.jupiter.execution.parallel.config.strategy = fixed +junit.jupiter.execution.parallel.config.fixed.parallelism = 16