Class SharedObjectsExtension

java.lang.Object
org.apache.flink.testutils.junit.SharedObjectsExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

@NotThreadSafe public class SharedObjectsExtension extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
This rule allows objects to be used both in the main test case as well as in UDFs by using serializable SharedReferences. Usage:

    @RegisterExtension
     public final SharedObjectsExtension sharedObjects = SharedObjectsExtension.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:

  • Avoid all kinds of static fields in test classes that only exist since all fields in UDFs need to be serializable.
  • Hopefully make it easier to reason about the test setup
  • Facilitate to share more test building blocks across test classes.
  • Fully allow tests to be rerun/run in parallel without worrying about static fields

Note that since the shared objects are accessed through multiple threads, they need to be thread-safe or accessed in a thread-safe manner.

  • Method Details

    • create

      public static SharedObjectsExtension create()
      Creates a new instance. Usually that should be done inside a JUnit test class as an instance-field annotated with Rule.
    • add

      public <T> SharedReference<T> add(T object)
      Adds a new object to this SharedObjects. Although not necessary, it is recommended to only access the object through the returned SharedReference.
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object