Java Code Examples for org.apache.flink.mock.Whitebox#getInternalState()

The following examples show how to use org.apache.flink.mock.Whitebox#getInternalState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimerServiceTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Test all timeouts registered can be unregistered
 * @throws Exception
  */
@Test
@SuppressWarnings("unchecked")
public void testUnregisterAllTimeouts() throws Exception {
	// Prepare all instances.
	ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
	ScheduledFuture scheduledFuture = mock(ScheduledFuture.class);
	when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)))
		.thenReturn(scheduledFuture);
	TimerService<AllocationID> timerService = new TimerService<>(scheduledExecutorService, 100L);
	TimeoutListener<AllocationID> listener = mock(TimeoutListener.class);

	timerService.start(listener);

	// Invoke register and unregister.
	timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);
	timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);

	timerService.unregisterAllTimeouts();

	// Verify.
	Map<?, ?> timeouts = (Map<?, ?>) Whitebox.getInternalState(timerService, "timeouts");
	assertTrue(timeouts.isEmpty());
	verify(scheduledFuture, times(2)).cancel(true);
}
 
Example 2
Source File: TimerServiceTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Test all timeouts registered can be unregistered
 * @throws Exception
  */
@Test
@SuppressWarnings("unchecked")
public void testUnregisterAllTimeouts() throws Exception {
	// Prepare all instances.
	ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
	ScheduledFuture scheduledFuture = mock(ScheduledFuture.class);
	when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)))
		.thenReturn(scheduledFuture);
	TimerService<AllocationID> timerService = new TimerService<>(scheduledExecutorService, 100L);
	TimeoutListener<AllocationID> listener = mock(TimeoutListener.class);

	timerService.start(listener);

	// Invoke register and unregister.
	timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);
	timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);

	timerService.unregisterAllTimeouts();

	// Verify.
	Map<?, ?> timeouts = (Map<?, ?>) Whitebox.getInternalState(timerService, "timeouts");
	assertTrue(timeouts.isEmpty());
	verify(scheduledFuture, times(2)).cancel(true);
}
 
Example 3
Source File: TimerServiceTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Test all timeouts registered can be unregistered
 * @throws Exception
  */
@Test
@SuppressWarnings("unchecked")
public void testUnregisterAllTimeouts() throws Exception {
	// Prepare all instances.
	ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
	ScheduledFuture scheduledFuture = mock(ScheduledFuture.class);
	when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)))
		.thenReturn(scheduledFuture);
	TimerService<AllocationID> timerService = new TimerService<>(scheduledExecutorService, 100L);
	TimeoutListener<AllocationID> listener = mock(TimeoutListener.class);

	timerService.start(listener);

	// Invoke register and unregister.
	timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);
	timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);

	timerService.unregisterAllTimeouts();

	// Verify.
	Map<?, ?> timeouts = (Map<?, ?>) Whitebox.getInternalState(timerService, "timeouts");
	assertTrue(timeouts.isEmpty());
	verify(scheduledFuture, times(2)).cancel(true);
}
 
Example 4
Source File: AvroOutputFormatTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void serializeAndDeserialize(final AvroOutputFormat.Codec codec, final Schema schema) throws IOException, ClassNotFoundException {
	// given
	final AvroOutputFormat<User> outputFormat = new AvroOutputFormat<>(User.class);
	if (codec != null) {
		outputFormat.setCodec(codec);
	}
	if (schema != null) {
		outputFormat.setSchema(schema);
	}

	final ByteArrayOutputStream bos = new ByteArrayOutputStream();

	// when
	try (final ObjectOutputStream oos = new ObjectOutputStream(bos)) {
		oos.writeObject(outputFormat);
	}
	try (final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
		// then
		Object o = ois.readObject();
		assertTrue(o instanceof AvroOutputFormat);
		@SuppressWarnings("unchecked")
		final AvroOutputFormat<User> restored = (AvroOutputFormat<User>) o;
		final AvroOutputFormat.Codec restoredCodec = (AvroOutputFormat.Codec) Whitebox.getInternalState(restored, "codec");
		final Schema restoredSchema = (Schema) Whitebox.getInternalState(restored, "userDefinedSchema");

		assertTrue(codec != null ? restoredCodec == codec : restoredCodec == null);
		assertTrue(schema != null ? restoredSchema.equals(schema) : restoredSchema == null);
	}
}
 
