use of org.apache.beam.runners.core.TimerInternals.TimerDataCoder in project beam by apache.
the class ApexTimerInternalsTest method testSerialization.
@Test
public void testSerialization() {
TimerDataCoder timerDataCoder = TimerDataCoder.of(GlobalWindow.Coder.INSTANCE);
TimerData timerData = TimerData.of("arbitrary-id", StateNamespaces.global(), new Instant(0), TimeDomain.EVENT_TIME);
String key = "key";
ApexTimerInternals<String> timerInternals = new ApexTimerInternals<>(timerDataCoder);
timerInternals.setContext(key, StringUtf8Coder.of(), Instant.now(), null);
timerInternals.setTimer(timerData);
ApexTimerInternals<String> cloned;
assertNotNull("Serialization", cloned = KryoCloneUtils.cloneObject(timerInternals));
cloned.setContext(key, StringUtf8Coder.of(), Instant.now(), null);
Map<?, Set<Slice>> timers = cloned.getTimerSet(TimeDomain.EVENT_TIME).getMap();
assertEquals(1, timers.size());
}
use of org.apache.beam.runners.core.TimerInternals.TimerDataCoder in project beam by apache.
the class ApexTimerInternalsTest method testEventTimeTimers.
@Test
public void testEventTimeTimers() {
final Map<String, Collection<TimerData>> firedTimers = new HashMap<>();
TimerProcessor<String> timerProcessor = new TimerProcessor<String>() {
@Override
public void fireTimer(String key, Collection<TimerData> timerData) {
firedTimers.put(key, timerData);
}
};
TimerDataCoder timerDataCoder = TimerDataCoder.of(GlobalWindow.Coder.INSTANCE);
String key1 = "key1";
Instant instant0 = new Instant(0);
Instant instant1 = new Instant(1);
Instant instant2 = new Instant(2);
ApexTimerInternals<String> timerInternals = new ApexTimerInternals<>(timerDataCoder);
timerInternals.setContext(key1, StringUtf8Coder.of(), Instant.now(), null);
TimerData timerData0 = TimerData.of("timerData0", StateNamespaces.global(), instant0, TimeDomain.EVENT_TIME);
timerInternals.setTimer(timerData0);
TimerData timerData1 = TimerData.of("timerData1", StateNamespaces.global(), instant1, TimeDomain.EVENT_TIME);
timerInternals.setTimer(timerData1);
timerInternals.fireReadyTimers(instant0.getMillis(), timerProcessor, TimeDomain.EVENT_TIME);
assertEquals(0, firedTimers.size());
firedTimers.clear();
timerInternals.fireReadyTimers(instant1.getMillis(), timerProcessor, TimeDomain.PROCESSING_TIME);
assertEquals(0, firedTimers.size());
timerInternals.fireReadyTimers(instant1.getMillis(), timerProcessor, TimeDomain.EVENT_TIME);
assertEquals(1, firedTimers.size());
assertEquals(Sets.newHashSet(timerData0), Sets.newHashSet(firedTimers.values().iterator().next()));
firedTimers.clear();
timerInternals.fireReadyTimers(instant2.getMillis(), timerProcessor, TimeDomain.EVENT_TIME);
assertEquals(1, firedTimers.size());
assertEquals(Sets.newHashSet(timerData1), Sets.newHashSet(firedTimers.values().iterator().next()));
firedTimers.clear();
}
use of org.apache.beam.runners.core.TimerInternals.TimerDataCoder in project beam by apache.
the class ApexTimerInternalsTest method testDeleteTimer.
@Test
public void testDeleteTimer() {
TimerDataCoder timerDataCoder = TimerDataCoder.of(GlobalWindow.Coder.INSTANCE);
String key1 = "key1";
Instant instant0 = new Instant(0);
Instant instant1 = new Instant(1);
ApexTimerInternals<String> timerInternals = new ApexTimerInternals<>(timerDataCoder);
timerInternals.setContext(key1, StringUtf8Coder.of(), Instant.now(), null);
TimerData timerData0 = TimerData.of("timerData0", StateNamespaces.global(), instant0, TimeDomain.EVENT_TIME);
timerInternals.setTimer(timerData0);
TimerData timerData1 = TimerData.of("timerData1", StateNamespaces.global(), instant1, TimeDomain.EVENT_TIME);
timerInternals.setTimer(timerData1);
Map<?, Set<Slice>> timerMap = timerInternals.getTimerSet(TimeDomain.EVENT_TIME).getMap();
assertEquals(1, timerMap.size());
assertEquals(2, timerMap.values().iterator().next().size());
timerInternals.deleteTimer(timerData0.getNamespace(), timerData0.getTimerId());
assertEquals(1, timerMap.size());
assertEquals(1, timerMap.values().iterator().next().size());
timerInternals.deleteTimer(timerData1.getNamespace(), timerData1.getTimerId(), TimeDomain.PROCESSING_TIME);
assertEquals(1, timerMap.size());
assertEquals(1, timerMap.values().iterator().next().size());
timerInternals.deleteTimer(timerData1.getNamespace(), timerData1.getTimerId(), TimeDomain.EVENT_TIME);
assertEquals(0, timerMap.size());
}
Aggregations