@NotThreadSafe public class SharedObjects extends org.junit.rules.ExternalResource
SharedReferences. Usage:
@Rule
public final SharedObjects sharedObjects = SharedObjects.create();
@Test
public void test() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
SharedReference<Queue<Long>> listRef = sharedObjects.add(new ConcurrentLinkedQueue<>());
int n = 10000;
env.setParallelism(100);
env.fromSequence(0, n).map(i -> listRef.get().add(i));
env.execute();
assertEquals(n + 1, listRef.get().size());
assertEquals(
LongStream.rangeClosed(0, n).boxed().collect(Collectors.toList()),
listRef.get().stream().sorted().collect(Collectors.toList()));
}
The main idea is that shared objects are bound to the scope of a test case instead of a class. That allows us to:
Note that since the shared objects are accessed through multiple threads, they need to be thread-safe or accessed in a thread-safe manner.
public static SharedObjects create()
Rule.public <T> SharedReference<T> add(T object)
SharedObjects. Although not necessary, it is recommended to
only access the object through the returned SharedReference.protected void before()
throws Throwable
before 在类中 org.junit.rules.ExternalResourceThrowableprotected void after()
after 在类中 org.junit.rules.ExternalResourceCopyright © 2014–2023 The Apache Software Foundation. All rights reserved.