Skip to content

Commit b5c014b

Browse files
committed
Add test for the enable disable processor
1 parent 9911cd6 commit b5c014b

File tree

3 files changed

+124
-5
lines changed

3 files changed

+124
-5
lines changed

src/main/java/org/phoebus/channelfinder/processors/ChannelProcessorService.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ public class ChannelProcessorService {
1919

2020
private static final Logger logger = Logger.getLogger(ChannelProcessorService.class.getName());
2121

22-
@Autowired private List<ChannelProcessor> channelProcessors;
22+
private final List<ChannelProcessor> channelProcessors;
2323

24-
@Autowired private TaskExecutor taskExecutor;
24+
private final TaskExecutor taskExecutor;
2525

26-
@Value("${processors.chunking.size:10000}")
27-
private int chunkSize;
26+
private final int chunkSize;
27+
28+
public ChannelProcessorService(
29+
@Autowired List<ChannelProcessor> channelProcessors, @Autowired TaskExecutor taskExecutor, @Value("${processors.chunking.size:10000}") int chunkSize) {
30+
this.channelProcessors = channelProcessors;
31+
this.taskExecutor = taskExecutor;
32+
this.chunkSize = chunkSize;
33+
}
2834

2935
long getProcessorCount() {
3036
return channelProcessors.size();
@@ -68,7 +74,6 @@ public void sendToProcessors(List<Channel> channels) {
6874
while (true) {
6975
List<Channel> chunk = new ArrayList<>(chunkSize);
7076
for (int i = 0; i < chunkSize && split.tryAdvance(chunk::add); i++) {}
71-
;
7277
if (chunk.isEmpty()) break;
7378
channelProcessor.process(chunk);
7479
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.phoebus.channelfinder.processors;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.concurrent.atomic.AtomicBoolean;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.phoebus.channelfinder.entity.Channel;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.test.context.TestPropertySource;
17+
18+
@SpringBootTest()
19+
@TestPropertySource(value = "classpath:application_test.properties")
20+
class ChannelProcessorServiceTest {
21+
22+
private ChannelProcessorService channelProcessorService;
23+
24+
@Autowired private DummyProcessor dummyProcessor;
25+
26+
@Configuration
27+
static class TestConfig {
28+
@Bean
29+
public DummyProcessor dummyProcessor() {
30+
return new DummyProcessor();
31+
}
32+
}
33+
34+
static class DummyProcessor implements ChannelProcessor {
35+
private final AtomicBoolean enabled = new AtomicBoolean(false);
36+
private final AtomicBoolean processed = new AtomicBoolean(false);
37+
38+
@Override
39+
public boolean enabled() {
40+
return enabled.get();
41+
}
42+
43+
@Override
44+
public void setEnabled(boolean enabled) {
45+
this.enabled.set(enabled);
46+
}
47+
48+
@Override
49+
public ChannelProcessorInfo processorInfo() {
50+
return new ChannelProcessorInfo("DummyProcessor", enabled.get(), Map.of());
51+
}
52+
53+
@Override
54+
public long process(List<Channel> channels) throws JsonProcessingException {
55+
processed.set(true);
56+
return channels.size();
57+
}
58+
59+
public boolean hasBeenProcessed() {
60+
return processed.get();
61+
}
62+
63+
public void reset() {
64+
processed.set(false);
65+
}
66+
}
67+
68+
@BeforeEach
69+
void setUp() {
70+
channelProcessorService = new ChannelProcessorService(List.of(dummyProcessor), Runnable::run, 10);
71+
}
72+
73+
@Test
74+
void testEnableAndDisableDummyProcessor() {
75+
// Initially, the dummy processor should be disabled
76+
Assertions.assertFalse(
77+
dummyProcessor.enabled(), "Dummy processor should be disabled initially");
78+
79+
// Enable the dummy processor
80+
channelProcessorService.setProcessorEnabled("DummyProcessor", true);
81+
Assertions.assertTrue(dummyProcessor.enabled(), "Dummy processor should be enabled");
82+
83+
// Disable the dummy processor
84+
channelProcessorService.setProcessorEnabled("DummyProcessor", false);
85+
Assertions.assertFalse(dummyProcessor.enabled(), "Dummy processor should be disabled");
86+
}
87+
88+
@Test
89+
void testDummyProcessorProcessing() {
90+
// Disable the dummy processor
91+
dummyProcessor.reset();
92+
channelProcessorService.setProcessorEnabled("DummyProcessor", false);
93+
Assertions.assertFalse(dummyProcessor.enabled(), "Dummy processor should be disabled");
94+
95+
// Process a channel - dummy processor should not be called
96+
channelProcessorService.sendToProcessors(
97+
Collections.singletonList(new Channel("test-channel")));
98+
Assertions.assertFalse(
99+
dummyProcessor.hasBeenProcessed(), "Dummy processor should not have been called");
100+
101+
// Enable the dummy processor
102+
channelProcessorService.setProcessorEnabled("DummyProcessor", true);
103+
Assertions.assertTrue(dummyProcessor.enabled(), "Dummy processor should be enabled");
104+
105+
// Process a channel - dummy processor should be called
106+
channelProcessorService.sendToProcessors(
107+
Collections.singletonList(new Channel("test-channel")));
108+
Assertions.assertTrue(
109+
dummyProcessor.hasBeenProcessed(), "Dummy processor should have been called");
110+
}
111+
}

src/test/resources/application_test.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ channelfinder.version=@project.version@
8585
# DEBUG level will log all requests and responses to and from the REST end points
8686
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=INFO
8787

88+
################ Processor ##################################################
89+
processors.chunking.size=10000
90+
8891
################ Archiver Appliance Configuration Processor #################
8992
aa.urls={'default': 'http://localhost:17665'}
9093
aa.default_alias=default

0 commit comments

Comments
 (0)