/
SimpleEventSchedulerTest.java
122 lines (110 loc) · 4.67 KB
/
SimpleEventSchedulerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2010-2018. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.axonframework.eventhandling.scheduling.java;
import org.axonframework.eventhandling.EventBus;
import org.axonframework.eventhandling.EventMessage;
import org.axonframework.eventhandling.GenericEventMessage;
import org.axonframework.eventhandling.saga.Saga;
import org.axonframework.eventhandling.scheduling.ScheduleToken;
import org.junit.*;
import org.mockito.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* @author Allard Buijze
* @author Nakul Mishra
*/
public class SimpleEventSchedulerTest {
private SimpleEventScheduler testSubject;
private EventBus eventBus;
private ScheduledExecutorService scheduledExecutorService;
@Before
public void setUp() {
eventBus = mock(EventBus.class);
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
testSubject = SimpleEventScheduler.builder()
.scheduledExecutorService(scheduledExecutorService)
.eventBus(eventBus)
.build();
}
@After
public void tearDown() {
if (scheduledExecutorService != null) {
scheduledExecutorService.shutdownNow();
}
}
@Test
public void testScheduleJob() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
latch.countDown();
return null;
}).when(eventBus).publish(isA(EventMessage.class));
Saga mockSaga = mock(Saga.class);
when(mockSaga.getSagaIdentifier()).thenReturn(UUID.randomUUID().toString());
testSubject.schedule(Duration.ofMillis(30), new Object());
latch.await(1, TimeUnit.SECONDS);
verify(eventBus).publish(isA(EventMessage.class));
}
@Test
public void testScheduleTokenIsSerializable() throws IOException, ClassNotFoundException {
ScheduleToken token = testSubject.schedule(Duration.ZERO, new Object());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(token);
oos.close();
ScheduleToken token2 = (ScheduleToken) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
.readObject();
testSubject.cancelSchedule(token2);
}
@Test
public void testCancelJob() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
latch.countDown();
return null;
}).when(eventBus).publish(isA(EventMessage.class));
Saga mockSaga = mock(Saga.class);
when(mockSaga.getSagaIdentifier()).thenReturn(UUID.randomUUID().toString());
EventMessage<Object> event1 = createEvent();
final EventMessage<Object> event2 = createEvent();
ScheduleToken token1 = testSubject.schedule(Duration.ofMillis(100), event1);
testSubject.schedule(Duration.ofMillis(120), event2);
testSubject.cancelSchedule(token1);
latch.await(1, TimeUnit.SECONDS);
verify(eventBus, never()).publish(event1);
verify(eventBus).publish(argThat((ArgumentMatcher<EventMessage>) item -> (item != null)
&& event2.getPayload().equals(item.getPayload())
&& event2.getMetaData().equals(item.getMetaData())));
scheduledExecutorService.shutdown();
assertTrue("Executor refused to shutdown within a second",
scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS));
}
private EventMessage<Object> createEvent() {
return new GenericEventMessage<>(new Object());
}
}