Skip to content

Commit

Permalink
Add unit tests for replay logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaddoll committed Nov 1, 2024
1 parent 9d4bed7 commit 181c141
Showing 1 changed file with 296 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
/**
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
* except in compliance with the License. A copy of the License is located at
*
* <p>http://aws.amazon.com/apache2.0
*
* <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.uber.cadence.internal.logging;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import com.uber.cadence.internal.replay.ReplayAware;
import java.util.function.Supplier;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.Marker;

public class ReplayAwareLoggerTest {

@Mock private Logger mockLogger;

@Mock private ReplayAware mockReplayAware;

@Mock private Supplier<Boolean> enableLoggingInReplay;

private ReplayAwareLogger replayAwareLogger;

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
replayAwareLogger = new ReplayAwareLogger(mockLogger, mockReplayAware, enableLoggingInReplay);
}

// ===========================
// Utility Methods for Testing
// ===========================
private void setReplayMode(boolean isReplaying, boolean loggingEnabledInReplay) {
when(mockReplayAware.isReplaying()).thenReturn(isReplaying);
when(enableLoggingInReplay.get()).thenReturn(loggingEnabledInReplay);
}

// ===========================
// Tests for Each Log Level
// ===========================

// TRACE LEVEL
@Test
public void testTraceLoggingWhenEnabled() {
setReplayMode(false, false);
replayAwareLogger.trace("Trace message");
verify(mockLogger).trace("Trace message");
}

@Test
public void testTraceLoggingWhenReplayAndDisabled() {
setReplayMode(true, false);
replayAwareLogger.trace("Trace message");
verify(mockLogger, never()).trace("Trace message");
}

@Test
public void testTraceLoggingWhenReplayAndEnabled() {
setReplayMode(true, true);
replayAwareLogger.trace("Trace message");
verify(mockLogger).trace("Trace message");
}

@Test
public void testTraceLoggingWithMarker() {
Marker marker = mock(Marker.class);
setReplayMode(false, false);
replayAwareLogger.trace(marker, "Marker trace message");
verify(mockLogger).trace(marker, "Marker trace message");
}

@Test
public void testTraceLoggingWithMarkerReplayAndDisabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, false);
replayAwareLogger.trace(marker, "Marker trace message");
verify(mockLogger, never()).trace(marker, "Marker trace message");
}

@Test
public void testTraceLoggingWithMarkerReplayAndEnabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, true);
replayAwareLogger.trace(marker, "Marker trace message");
verify(mockLogger).trace(marker, "Marker trace message");
}

// DEBUG LEVEL
@Test
public void testDebugLoggingWhenEnabled() {
setReplayMode(false, false);
replayAwareLogger.debug("Debug message");
verify(mockLogger).debug("Debug message");
}

@Test
public void testDebugLoggingWhenReplayAndDisabled() {
setReplayMode(true, false);
replayAwareLogger.debug("Debug message");
verify(mockLogger, never()).debug("Debug message");
}

@Test
public void testDebugLoggingWhenReplayAndEnabled() {
setReplayMode(true, true);
replayAwareLogger.debug("Debug message");
verify(mockLogger).debug("Debug message");
}

@Test
public void testDebugLoggingWithMarker() {
Marker marker = mock(Marker.class);
setReplayMode(false, false);
replayAwareLogger.debug(marker, "Marker debug message");
verify(mockLogger).debug(marker, "Marker debug message");
}

@Test
public void testDebugLoggingWithMarkerReplayAndDisabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, false);
replayAwareLogger.debug(marker, "Marker debug message");
verify(mockLogger, never()).debug(marker, "Marker debug message");
}

@Test
public void testDebugLoggingWithMarkerReplayAndEnabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, true);
replayAwareLogger.debug(marker, "Marker debug message");
verify(mockLogger).debug(marker, "Marker debug message");
}

// INFO LEVEL
@Test
public void testInfoLoggingWhenEnabled() {
setReplayMode(false, false);
replayAwareLogger.info("Info message");
verify(mockLogger).info("Info message");
}

@Test
public void testInfoLoggingWhenReplayAndDisabled() {
setReplayMode(true, false);
replayAwareLogger.info("Info message");
verify(mockLogger, never()).info("Info message");
}

@Test
public void testInfoLoggingWhenReplayAndEnabled() {
setReplayMode(true, true);
replayAwareLogger.info("Info message");
verify(mockLogger).info("Info message");
}

@Test
public void testInfoLoggingWithMarker() {
Marker marker = mock(Marker.class);
setReplayMode(false, false);
replayAwareLogger.info(marker, "Marker info message");
verify(mockLogger).info(marker, "Marker info message");
}

@Test
public void testInfoLoggingWithMarkerReplayAndDisabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, false);
replayAwareLogger.info(marker, "Marker info message");
verify(mockLogger, never()).info(marker, "Marker info message");
}

@Test
public void testInfoLoggingWithMarkerReplayAndEnabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, true);
replayAwareLogger.info(marker, "Marker info message");
verify(mockLogger).info(marker, "Marker info message");
}

// WARN LEVEL
@Test
public void testWarnLoggingWhenEnabled() {
setReplayMode(false, false);
replayAwareLogger.warn("Warn message");
verify(mockLogger).warn("Warn message");
}

@Test
public void testWarnLoggingWhenReplayAndDisabled() {
setReplayMode(true, false);
replayAwareLogger.warn("Warn message");
verify(mockLogger, never()).warn("Warn message");
}

@Test
public void testWarnLoggingWhenReplayAndEnabled() {
setReplayMode(true, true);
replayAwareLogger.warn("Warn message");
verify(mockLogger).warn("Warn message");
}

@Test
public void testWarnLoggingWithMarker() {
Marker marker = mock(Marker.class);
setReplayMode(false, false);
replayAwareLogger.warn(marker, "Marker warn message");
verify(mockLogger).warn(marker, "Marker warn message");
}

@Test
public void testWarnLoggingWithMarkerReplayAndDisabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, false);
replayAwareLogger.warn(marker, "Marker warn message");
verify(mockLogger, never()).warn(marker, "Marker warn message");
}

@Test
public void testWarnLoggingWithMarkerReplayAndEnabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, true);
replayAwareLogger.warn(marker, "Marker warn message");
verify(mockLogger).warn(marker, "Marker warn message");
}

// ERROR LEVEL
@Test
public void testErrorLoggingWhenEnabled() {
setReplayMode(false, false);
replayAwareLogger.error("Error message");
verify(mockLogger).error("Error message");
}

@Test
public void testErrorLoggingWhenReplayAndDisabled() {
setReplayMode(true, false);
replayAwareLogger.error("Error message");
verify(mockLogger, never()).error("Error message");
}

@Test
public void testErrorLoggingWhenReplayAndEnabled() {
setReplayMode(true, true);
replayAwareLogger.error("Error message");
verify(mockLogger).error("Error message");
}

@Test
public void testErrorLoggingWithMarker() {
Marker marker = mock(Marker.class);
setReplayMode(false, false);
replayAwareLogger.error(marker, "Marker error message");
verify(mockLogger).error(marker, "Marker error message");
}

@Test
public void testErrorLoggingWithMarkerReplayAndDisabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, false);
replayAwareLogger.error(marker, "Marker error message");
verify(mockLogger, never()).error(marker, "Marker error message");
}

@Test
public void testErrorLoggingWithMarkerReplayAndEnabled() {
Marker marker = mock(Marker.class);
setReplayMode(true, true);
replayAwareLogger.error(marker, "Marker error message");
verify(mockLogger).error(marker, "Marker error message");
}

// ===========================
// Test getName Delegation
// ===========================
@Test
public void testGetNameDelegation() {
when(mockLogger.getName()).thenReturn("TestLogger");
assertEquals("TestLogger", replayAwareLogger.getName());
verify(mockLogger).getName();
}
}

0 comments on commit 181c141

Please sign in to comment.