Example 5
Source File: CEPOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeyedCEPOperatorNFAUpdateTimes() throws Exception {
	CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOpearator(
		true,
		new SimpleNFAFactory());
	OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(operator);

	try {
		harness.open();

		final ValueState nfaOperatorState = (ValueState) Whitebox.<ValueState>getInternalState(operator, "computationStates");
		final ValueState nfaOperatorStateSpy = Mockito.spy(nfaOperatorState);
		Whitebox.setInternalState(operator, "computationStates", nfaOperatorStateSpy);

		Event startEvent = new Event(42, "c", 1.0);
		SubEvent middleEvent = new SubEvent(42, "a", 1.0, 10.0);
		Event endEvent = new Event(42, "b", 1.0);

		harness.processElement(new StreamRecord<>(startEvent, 1L));
		harness.processElement(new StreamRecord<>(new Event(42, "d", 1.0), 4L));
		harness.processElement(new StreamRecord<Event>(middleEvent, 4L));
		harness.processElement(new StreamRecord<>(endEvent, 4L));

		// verify the number of invocations NFA is updated
		Mockito.verify(nfaOperatorStateSpy, Mockito.times(3)).update(Mockito.any());

		// get and verify the output
		Queue<Object> result = harness.getOutput();

		assertEquals(1, result.size());

		verifyPattern(result.poll(), startEvent, middleEvent, endEvent);
	} finally {
		harness.close();
	}
}
 
Example 6
Source File: AvroOutputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void serializeAndDeserialize(final AvroOutputFormat.Codec codec, final Schema schema) throws IOException, ClassNotFoundException {
	// given
	final AvroOutputFormat<User> outputFormat = new AvroOutputFormat<>(User.class);
	if (codec != null) {
		outputFormat.setCodec(codec);
	}
	if (schema != null) {
		outputFormat.setSchema(schema);
	}

	final ByteArrayOutputStream bos = new ByteArrayOutputStream();

	// when
	try (final ObjectOutputStream oos = new ObjectOutputStream(bos)) {
		oos.writeObject(outputFormat);
	}
	try (final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
		// then
		Object o = ois.readObject();
		assertTrue(o instanceof AvroOutputFormat);
		@SuppressWarnings("unchecked")
		final AvroOutputFormat<User> restored = (AvroOutputFormat<User>) o;
		final AvroOutputFormat.Codec restoredCodec = (AvroOutputFormat.Codec) Whitebox.getInternalState(restored, "codec");
		final Schema restoredSchema = (Schema) Whitebox.getInternalState(restored, "userDefinedSchema");

		assertTrue(codec != null ? restoredCodec == codec : restoredCodec == null);
		assertTrue(schema != null ? restoredSchema.equals(schema) : restoredSchema == null);
	}
}
 
Example 7
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeyedCEPOperatorNFAUpdateTimes() throws Exception {
	CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOpearator(
		true,
		new SimpleNFAFactory());
	OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(operator);

	try {
		harness.open();

		final ValueState nfaOperatorState = (ValueState) Whitebox.<ValueState>getInternalState(operator, "computationStates");
		final ValueState nfaOperatorStateSpy = Mockito.spy(nfaOperatorState);
		Whitebox.setInternalState(operator, "computationStates", nfaOperatorStateSpy);

		Event startEvent = new Event(42, "c", 1.0);
		SubEvent middleEvent = new SubEvent(42, "a", 1.0, 10.0);
		Event endEvent = new Event(42, "b", 1.0);

		harness.processElement(new StreamRecord<>(startEvent, 1L));
		harness.processElement(new StreamRecord<>(new Event(42, "d", 1.0), 4L));
		harness.processElement(new StreamRecord<Event>(middleEvent, 4L));
		harness.processElement(new StreamRecord<>(endEvent, 4L));

		// verify the number of invocations NFA is updated
		Mockito.verify(nfaOperatorStateSpy, Mockito.times(3)).update(Mockito.any());

		// get and verify the output
		Queue<Object> result = harness.getOutput();

		assertEquals(1, result.size());

		verifyPattern(result.poll(), startEvent, middleEvent, endEvent);
	} finally {
		harness.close();
	}
}
 
