Skip to content
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

[#noissue] Fix intermittent object initialization issue #11586

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.navercorp.pinpoint.common.util.CollectionUtils;
import com.navercorp.pinpoint.profiler.context.exception.DefaultExceptionRecorder;
import com.navercorp.pinpoint.profiler.context.exception.ExceptionRecorder;
import com.navercorp.pinpoint.profiler.context.exception.model.DefaultExceptionContext;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionWrapper;
Expand All @@ -26,6 +27,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand All @@ -42,30 +44,22 @@ public class DefaultExceptionRecorderTest {
ExceptionChainSampler exceptionChainSampler = new ExceptionChainSampler(1000);
ExceptionWrapperFactory exceptionWrapperFactory = new ExceptionWrapperFactory(10, 1048);


List<Throwable> exceptions = new ArrayList<>();
TestExceptionStorage exceptionStorage = new TestExceptionStorage();

ExceptionContext context = new DefaultExceptionContext(exceptionStorage);

DefaultExceptionRecorder exceptionRecorder = new DefaultExceptionRecorder(
exceptionChainSampler, exceptionWrapperFactory, context
);

long START_TIME = 1;

public List<ExceptionWrapper> outputStream = new ArrayList<>();

class TestExceptionStorage implements ExceptionStorage {

List<ExceptionWrapper> wrappers;
public List<ExceptionWrapper> outputStream;

public TestExceptionStorage() {
this.wrappers = new ArrayList<>();
this.outputStream = new ArrayList<>();
}

@Override
public void store(List<ExceptionWrapper> wrappers) {
logger.error(wrappers);
this.wrappers.addAll(wrappers);
}

Expand All @@ -85,27 +79,25 @@ public void close() {
public List<ExceptionWrapper> getWrappers() {
return this.wrappers;
}
}

private void resetContext() {
exceptionStorage.flush();
outputStream.clear();
exceptions.clear();
context = new DefaultExceptionContext(exceptionStorage);
public List<ExceptionWrapper> getOutputStream() {
return outputStream;
}
}

private final Function<Throwable, Throwable> throwableFunction = (Throwable th) -> {
record(th);
exceptions.add(th);
logger.info(th);
return th;
};


private void record(Throwable throwable) {
exceptionRecorder.recordException(throwable, START_TIME);
private Function<Throwable, Throwable> getThrowableFunction(
DefaultExceptionRecorder recorder,
List<Throwable> throwable
) {
return (Throwable th) -> {
recorder.recordException(th, START_TIME);
throwable.add(th);
logger.info(th);
return th;
};
}


private List<ExceptionWrapper> newExceptionWrappers(Throwable throwable, long startTime, long exceptionId) {
List<ExceptionWrapper> wrappers = new ArrayList<>();
exceptionWrapperFactory.addAllExceptionWrappers(wrappers, throwable, null, startTime, exceptionId, 0);
Expand All @@ -114,20 +106,37 @@ private List<ExceptionWrapper> newExceptionWrappers(Throwable throwable, long st

@Test
public void testRecordNothing() {
resetContext();
List<Throwable> exceptions = new ArrayList<>();
TestExceptionStorage exceptionStorage = new TestExceptionStorage();
ExceptionContext context = new DefaultExceptionContext(exceptionStorage);
DefaultExceptionRecorder exceptionRecorder = new DefaultExceptionRecorder(
exceptionChainSampler, exceptionWrapperFactory, context
);
Function<Throwable, Throwable> throwableFunction = getThrowableFunction(
exceptionRecorder, exceptions
);

exceptionRecorder.recordException(null, 0);
exceptionRecorder.close();

List<ExceptionWrapper> expected = new ArrayList<>();
List<ExceptionWrapper> actual = outputStream;
List<ExceptionWrapper> actual = exceptionStorage.getOutputStream();

Assertions.assertEquals(expected, actual);
}

@Test
public void testRecordException() {
resetContext();
List<Throwable> exceptions = new ArrayList<>();
TestExceptionStorage exceptionStorage = new TestExceptionStorage();
ExceptionContext context = new DefaultExceptionContext(exceptionStorage);
DefaultExceptionRecorder exceptionRecorder = new DefaultExceptionRecorder(
exceptionChainSampler, exceptionWrapperFactory, context
);
Function<Throwable, Throwable> throwableFunction = getThrowableFunction(
exceptionRecorder, exceptions
);

List<ExceptionWrapper> expected = null;
List<ExceptionWrapper> actual = null;

Expand All @@ -140,13 +149,22 @@ public void testRecordException() {
Assertions.assertTrue(actual.isEmpty());
}
exceptionRecorder.close();
actual = outputStream;
actual = exceptionStorage.getOutputStream();
Assertions.assertEquals(expected, actual);
}

@Test
public void testRecordNotChainedException() {
resetContext();
List<Throwable> exceptions = new ArrayList<>();
TestExceptionStorage exceptionStorage = new TestExceptionStorage();
ExceptionContext context = new DefaultExceptionContext(exceptionStorage);
DefaultExceptionRecorder exceptionRecorder = new DefaultExceptionRecorder(
exceptionChainSampler, exceptionWrapperFactory, context
);
Function<Throwable, Throwable> throwableFunction = getThrowableFunction(
exceptionRecorder, exceptions
);

List<ExceptionWrapper> expected1 = null;
List<ExceptionWrapper> expected2 = null;
List<ExceptionWrapper> actual1 = null;
Expand All @@ -159,24 +177,37 @@ public void testRecordNotChainedException() {
} catch (Throwable e) {
expected1 = newExceptionWrappers(exceptions.get(exceptions.size() - 2), START_TIME, 1);
exceptionRecorder.recordException(e, START_TIME);
logger.warn(exceptionStorage.getWrappers());
actual1 = new ArrayList<>(exceptionStorage.getWrappers());
throwable = e;
logger.warn(actual1);
logger.warn(actual2);
Assertions.assertFalse(actual1.isEmpty());
Assertions.assertEquals(expected1, actual1);
}


exceptionStorage.flush();
outputStream.clear();
exceptionStorage.getOutputStream().clear();
expected2 = newExceptionWrappers(throwable, START_TIME, 2);
exceptionRecorder.close();
actual2 = outputStream;
actual2 = exceptionStorage.getOutputStream();
Assertions.assertEquals(expected2, actual2);
}

@Test
public void testRecordRethrowGivenException() {
resetContext();

List<Throwable> exceptions = new ArrayList<>();
TestExceptionStorage exceptionStorage = new TestExceptionStorage();
ExceptionContext context = new DefaultExceptionContext(exceptionStorage);
DefaultExceptionRecorder exceptionRecorder = new DefaultExceptionRecorder(
exceptionChainSampler, exceptionWrapperFactory, context
);
Function<Throwable, Throwable> throwableFunction = getThrowableFunction(
exceptionRecorder, exceptions
);

List<ExceptionWrapper> expected = null;
List<ExceptionWrapper> actual = null;

Expand All @@ -190,7 +221,7 @@ public void testRecordRethrowGivenException() {
}

exceptionRecorder.close();
actual = outputStream;
actual = exceptionStorage.getOutputStream();
Assertions.assertEquals(expected, actual);
}

Expand Down