-
-
Notifications
You must be signed in to change notification settings - Fork 27.1k
fixing busy waiting in abstraction and eliminate busy-waiting #3302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
240e8a9
4ba85f1
df5d21a
c1a3999
c02dcea
f0fe72f
e9deb6e
447d5df
351cf60
093d086
9f845de
cc9578e
8c1dbc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,47 +24,166 @@ | |
*/ | ||
package com.iluwatar.logaggregation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.time.LocalDateTime; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
/** | ||
* Unit tests for LogAggregator class. | ||
* | ||
* <p>Tests the event-driven log aggregation functionality including threshold-based flushing, | ||
* scheduled periodic flushing, and graceful shutdown behavior. | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class LogAggregatorTest { | ||
|
||
@Mock private CentralLogStore centralLogStore; | ||
|
||
private LogAggregator logAggregator; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
logAggregator = new LogAggregator(centralLogStore, LogLevel.INFO); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws InterruptedException { | ||
if (logAggregator != null && logAggregator.isRunning()) { | ||
logAggregator.stop(); | ||
logAggregator.awaitShutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem() { | ||
void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem() | ||
throws InterruptedException { | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 1")); | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 2")); | ||
|
||
assertEquals(2, logAggregator.getLogCount()); | ||
verifyNoInteractionsWithCentralLogStore(); | ||
|
||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 3")); | ||
|
||
Thread.sleep(1000); | ||
|
||
verifyCentralLogStoreInvokedTimes(3); | ||
assertEquals(0, logAggregator.getLogCount()); | ||
Comment on lines
81
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
Comment on lines
81
to
83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
@Test | ||
void whenDebugLogIsCollected_thenNoLogsShouldBeStored() { | ||
void whenDebugLogIsCollected_thenNoLogsShouldBeStored() throws InterruptedException { | ||
logAggregator.collectLog(createLogEntry(LogLevel.DEBUG, "Sample debug log message")); | ||
|
||
assertEquals(0, logAggregator.getLogCount()); | ||
assertEquals(0, logAggregator.getBufferSize()); | ||
|
||
Thread.sleep(500); | ||
|
||
verifyNoInteractionsWithCentralLogStore(); | ||
} | ||
|
||
@Test | ||
void whenTwoLogsCollected_thenBufferShouldContainThem() { | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Message 1")); | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Message 2")); | ||
|
||
assertEquals(2, logAggregator.getLogCount()); | ||
assertEquals(2, logAggregator.getBufferSize()); | ||
|
||
verifyNoInteractionsWithCentralLogStore(); | ||
} | ||
|
||
@Test | ||
void whenScheduledFlushOccurs_thenBufferedLogsShouldBeStored() throws InterruptedException { | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Scheduled flush test")); | ||
|
||
assertEquals(1, logAggregator.getLogCount()); | ||
verifyNoInteractionsWithCentralLogStore(); | ||
|
||
Thread.sleep(6000); | ||
|
||
verifyCentralLogStoreInvokedTimes(1); | ||
assertEquals(0, logAggregator.getLogCount()); | ||
} | ||
|
||
@Test | ||
void whenLogAggregatorStopped_thenRemainingLogsShouldBeStored() throws InterruptedException { | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Final message 1")); | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Final message 2")); | ||
|
||
assertEquals(2, logAggregator.getLogCount()); | ||
verifyNoInteractionsWithCentralLogStore(); | ||
|
||
logAggregator.stop(); | ||
logAggregator.awaitShutdown(); | ||
|
||
verifyCentralLogStoreInvokedTimes(2); | ||
assertEquals(0, logAggregator.getLogCount()); | ||
assertFalse(logAggregator.isRunning()); | ||
} | ||
|
||
@Test | ||
void whenLogLevelBelowThreshold_thenLogShouldBeFiltered() { | ||
logAggregator.collectLog(createLogEntry(LogLevel.DEBUG, "Debug message")); | ||
|
||
assertEquals(0, logAggregator.getLogCount()); | ||
assertEquals(0, logAggregator.getBufferSize()); | ||
verifyNoInteractionsWithCentralLogStore(); | ||
} | ||
|
||
@Test | ||
void whenLogLevelAtOrAboveThreshold_thenLogShouldBeAccepted() { | ||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Info message")); | ||
logAggregator.collectLog(createLogEntry(LogLevel.ERROR, "Error message")); | ||
|
||
assertEquals(2, logAggregator.getLogCount()); | ||
assertEquals(2, logAggregator.getBufferSize()); | ||
} | ||
|
||
@Test | ||
void whenNullLogLevelProvided_thenLogShouldBeSkipped() { | ||
LogEntry nullLevelEntry = | ||
new LogEntry("ServiceA", null, "Null level message", LocalDateTime.now()); | ||
|
||
logAggregator.collectLog(nullLevelEntry); | ||
|
||
assertEquals(0, logAggregator.getLogCount()); | ||
verifyNoInteractionsWithCentralLogStore(); | ||
} | ||
|
||
@Test | ||
void whenLogAggregatorIsShutdown_thenNewLogsShouldBeRejected() throws InterruptedException { | ||
logAggregator.stop(); | ||
logAggregator.awaitShutdown(); | ||
|
||
assertFalse(logAggregator.isRunning()); | ||
|
||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Post-shutdown message")); | ||
|
||
assertEquals(0, logAggregator.getLogCount()); | ||
verifyNoInteractionsWithCentralLogStore(); | ||
} | ||
|
||
@Test | ||
void testBasicFunctionality() throws InterruptedException { | ||
assertTrue(logAggregator.isRunning()); | ||
|
||
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Basic test")); | ||
assertEquals(1, logAggregator.getLogCount()); | ||
} | ||
|
||
private static LogEntry createLogEntry(LogLevel logLevel, String message) { | ||
return new LogEntry("ServiceA", logLevel, message, LocalDateTime.now()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
verifyCentralLogStoreInvokedTimes
method is not defined in the provided code snippet. Please define this method or use the appropriate Mockito verification methods.