mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 23:59:27 -05:00
test: capture log records through LogCapture.capture (#8492)
This commit is contained in:
1 parent
8206115dc8
commit
edb42cebf1
7 files changed
+24
-179
No files matched your search
@@ -66,10 +66,6 @@ import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -807,29 +803,7 @@ class EmulatorLifecycleTest {
|
||||
|
||||
/** Collects the messages EmulatorLifecycle logs while {@code action} runs. */
|
||||
private List<String> lifecycleLogMessages(Runnable action) {
|
||||
Logger logger = Logger.getLogger(EmulatorLifecycle.class.getName());
|
||||
List<String> messages = new CopyOnWriteArrayList<>();
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
messages.add(LogCapture.formatted(logRecord));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
logger.addHandler(handler);
|
||||
try {
|
||||
action.run();
|
||||
} finally {
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
return messages;
|
||||
return LogCapture.capture(EmulatorLifecycle.class, action).stream().map(LogCapture::formatted).toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-27
@@ -7,16 +7,14 @@ import io.floci.aws.services.cloudformation.provisioners.CfnResourceDispatcher;
|
||||
import io.floci.aws.services.cloudformation.provisioners.LambdaCfnProvisioner;
|
||||
import io.floci.aws.services.lambda.LambdaService;
|
||||
import io.floci.aws.services.lambda.model.LambdaFunction;
|
||||
import io.floci.aws.testutil.LogCapture;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -147,30 +145,7 @@ class CloudFormationLambdaLegacyNameModeTest {
|
||||
|
||||
/** Collects the messages LambdaCfnProvisioner logs while {@code action} runs. */
|
||||
private List<String> provisionerLogMessages(Runnable action) {
|
||||
Logger logger =
|
||||
Logger.getLogger(LambdaCfnProvisioner.class.getName());
|
||||
List<String> messages = new CopyOnWriteArrayList<>();
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
messages.add(logRecord.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
logger.addHandler(handler);
|
||||
try {
|
||||
action.run();
|
||||
} finally {
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
return messages;
|
||||
return LogCapture.capture(LambdaCfnProvisioner.class, action).stream().map(LogRecord::getMessage).toList();
|
||||
}
|
||||
|
||||
private StackResource provision(String propertiesJson, String existingPhysicalId,
|
||||
|
||||
+4
-27
@@ -13,12 +13,11 @@ import org.junit.jupiter.api.Test;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -201,31 +200,9 @@ class CloudFormationUnsupportedResourceTypeTest {
|
||||
/** Runs {@code action} with a handler on the provisioner's logger, collecting every record. */
|
||||
private <T> T whileCapturingProvisionerLogs(List<LogRecord> collected,
|
||||
Supplier<T> action) {
|
||||
Logger logger =
|
||||
Logger.getLogger(CfnResourceDispatcher.class.getName());
|
||||
Level original = logger.getLevel();
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
collected.add(logRecord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
logger.addHandler(handler);
|
||||
logger.setLevel(Level.ALL);
|
||||
try {
|
||||
return action.get();
|
||||
} finally {
|
||||
logger.setLevel(original);
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
AtomicReference<T> result = new AtomicReference<>();
|
||||
collected.addAll(LogCapture.capture(CfnResourceDispatcher.class, () -> result.set(action.get())));
|
||||
return result.get();
|
||||
}
|
||||
|
||||
private static List<String> rendered(List<LogRecord> records) {
|
||||
|
||||
+8
-30
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.services.lambda.LambdaService;
|
||||
import io.floci.aws.testutil.LogCapture;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -17,10 +18,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.LongConsumer;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -128,36 +126,16 @@ class CustomResourceResponseStoreTest {
|
||||
|
||||
@Test
|
||||
void toleratedWaitSliceTimeoutIsLogged() throws Exception {
|
||||
Logger logger = Logger.getLogger(CustomResourceResponseStore.class.getName());
|
||||
Level previousLevel = logger.getLevel();
|
||||
List<LogRecord> records = new ArrayList<>();
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
handler.setLevel(Level.ALL);
|
||||
logger.setLevel(Level.ALL);
|
||||
logger.addHandler(handler);
|
||||
try {
|
||||
List<LogRecord> records = LogCapture.capture(CustomResourceResponseStore.class, () -> {
|
||||
String token = store.register();
|
||||
heartbeatDuringWait(token);
|
||||
response.onWait(remaining -> store.complete(token, success()));
|
||||
|
||||
store.await(token, IDLE_TIMEOUT);
|
||||
} finally {
|
||||
logger.removeHandler(handler);
|
||||
logger.setLevel(previousLevel);
|
||||
}
|
||||
try {
|
||||
store.await(token, IDLE_TIMEOUT);
|
||||
} catch (TimeoutException e) {
|
||||
throw new AssertionError("the wait timed out", e);
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(records.stream().anyMatch(record -> record.getMessage() != null
|
||||
&& record.getMessage().contains("wait slice")),
|
||||
|
||||
@@ -10,11 +10,9 @@ import org.junit.jupiter.api.Test;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -3247,32 +3245,8 @@ class SamTransformProcessorTest {
|
||||
}
|
||||
""");
|
||||
|
||||
List<LogRecord> logged = new CopyOnWriteArrayList<>();
|
||||
Logger logger =
|
||||
Logger.getLogger(SamTransformProcessor.class.getName());
|
||||
Level original = logger.getLevel();
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
logged.add(logRecord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
logger.addHandler(handler);
|
||||
logger.setLevel(Level.ALL);
|
||||
try {
|
||||
processor.expandSamTemplate(template);
|
||||
} finally {
|
||||
logger.setLevel(original);
|
||||
logger.removeHandler(handler);
|
||||
}
|
||||
List<LogRecord> logged = LogCapture.capture(SamTransformProcessor.class,
|
||||
() -> processor.expandSamTemplate(template));
|
||||
|
||||
String expected = "Unsupported SAM resource type AWS::Serverless::Application (MyApp): "
|
||||
+ "left in the template for the CloudFormation provisioner.";
|
||||
|
||||
+4
-39
@@ -31,10 +31,8 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -222,15 +220,8 @@ class StepFunctionsServicePersistenceTest {
|
||||
executions.putForAccount(OTHER_ACCOUNT, OTHER_ACCOUNT_EXECUTION_ARN,
|
||||
running(OTHER_ACCOUNT_EXECUTION_ARN));
|
||||
|
||||
List<LogRecord> logRecords = new ArrayList<>();
|
||||
Handler collector = collectorInto(logRecords);
|
||||
Logger serviceLogger = Logger.getLogger(StepFunctionsService.class.getName());
|
||||
serviceLogger.addHandler(collector);
|
||||
try {
|
||||
serviceWithStorage(storage).abortAbandonedExecutions();
|
||||
} finally {
|
||||
serviceLogger.removeHandler(collector);
|
||||
}
|
||||
List<LogRecord> logRecords = LogCapture.capture(StepFunctionsService.class,
|
||||
() -> serviceWithStorage(storage).abortAbandonedExecutions());
|
||||
|
||||
List<LogRecord> warnings = warningsIn(logRecords);
|
||||
assertEquals(1, warnings.size(),
|
||||
@@ -271,15 +262,8 @@ class StepFunctionsServicePersistenceTest {
|
||||
|
||||
// Nothing is RUNNING any more, so the second sweep retires nothing and says nothing: a WARN
|
||||
// on every clean boot is the noise the repo's logging rule forbids.
|
||||
List<LogRecord> logRecords = new ArrayList<>();
|
||||
Handler collector = collectorInto(logRecords);
|
||||
Logger serviceLogger = Logger.getLogger(StepFunctionsService.class.getName());
|
||||
serviceLogger.addHandler(collector);
|
||||
try {
|
||||
afterRestart.abortAbandonedExecutions();
|
||||
} finally {
|
||||
serviceLogger.removeHandler(collector);
|
||||
}
|
||||
List<LogRecord> logRecords = LogCapture.capture(StepFunctionsService.class,
|
||||
afterRestart::abortAbandonedExecutions);
|
||||
assertEquals(List.of(), warningsIn(logRecords),
|
||||
"expected no WARN record from a sweep that found nothing RUNNING, got: "
|
||||
+ formattedAll(logRecords));
|
||||
@@ -298,25 +282,6 @@ class StepFunctionsServicePersistenceTest {
|
||||
verifyNoInteractions(aslExecutor, mockLoader, regionResolver);
|
||||
}
|
||||
|
||||
private static Handler collectorInto(List<LogRecord> records) {
|
||||
Handler collector = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
collector.setLevel(Level.ALL);
|
||||
return collector;
|
||||
}
|
||||
|
||||
private static List<LogRecord> warningsIn(List<LogRecord> records) {
|
||||
// The level is compared by value: under JBoss LogManager the record carries its own WARN
|
||||
// instance, not the java.util.logging constant.
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
@@ -26,7 +27,8 @@ public final class LogCapture {
|
||||
}
|
||||
|
||||
public static List<LogRecord> capture(Class<?> source, Runnable action) {
|
||||
List<LogRecord> records = new ArrayList<>();
|
||||
// A record logged from another thread while the action runs is collected too.
|
||||
List<LogRecord> records = new CopyOnWriteArrayList<>();
|
||||
withHandler(source, records::add, () -> {
|
||||
action.run();
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user