use of org.apache.beam.sdk.coders.Coder.NonDeterministicException in project beam by apache.
the class WindowTest method testNonDeterministicWindowCoder.
@Test
public void testNonDeterministicWindowCoder() throws NonDeterministicException {
FixedWindows mockWindowFn = Mockito.mock(FixedWindows.class);
@SuppressWarnings({ "unchecked", "rawtypes" }) Class<Coder<IntervalWindow>> coderClazz = (Class) Coder.class;
Coder<IntervalWindow> mockCoder = Mockito.mock(coderClazz);
when(mockWindowFn.windowCoder()).thenReturn(mockCoder);
NonDeterministicException toBeThrown = new NonDeterministicException(mockCoder, "Its just not deterministic.");
Mockito.doThrow(toBeThrown).when(mockCoder).verifyDeterministic();
thrown.expect(IllegalArgumentException.class);
thrown.expectCause(Matchers.sameInstance(toBeThrown));
thrown.expectMessage("Window coders must be deterministic");
Window.into(mockWindowFn);
}
use of org.apache.beam.sdk.coders.Coder.NonDeterministicException in project beam by apache.
the class ProtobufUtil method verifyDeterministic.
/**
* Recursively checks whether the specified class uses any Protocol Buffers fields that cannot
* be deterministically encoded.
*
* @throws NonDeterministicException if the object cannot be encoded deterministically.
*/
static void verifyDeterministic(ProtoCoder<?> coder) throws NonDeterministicException {
Class<? extends Message> message = coder.getMessageType();
ExtensionRegistry registry = coder.getExtensionRegistry();
Set<Descriptor> descriptors = getRecursiveDescriptorsForClass(message, registry);
for (Descriptor d : descriptors) {
for (FieldDescriptor fd : d.getFields()) {
// be encoded deterministically.
if (fd.isMapField()) {
String reason = String.format("Protocol Buffers message %s transitively includes Map field %s (from file %s)." + " Maps cannot be deterministically encoded.", message.getName(), fd.getFullName(), fd.getFile().getFullName());
throw new NonDeterministicException(coder, reason);
}
}
}
}
use of org.apache.beam.sdk.coders.Coder.NonDeterministicException in project beam by apache.
the class CoderTest method testNonDeterministicException.
@Test
public void testNonDeterministicException() {
NonDeterministicException rootCause = new NonDeterministicException(VoidCoder.of(), "Root Cause");
NonDeterministicException exception = new NonDeterministicException(StringUtf8Coder.of(), "Problem", rootCause);
assertEquals(rootCause, exception.getCause());
assertThat(exception.getReasons(), contains("Problem"));
assertThat(exception.toString(), containsString("Problem"));
assertThat(exception.toString(), containsString("is not deterministic"));
}
Aggregations