Example 8
Source File: AvroOutputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void serializeAndDeserialize(final AvroOutputFormat.Codec codec, final Schema schema) throws IOException, ClassNotFoundException {
	// given
	final AvroOutputFormat<User> outputFormat = new AvroOutputFormat<>(User.class);
	if (codec != null) {
		outputFormat.setCodec(codec);
	}
	if (schema != null) {
		outputFormat.setSchema(schema);
	}

	final ByteArrayOutputStream bos = new ByteArrayOutputStream();

	// when
	try (final ObjectOutputStream oos = new ObjectOutputStream(bos)) {
		oos.writeObject(outputFormat);
	}
	try (final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
		// then
		Object o = ois.readObject();
		assertTrue(o instanceof AvroOutputFormat);
		@SuppressWarnings("unchecked")
		final AvroOutputFormat<User> restored = (AvroOutputFormat<User>) o;
		final AvroOutputFormat.Codec restoredCodec = (AvroOutputFormat.Codec) Whitebox.getInternalState(restored, "codec");
		final Schema restoredSchema = (Schema) Whitebox.getInternalState(restored, "userDefinedSchema");

		assertTrue(codec != null ? restoredCodec == codec : restoredCodec == null);
		assertTrue(schema != null ? restoredSchema.equals(schema) : restoredSchema == null);
	}
}
 
Example 9
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeyedCEPOperatorNFAUpdateTimes() throws Exception {
	CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOpearator(
		true,
		new SimpleNFAFactory());
	OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(operator);

	try {
		harness.open();

		final ValueState nfaOperatorState = (ValueState) Whitebox.<ValueState>getInternalState(operator, "computationStates");
		final ValueState nfaOperatorStateSpy = Mockito.spy(nfaOperatorState);
		Whitebox.setInternalState(operator, "computationStates", nfaOperatorStateSpy);

		Event startEvent = new Event(42, "c", 1.0);
		SubEvent middleEvent = new SubEvent(42, "a", 1.0, 10.0);
		Event endEvent = new Event(42, "b", 1.0);

		harness.processElement(new StreamRecord<>(startEvent, 1L));
		harness.processElement(new StreamRecord<>(new Event(42, "d", 1.0), 4L));
		harness.processElement(new StreamRecord<Event>(middleEvent, 4L));
		harness.processElement(new StreamRecord<>(endEvent, 4L));

		// verify the number of invocations NFA is updated
		Mockito.verify(nfaOperatorStateSpy, Mockito.times(3)).update(Mockito.any());

		// get and verify the output
		Queue<Object> result = harness.getOutput();

		assertEquals(1, result.size());

		verifyPattern(result.poll(), startEvent, middleEvent, endEvent);
	} finally {
		harness.close();
	}
}
 
Example 10
Source File: CEPOperatorTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyedCEPOperatorNFAUpdateTimesWithRocksDB() throws Exception {

	String rocksDbPath = tempFolder.newFolder().getAbsolutePath();
	RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend(new MemoryStateBackend());
	rocksDBStateBackend.setDbStoragePath(rocksDbPath);

	CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOpearator(
		true,
		new SimpleNFAFactory());
	OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(
		operator);

	try {
		harness.setStateBackend(rocksDBStateBackend);

		harness.open();

		final ValueState nfaOperatorState = (ValueState) Whitebox.<ValueState>getInternalState(operator, "computationStates");
		final ValueState nfaOperatorStateSpy = Mockito.spy(nfaOperatorState);
		Whitebox.setInternalState(operator, "computationStates", nfaOperatorStateSpy);

		Event startEvent = new Event(42, "c", 1.0);
		SubEvent middleEvent = new SubEvent(42, "a", 1.0, 10.0);
		Event endEvent = new Event(42, "b", 1.0);

		harness.processElement(new StreamRecord<>(startEvent, 1L));
		harness.processElement(new StreamRecord<>(new Event(42, "d", 1.0), 4L));
		harness.processElement(new StreamRecord<Event>(middleEvent, 4L));
		harness.processElement(new StreamRecord<>(endEvent, 4L));

		// verify the number of invocations NFA is updated
		Mockito.verify(nfaOperatorStateSpy, Mockito.times(3)).update(Mockito.any());

		// get and verify the output
		Queue<Object> result = harness.getOutput();

		assertEquals(1, result.size());

		verifyPattern(result.poll(), startEvent, middleEvent, endEvent);
	} finally {
		harness.close();
	}
}
 
