Skip to content

Commit 550e58a

Browse files
committed
tests
1 parent d981a7b commit 550e58a

10 files changed

Lines changed: 2010 additions & 0 deletions

File tree

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright 2025 Conductor Authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.netflix.conductor.client.events;
14+
15+
import java.time.Duration;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import com.netflix.conductor.client.events.task.TaskPayloadUsedEvent;
20+
import com.netflix.conductor.client.events.task.TaskResultPayloadSizeEvent;
21+
import com.netflix.conductor.client.events.taskrunner.PollCompleted;
22+
import com.netflix.conductor.client.events.taskrunner.PollFailure;
23+
import com.netflix.conductor.client.events.taskrunner.PollStarted;
24+
import com.netflix.conductor.client.events.taskrunner.TaskExecutionCompleted;
25+
import com.netflix.conductor.client.events.taskrunner.TaskExecutionFailure;
26+
import com.netflix.conductor.client.events.taskrunner.TaskExecutionStarted;
27+
import com.netflix.conductor.client.events.workflow.WorkflowInputPayloadSizeEvent;
28+
import com.netflix.conductor.client.events.workflow.WorkflowPayloadUsedEvent;
29+
import com.netflix.conductor.client.events.workflow.WorkflowStartedEvent;
30+
31+
import static org.junit.jupiter.api.Assertions.*;
32+
33+
class EventPojoTests {
34+
35+
// --- taskrunner package ---
36+
37+
@Test
38+
void testPollStarted() {
39+
PollStarted event = new PollStarted("HTTP_TASK");
40+
41+
assertEquals("HTTP_TASK", event.getTaskType());
42+
assertNotNull(event.getTime());
43+
}
44+
45+
@Test
46+
void testPollCompleted() {
47+
PollCompleted event = new PollCompleted("HTTP_TASK", 500L);
48+
49+
assertEquals("HTTP_TASK", event.getTaskType());
50+
assertEquals(Duration.ofMillis(500), event.getDuration());
51+
assertNotNull(event.getTime());
52+
}
53+
54+
@Test
55+
void testPollFailure() {
56+
RuntimeException cause = new RuntimeException("timeout");
57+
PollFailure event = new PollFailure("HTTP_TASK", 1200L, cause);
58+
59+
assertEquals("HTTP_TASK", event.getTaskType());
60+
assertEquals(Duration.ofMillis(1200), event.getDuration());
61+
assertSame(cause, event.getCause());
62+
assertNotNull(event.getTime());
63+
}
64+
65+
@Test
66+
void testTaskExecutionStarted() {
67+
TaskExecutionStarted event = new TaskExecutionStarted("SIMPLE", "task-123", "worker-1");
68+
69+
assertEquals("SIMPLE", event.getTaskType());
70+
assertEquals("task-123", event.getTaskId());
71+
assertEquals("worker-1", event.getWorkerId());
72+
assertNotNull(event.getTime());
73+
}
74+
75+
@Test
76+
void testTaskExecutionCompleted() {
77+
TaskExecutionCompleted event = new TaskExecutionCompleted("SIMPLE", "task-456", "worker-2", 350L);
78+
79+
assertEquals("SIMPLE", event.getTaskType());
80+
assertEquals("task-456", event.getTaskId());
81+
assertEquals("worker-2", event.getWorkerId());
82+
assertEquals(Duration.ofMillis(350), event.getDuration());
83+
assertNotNull(event.getTime());
84+
}
85+
86+
@Test
87+
void testTaskExecutionFailure() {
88+
Throwable cause = new IllegalStateException("bad state");
89+
TaskExecutionFailure event = new TaskExecutionFailure("SIMPLE", "task-789", "worker-3", cause, 999L);
90+
91+
assertEquals("SIMPLE", event.getTaskType());
92+
assertEquals("task-789", event.getTaskId());
93+
assertEquals("worker-3", event.getWorkerId());
94+
assertSame(cause, event.getCause());
95+
assertEquals(Duration.ofMillis(999), event.getDuration());
96+
assertNotNull(event.getTime());
97+
}
98+
99+
// --- workflow package ---
100+
101+
@Test
102+
void testWorkflowStartedEvent() {
103+
Throwable t = new RuntimeException("fail");
104+
WorkflowStartedEvent event = new WorkflowStartedEvent("myWorkflow", 2, false, t);
105+
106+
assertEquals("myWorkflow", event.getName());
107+
assertEquals(2, event.getVersion());
108+
assertFalse(event.isSuccess());
109+
assertSame(t, event.getThrowable());
110+
assertNotNull(event.getTime());
111+
}
112+
113+
@Test
114+
void testWorkflowInputPayloadSizeEvent() {
115+
WorkflowInputPayloadSizeEvent event = new WorkflowInputPayloadSizeEvent("wf1", 1, 4096L);
116+
117+
assertEquals("wf1", event.getName());
118+
assertEquals(1, event.getVersion());
119+
assertEquals(4096L, event.getSize());
120+
assertNotNull(event.getTime());
121+
}
122+
123+
@Test
124+
void testWorkflowPayloadUsedEvent() {
125+
WorkflowPayloadUsedEvent event = new WorkflowPayloadUsedEvent("wf2", 3, "READ", "input");
126+
127+
assertEquals("wf2", event.getName());
128+
assertEquals(3, event.getVersion());
129+
assertEquals("READ", event.getOperation());
130+
assertEquals("input", event.getPayloadType());
131+
assertNotNull(event.getTime());
132+
}
133+
134+
// --- task package ---
135+
136+
@Test
137+
void testTaskPayloadUsedEvent() {
138+
TaskPayloadUsedEvent event = new TaskPayloadUsedEvent("DECIDE", "WRITE", "output");
139+
140+
assertEquals("DECIDE", event.getTaskType());
141+
assertEquals("WRITE", event.getOperation());
142+
assertEquals("output", event.getPayloadType());
143+
assertNotNull(event.getTime());
144+
}
145+
146+
@Test
147+
void testTaskResultPayloadSizeEvent() {
148+
TaskResultPayloadSizeEvent event = new TaskResultPayloadSizeEvent("HTTP_TASK", 2048L);
149+
150+
assertEquals("HTTP_TASK", event.getTaskType());
151+
assertEquals(2048L, event.getSize());
152+
assertNotNull(event.getTime());
153+
}
154+
155+
// --- cross-cutting tests ---
156+
157+
@Test
158+
void testPollStartedInheritance() {
159+
PollStarted event = new PollStarted("testTask");
160+
161+
assertInstanceOf(ConductorClientEvent.class, event);
162+
assertNotNull(event.getTime());
163+
}
164+
165+
@Test
166+
void testWorkflowStartedConvenienceConstructor() {
167+
WorkflowStartedEvent event = new WorkflowStartedEvent("simpleWf", 1);
168+
169+
assertEquals("simpleWf", event.getName());
170+
assertEquals(1, event.getVersion());
171+
assertTrue(event.isSuccess());
172+
assertNull(event.getThrowable());
173+
}
174+
175+
@Test
176+
void testToStringDoesNotThrow() {
177+
PollStarted pollStarted = new PollStarted("task1");
178+
assertDoesNotThrow(() -> pollStarted.toString());
179+
assertNotNull(pollStarted.toString());
180+
181+
WorkflowStartedEvent wfEvent = new WorkflowStartedEvent("wf", 1, false, null);
182+
assertDoesNotThrow(() -> wfEvent.toString());
183+
assertNotNull(wfEvent.toString());
184+
185+
TaskResultPayloadSizeEvent sizeEvent = new TaskResultPayloadSizeEvent("task2", 100L);
186+
assertDoesNotThrow(() -> sizeEvent.toString());
187+
assertNotNull(sizeEvent.toString());
188+
}
189+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2025 Conductor Authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.netflix.conductor.client.events.dispatcher;
14+
15+
import java.util.concurrent.CountDownLatch;
16+
import java.util.concurrent.TimeUnit;
17+
import java.util.concurrent.atomic.AtomicBoolean;
18+
import java.util.concurrent.atomic.AtomicReference;
19+
import java.util.function.Consumer;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import com.netflix.conductor.client.events.ConductorClientEvent;
25+
import com.netflix.conductor.client.events.taskrunner.PollStarted;
26+
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
class EventDispatcherTest {
30+
31+
private EventDispatcher<ConductorClientEvent> dispatcher;
32+
33+
@BeforeEach
34+
void setUp() {
35+
dispatcher = new EventDispatcher<>();
36+
}
37+
38+
@Test
39+
void testRegisterAndPublish() throws InterruptedException {
40+
CountDownLatch latch = new CountDownLatch(1);
41+
AtomicReference<PollStarted> received = new AtomicReference<>();
42+
43+
Consumer<PollStarted> listener = event -> {
44+
received.set(event);
45+
latch.countDown();
46+
};
47+
48+
dispatcher.register(PollStarted.class, listener);
49+
PollStarted event = new PollStarted("testTask");
50+
dispatcher.publish(event);
51+
52+
assertTrue(latch.await(1, TimeUnit.SECONDS), "Listener should have been called within 1 second");
53+
assertSame(event, received.get());
54+
assertEquals("testTask", received.get().getTaskType());
55+
}
56+
57+
@Test
58+
void testUnregister() throws InterruptedException {
59+
AtomicBoolean called = new AtomicBoolean(false);
60+
61+
Consumer<PollStarted> listener = event -> called.set(true);
62+
63+
dispatcher.register(PollStarted.class, listener);
64+
dispatcher.unregister(PollStarted.class, listener);
65+
dispatcher.publish(new PollStarted("testTask"));
66+
67+
// Give async execution a chance to run (if it were to fire)
68+
Thread.sleep(200);
69+
assertFalse(called.get(), "Listener should not have been called after unregistration");
70+
}
71+
72+
@Test
73+
void testPublishNoListeners() {
74+
// Should not throw NPE or any exception when no listeners are registered
75+
assertDoesNotThrow(() -> dispatcher.publish(new PollStarted("orphanTask")));
76+
}
77+
78+
@Test
79+
void testPromiscuousListener() throws InterruptedException {
80+
CountDownLatch latch = new CountDownLatch(1);
81+
AtomicReference<ConductorClientEvent> received = new AtomicReference<>();
82+
83+
Consumer<ConductorClientEvent> promiscuousListener = event -> {
84+
received.set(event);
85+
latch.countDown();
86+
};
87+
88+
dispatcher.register(ConductorClientEvent.class, promiscuousListener);
89+
PollStarted event = new PollStarted("anyTask");
90+
dispatcher.publish(event);
91+
92+
assertTrue(latch.await(1, TimeUnit.SECONDS), "Promiscuous listener should receive all event types");
93+
assertSame(event, received.get());
94+
assertInstanceOf(PollStarted.class, received.get());
95+
}
96+
97+
@Test
98+
void testMultipleListeners() throws InterruptedException {
99+
CountDownLatch latch = new CountDownLatch(2);
100+
101+
Consumer<PollStarted> listener1 = event -> latch.countDown();
102+
Consumer<PollStarted> listener2 = event -> latch.countDown();
103+
104+
dispatcher.register(PollStarted.class, listener1);
105+
dispatcher.register(PollStarted.class, listener2);
106+
dispatcher.publish(new PollStarted("multiTask"));
107+
108+
assertTrue(latch.await(1, TimeUnit.SECONDS), "Both listeners should have been called");
109+
}
110+
111+
@Test
112+
void testUnregisterFromEmpty() {
113+
Consumer<PollStarted> listener = event -> {};
114+
115+
// Unregister from a type that was never registered - should not throw
116+
assertDoesNotThrow(() -> dispatcher.unregister(PollStarted.class, listener));
117+
}
118+
}

0 commit comments

Comments
 (0)