mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Add better logging for integration tests by injecting a ThreadFactory that can set thread names
This commit is contained in:
@@ -18,6 +18,8 @@ import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.test.BrambleIntegrationTest;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||
import org.briarproject.bramble.test.TestLogFormatter;
|
||||
import org.briarproject.bramble.test.TestThreadFactoryModule;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||
import org.briarproject.mailbox.lib.AbstractMailbox;
|
||||
import org.briarproject.mailbox.lib.TestMailbox;
|
||||
@@ -43,6 +45,10 @@ abstract class AbstractMailboxIntegrationTest
|
||||
|
||||
static final String URL_BASE = "http://127.0.0.1:8000";
|
||||
|
||||
AbstractMailboxIntegrationTest() {
|
||||
TestLogFormatter.use();
|
||||
}
|
||||
|
||||
private final File dir1 = new File(testDir, "alice");
|
||||
private final File dir2 = new File(testDir, "bob");
|
||||
private final SecretKey rootKey = getSecretKey();
|
||||
@@ -73,13 +79,16 @@ abstract class AbstractMailboxIntegrationTest
|
||||
mailbox.stopLifecycle(true);
|
||||
}
|
||||
|
||||
MailboxIntegrationTestComponent startTestComponent(
|
||||
private MailboxIntegrationTestComponent startTestComponent(
|
||||
File databaseDir, String name) throws Exception {
|
||||
TestThreadFactoryModule threadFactoryModule =
|
||||
new TestThreadFactoryModule(name);
|
||||
TestDatabaseConfigModule dbModule =
|
||||
new TestDatabaseConfigModule(databaseDir);
|
||||
MailboxIntegrationTestComponent component =
|
||||
DaggerMailboxIntegrationTestComponent
|
||||
.builder()
|
||||
.testThreadFactoryModule(threadFactoryModule)
|
||||
.testDatabaseConfigModule(dbModule)
|
||||
.build();
|
||||
injectEagerSingletons(component);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.LogManager;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.SimpleFormatter;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class TestLogFormatter extends SimpleFormatter {
|
||||
|
||||
private final Object lock = new Object();
|
||||
private final DateFormat dateFormat; // Locking: lock
|
||||
private final Date date; // Locking: lock
|
||||
|
||||
public static void use() {
|
||||
LogManager.getLogManager().reset();
|
||||
Logger rootLogger = LogManager.getLogManager().getLogger("");
|
||||
ConsoleHandler handler = new ConsoleHandler();
|
||||
handler.setFormatter(new TestLogFormatter());
|
||||
rootLogger.addHandler(handler);
|
||||
}
|
||||
|
||||
private TestLogFormatter() {
|
||||
synchronized (lock) {
|
||||
dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
|
||||
date = new Date();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(LogRecord rec) {
|
||||
if (rec.getThrown() == null) {
|
||||
String dateString;
|
||||
synchronized (lock) {
|
||||
date.setTime(rec.getMillis());
|
||||
dateString = dateFormat.format(date);
|
||||
}
|
||||
return String.format("%s [%s] %s %s - %s\n",
|
||||
dateString,
|
||||
Thread.currentThread().getName(),
|
||||
rec.getLevel().getName(),
|
||||
rec.getLoggerName(),
|
||||
rec.getMessage());
|
||||
} else {
|
||||
return super.format(rec);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user