Skip to content

Commit

Permalink
Merge pull request #28299 from vespa-engine/jonmv/shut-down-logger-an…
Browse files Browse the repository at this point in the history
…d-fix-unit-test

Shut down loggers on deconstruct, and do this in unit test instead of…
  • Loading branch information
Harald Musum authored Aug 31, 2023
2 parents 0978f02 + eb479a5 commit 98a1f7e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public boolean send(LoggerEntry entry) {
return true;
}

// TODO Call from a component or make this class a component
public void shutdown() {
@Override
public void deconstruct() {
super.deconstruct();
executorService.shutdown();
try {
if ( ! executorService.awaitTermination(10, TimeUnit.SECONDS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.yahoo.search.logging;

import com.yahoo.component.AbstractComponent;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
Expand All @@ -10,7 +12,7 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

abstract class AbstractThreadedLogger implements Logger {
abstract class AbstractThreadedLogger extends AbstractComponent implements Logger {

private final static java.util.logging.Logger log = java.util.logging.Logger.getLogger(AbstractThreadedLogger.class.getName());

Expand Down Expand Up @@ -51,10 +53,15 @@ protected void dequeue(LoggerEntry entry) {
}

/**
* Actually transports the entry to it's destination
* Actually transports the entry to its destination
*/
public abstract boolean transport(LoggerEntry entry);

/** Synchronously shuts down and waits for enqueued entries to be sent. */
@Override
public void deconstruct() {
executor.close();
}

private static class WorkerThread extends Thread {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,11 @@ public void testLocalDiskLogger() throws InterruptedException, IOException {
.blob("my entry blob content".getBytes())
.track("my-track")
.send();
waitForFile(logFile);
logger.deconstruct();

String test = IOUtils.readAll(new FileReader(logFile));
assertTrue(test.contains(Base64.getEncoder().encodeToString("my entry blob content".getBytes())));
assertTrue(test.contains("my-track"));
}

private void waitForFile(File file) throws InterruptedException {
int waitFor = 10;
while ( ! file.exists() && --waitFor > 0) {
Thread.sleep(10);
}
if ( ! file.exists()) {
fail("Local disk logger file was not created");
}
}

}

0 comments on commit 98a1f7e

Please sign in to comment.