Skip to content

Commit c1a3999

Browse files
committed
minor fi log aggregator test
1 parent df5d21a commit c1a3999

File tree

1 file changed

+57
-32
lines changed

1 file changed

+57
-32
lines changed

microservices-log-aggregation/src/test/java/com/iluwatar/logaggregation/LogAggregatorTest.java

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
33
*
44
* The MIT License
@@ -27,48 +27,82 @@
2727
import static org.mockito.ArgumentMatchers.any;
2828
import static org.mockito.Mockito.times;
2929
import static org.mockito.Mockito.verify;
30+
// CRITICAL MISSING IMPORTS - FIXED!
31+
import static org.junit.jupiter.api.Assertions.*;
3032

3133
import java.time.LocalDateTime;
34+
import java.util.concurrent.TimeUnit;
3235
import org.junit.jupiter.api.BeforeEach;
36+
import org.junit.jupiter.api.AfterEach;
3337
import org.junit.jupiter.api.Test;
3438
import org.junit.jupiter.api.extension.ExtendWith;
3539
import org.mockito.Mock;
3640
import org.mockito.junit.jupiter.MockitoExtension;
3741

42+
/**
43+
* FIXED Championship Test Suite - LogAggregator
44+
*
45+
* Fixed to work with the actual LogAggregator API and proper imports
46+
*/
3847
@ExtendWith(MockitoExtension.class)
3948
class LogAggregatorTest {
40-
41-
@Mock private CentralLogStore centralLogStore;
49+
50+
@Mock
51+
private CentralLogStore centralLogStore;
52+
4253
private LogAggregator logAggregator;
4354

4455
@BeforeEach
4556
void setUp() {
4657
logAggregator = new LogAggregator(centralLogStore, LogLevel.INFO);
4758
}
4859

60+
@AfterEach
61+
void tearDown() throws InterruptedException {
62+
// 🚀 CHAMPIONSHIP CLEANUP - Properly shutdown the event-driven aggregator
63+
if (logAggregator != null && logAggregator.isRunning()) {
64+
logAggregator.stop();
65+
logAggregator.awaitShutdown();
66+
}
67+
}
68+
4969
@Test
50-
void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem() {
70+
void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem() throws InterruptedException {
71+
// ELITE FIX: Account for asynchronous threshold-based flushing
5172
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 1"));
5273
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 2"));
53-
74+
75+
// At this point, we should have 2 logs in buffer, no flush yet
76+
assertEquals(2, logAggregator.getLogCount());
5477
verifyNoInteractionsWithCentralLogStore();
55-
78+
79+
// Third log should trigger immediate flush (threshold = 3)
5680
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 3"));
57-
81+
82+
// CHAMPIONSHIP WAIT: Allow time for ScheduledExecutorService to process
83+
Thread.sleep(1000); // Give executor time to flush
84+
5885
verifyCentralLogStoreInvokedTimes(3);
86+
assertEquals(0, logAggregator.getLogCount()); // Buffer should be empty after flush
5987
}
6088

6189
@Test
62-
void whenDebugLogIsCollected_thenNoLogsShouldBeStored() {
90+
void whenDebugLogIsCollected_thenNoLogsShouldBeStored() throws InterruptedException {
6391
logAggregator.collectLog(createLogEntry(LogLevel.DEBUG, "Sample debug log message"));
64-
92+
93+
// Debug log should be filtered out before reaching buffer
94+
assertEquals(0, logAggregator.getLogCount());
95+
assertEquals(0, logAggregator.getBufferSize());
96+
97+
// Wait a bit to ensure no delayed processing
98+
Thread.sleep(500);
99+
65100
verifyNoInteractionsWithCentralLogStore();
66101
}
67102

68-
69-
@Test
103+
@Test
70104
void whenTwoLogsCollected_thenBufferShouldContainThem() {
71-
// NEW TEST: Verify buffer state management
105+
// 🎯 NEW TEST: Verify buffer state management
72106
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Message 1"));
73107
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Message 2"));
74108

@@ -81,7 +115,7 @@ void whenTwoLogsCollected_thenBufferShouldContainThem() {
81115

82116
@Test
83117
void whenScheduledFlushOccurs_thenBufferedLogsShouldBeStored() throws InterruptedException {
84-
// NEW TEST: Verify scheduled periodic flushing
118+
// 🏆 NEW TEST: Verify scheduled periodic flushing
85119
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Scheduled flush test"));
86120

87121
assertEquals(1, logAggregator.getLogCount());
@@ -96,7 +130,7 @@ void whenScheduledFlushOccurs_thenBufferedLogsShouldBeStored() throws Interrupte
96130

97131
@Test
98132
void whenLogAggregatorStopped_thenRemainingLogsShouldBeStored() throws InterruptedException {
99-
// NEW TEST: Verify graceful shutdown flushes remaining logs
133+
// 🚀 NEW TEST: Verify graceful shutdown flushes remaining logs
100134
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Final message 1"));
101135
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Final message 2"));
102136

@@ -114,9 +148,8 @@ void whenLogAggregatorStopped_thenRemainingLogsShouldBeStored() throws Interrupt
114148

115149
@Test
116150
void whenLogLevelBelowThreshold_thenLogShouldBeFiltered() {
117-
// 🎯 ENHANCED TEST: Test all log levels below INFO
151+
// FIXED TEST: Only use available log levels
118152
logAggregator.collectLog(createLogEntry(LogLevel.DEBUG, "Debug message"));
119-
logAggregator.collectLog(createLogEntry(LogLevel.TRACE, "Trace message"));
120153

121154
assertEquals(0, logAggregator.getLogCount());
122155
assertEquals(0, logAggregator.getBufferSize());
@@ -125,18 +158,17 @@ void whenLogLevelBelowThreshold_thenLogShouldBeFiltered() {
125158

126159
@Test
127160
void whenLogLevelAtOrAboveThreshold_thenLogShouldBeAccepted() {
128-
// NEW TEST: Verify all accepted log levels
161+
// FIXED TEST: Use only available log levels (INFO, DEBUG, ERROR)
129162
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Info message"));
130-
logAggregator.collectLog(createLogEntry(LogLevel.WARN, "Warning message"));
131163
logAggregator.collectLog(createLogEntry(LogLevel.ERROR, "Error message"));
132164

133-
assertEquals(3, logAggregator.getLogCount());
134-
assertEquals(3, logAggregator.getBufferSize());
165+
assertEquals(2, logAggregator.getLogCount());
166+
assertEquals(2, logAggregator.getBufferSize());
135167
}
136168

137169
@Test
138170
void whenNullLogLevelProvided_thenLogShouldBeSkipped() {
139-
// EDGE CASE TEST: Null safety
171+
// EDGE CASE TEST: Null safety
140172
LogEntry nullLevelEntry = new LogEntry("ServiceA", null, "Null level message", LocalDateTime.now());
141173

142174
logAggregator.collectLog(nullLevelEntry);
@@ -147,7 +179,7 @@ void whenNullLogLevelProvided_thenLogShouldBeSkipped() {
147179

148180
@Test
149181
void whenLogAggregatorIsShutdown_thenNewLogsShouldBeRejected() throws InterruptedException {
150-
// NEW TEST: Verify shutdown behavior
182+
// NEW TEST: Verify shutdown behavior
151183
logAggregator.stop();
152184
logAggregator.awaitShutdown();
153185

@@ -161,19 +193,12 @@ void whenLogAggregatorIsShutdown_thenNewLogsShouldBeRejected() throws Interrupte
161193
}
162194

163195
@Test
164-
void testPerformanceMetrics() throws InterruptedException {
165-
// CHAMPIONSHIP TEST: Verify performance monitoring
196+
void testBasicFunctionality() throws InterruptedException {
197+
// SIMPLIFIED TEST: Basic functionality without advanced features
166198
assertTrue(logAggregator.isRunning());
167-
assertFalse(logAggregator.isSuspended());
168-
assertEquals(4.0, logAggregator.getFrameRate(), 0.1); // 1000ms / 250ms = 4 FPS
169199

170-
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Performance test"));
200+
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Basic test"));
171201
assertEquals(1, logAggregator.getLogCount());
172-
173-
String report = logAggregator.getPerformanceReport();
174-
assertNotNull(report);
175-
assertTrue(report.contains("Event-Driven"));
176-
assertTrue(report.contains("Zero Busy-Wait"));
177202
}
178203

179204
private static LogEntry createLogEntry(LogLevel logLevel, String message) {

0 commit comments

Comments
 (0)