apache/beam · error · UnsupportedOperationException

Not expected to access Restriction from a regular DoFn in Do

Error message

Not expected to access Restriction from a regular DoFn in DoFnTester

What it means

DoFnTester is a testing harness for regular (non-splittable) DoFns. Its internal ProcessContext exposes restriction() only for splittable-DoFn support; calling it on a regular DoFn's tester throws this UnsupportedOperationException because there is no restriction to return.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/DoFnTester.java:344

            public TimeDomain timeDomain(DoFn<InputT, OutputT> doFn) {
              throw new UnsupportedOperationException(
                  "Not expected to access TimeDomain from @ProcessElement");
            }

            @Override
            public OutputReceiver<OutputT> outputReceiver(DoFn<InputT, OutputT> doFn) {
              return DoFnOutputReceivers.windowedReceiver(processContext, builderSupplier, null);
            }

            @Override
            public MultiOutputReceiver taggedOutputReceiver(DoFn<InputT, OutputT> doFn) {
              return DoFnOutputReceivers.windowedMultiReceiver(
                  processContext, builderSupplier, null);
            }

            @Override
            public Object restriction() {
              throw new UnsupportedOperationException(
                  "Not expected to access Restriction from a regular DoFn in DoFnTester");
            }

            @Override
            public RestrictionTracker<?, ?> restrictionTracker() {
              throw new UnsupportedOperationException(
                  "Not expected to access RestrictionTracker from a regular DoFn in DoFnTester");
            }
          });
    } catch (UserCodeException e) {
      unwrapUserCodeException(e);
    }
  }

  /**
   * @deprecated Use {@link TestPipeline} with the {@code DirectRunner}.
   */
  @Deprecated

View on GitHub (pinned to 12126d8942)

Solutions

  1. Remove the restriction() call; a regular DoFn has no restriction to test
  2. Use TestStream or a dedicated splittable-DoFn test harness instead of DoFnTester for SDF behavior
  3. If the fn should be splittable, implement ProcessElement returning a ProcessContinuation and a GetInitialRestriction method, then test via a pipeline runner rather than DoFnTester

Example fix

// before
Object r = tester.processContext.restriction();
// after
// regular DoFn: no restriction access; test outputs only
tester.processElement(in);
Defensive patterns

Strategy: try-catch

Validate before calling

// Only call restriction() if the fn is splittable
if (myDoFn.getClass().getAnnotation(DoFn.BoundedPerElement.class) != null || usesRestriction(myDoFn)) { /* use SDF harness */ }

Type guard

boolean isSplittableDoFn(DoFn<?, ?> fn) {
  return java.util.Arrays.stream(fn.getClass().getDeclaredMethods())
      .anyMatch(m -> m.isAnnotationPresent(DoFn.GetInitialRestriction.class));
}

Try / catch

try { Object r = tester.processContext.restriction(); }
catch (UnsupportedOperationException e) { // regular DoFn: skip restriction assertions
}

Prevention

When it happens

Trigger: Calling DoFnTester's processContext.restriction() (directly or via test code that assumes an SDF) while the wrapped fn is a plain DoFn that does not implement splittable processing.

Common situations: Reusing a generic test helper written for splittable DoFns against a regular DoFn; copy-pasting an SDF unit test for a non-SDF transform.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/e5356402d0346e4b. Report an issue: GitHub.