Example 11
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyedCEPOperatorNFAUpdateTimesWithRocksDB() throws Exception {

	String rocksDbPath = tempFolder.newFolder().getAbsolutePath();
	RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend(new MemoryStateBackend());
	rocksDBStateBackend.setDbStoragePath(rocksDbPath);

	CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOpearator(
		true,
		new SimpleNFAFactory());
	OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(
		operator);

	try {
		harness.setStateBackend(rocksDBStateBackend);

		harness.open();

		final ValueState nfaOperatorState = (ValueState) Whitebox.<ValueState>getInternalState(operator, "computationStates");
		final ValueState nfaOperatorStateSpy = Mockito.spy(nfaOperatorState);
		Whitebox.setInternalState(operator, "computationStates", nfaOperatorStateSpy);

		Event startEvent = new Event(42, "c", 1.0);
		SubEvent middleEvent = new SubEvent(42, "a", 1.0, 10.0);
		Event endEvent = new Event(42, "b", 1.0);

		harness.processElement(new StreamRecord<>(startEvent, 1L));
		harness.processElement(new StreamRecord<>(new Event(42, "d", 1.0), 4L));
		harness.processElement(new StreamRecord<Event>(middleEvent, 4L));
		harness.processElement(new StreamRecord<>(endEvent, 4L));

		// verify the number of invocations NFA is updated
		Mockito.verify(nfaOperatorStateSpy, Mockito.times(3)).update(Mockito.any());

		// get and verify the output
		Queue<Object> result = harness.getOutput();

		assertEquals(1, result.size());

		verifyPattern(result.poll(), startEvent, middleEvent, endEvent);
	} finally {
		harness.close();
	}
}
 
Example 12
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyedCEPOperatorNFAUpdateTimesWithRocksDB() throws Exception {

	String rocksDbPath = tempFolder.newFolder().getAbsolutePath();
	RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend(new MemoryStateBackend());
	rocksDBStateBackend.setDbStoragePath(rocksDbPath);

	CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOpearator(
		true,
		new SimpleNFAFactory());
	OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(
		operator);

	try {
		harness.setStateBackend(rocksDBStateBackend);

		harness.open();

		final ValueState nfaOperatorState = (ValueState) Whitebox.<ValueState>getInternalState(operator, "computationStates");
		final ValueState nfaOperatorStateSpy = Mockito.spy(nfaOperatorState);
		Whitebox.setInternalState(operator, "computationStates", nfaOperatorStateSpy);

		Event startEvent = new Event(42, "c", 1.0);
		SubEvent middleEvent = new SubEvent(42, "a", 1.0, 10.0);
		Event endEvent = new Event(42, "b", 1.0);

		harness.processElement(new StreamRecord<>(startEvent, 1L));
		harness.processElement(new StreamRecord<>(new Event(42, "d", 1.0), 4L));
		harness.processElement(new StreamRecord<Event>(middleEvent, 4L));
		harness.processElement(new StreamRecord<>(endEvent, 4L));

		// verify the number of invocations NFA is updated
		Mockito.verify(nfaOperatorStateSpy, Mockito.times(3)).update(Mockito.any());

		// get and verify the output
		Queue<Object> result = harness.getOutput();

		assertEquals(1, result.size());

		verifyPattern(result.poll(), startEvent, middleEvent, endEvent);
	} finally {
		harness.close();
	}
}