Skip to content

Commit 7d9d3dc

Browse files
committed
Add test for the enable disable processor
1 parent 599381a commit 7d9d3dc

File tree

3 files changed

+127
-5
lines changed

3 files changed

+127
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@ 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,
30+
@Autowired TaskExecutor taskExecutor,
31+
@Value("${processors.chunking.size:10000}") int chunkSize) {
32+
this.channelProcessors = channelProcessors;
33+
this.taskExecutor = taskExecutor;
34+
this.chunkSize = chunkSize;
35+
}
2836

2937
long getProcessorCount() {
3038
return channelProcessors.size();
@@ -68,7 +76,6 @@ public void sendToProcessors(List<Channel> channels) {
6876
while (true) {
6977
List<Channel> chunk = new ArrayList<>(chunkSize);
7078
for (int i = 0; i < chunkSize && split.tryAdvance(chunk::add); i++) {}
71-
;
7279
if (chunk.isEmpty()) break;
7380
channelProcessor.process(chunk);
7481
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 =
71+
new ChannelProcessorService(List.of(dummyProcessor), Runnable::run, 10);
72+
}
73+
74+
@Test
75+
void testEnableAndDisableDummyProcessor() {
76+
// Initially, the dummy processor should be disabled
77+
Assertions.assertFalse(
78+
dummyProcessor.enabled(), "Dummy processor should be disabled initially");
79+
80+
// Enable the dummy processor
81+
channelProcessorService.setProcessorEnabled("DummyProcessor", true);
82+
Assertions.assertTrue(dummyProcessor.enabled(), "Dummy processor should be enabled");
83+
84+
// Disable the dummy processor
85+
channelProcessorService.setProcessorEnabled("DummyProcessor", false);
86+
Assertions.assertFalse(dummyProcessor.enabled(), "Dummy processor should be disabled");
87+
}
88+
89+
@Test
90+
void testDummyProcessorProcessing() {
91+
// Disable the dummy processor
92+
dummyProcessor.reset();
93+
channelProcessorService.setProcessorEnabled("DummyProcessor", false);
94+
Assertions.assertFalse(dummyProcessor.enabled(), "Dummy processor should be disabled");
95+
96+
// Process a channel - dummy processor should not be called
97+
channelProcessorService.sendToProcessors(
98+
Collections.singletonList(new Channel("test-channel")));
99+
Assertions.assertFalse(
100+
dummyProcessor.hasBeenProcessed(), "Dummy processor should not have been called");
101+
102+
// Enable the dummy processor
103+
channelProcessorService.setProcessorEnabled("DummyProcessor", true);
104+
Assertions.assertTrue(dummyProcessor.enabled(), "Dummy processor should be enabled");
105+
106+
// Process a channel - dummy processor should be called
107+
channelProcessorService.sendToProcessors(
108+
Collections.singletonList(new Channel("test-channel")));
109+
Assertions.assertTrue(
110+
dummyProcessor.hasBeenProcessed(), "Dummy processor should have been called");
111+
}
112+
}

src/test/resources/application_test.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ [email protected]@
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)