aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/research/ResearchLog.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/research/ResearchLog.java')
-rw-r--r--java/src/com/android/inputmethod/research/ResearchLog.java58
1 files changed, 41 insertions, 17 deletions
diff --git a/java/src/com/android/inputmethod/research/ResearchLog.java b/java/src/com/android/inputmethod/research/ResearchLog.java
index 9016e23b3..f7c5fd50e 100644
--- a/java/src/com/android/inputmethod/research/ResearchLog.java
+++ b/java/src/com/android/inputmethod/research/ResearchLog.java
@@ -20,11 +20,11 @@ import android.content.Context;
import android.util.JsonWriter;
import android.util.Log;
+import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.define.ProductionFlag;
import java.io.BufferedWriter;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -54,7 +54,6 @@ public class ResearchLog {
private static final String TAG = ResearchLog.class.getSimpleName();
private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
private static final long FLUSH_DELAY_IN_MS = 1000 * 5;
- private static final int ABORT_TIMEOUT_IN_MS = 1000 * 4;
/* package */ final ScheduledExecutorService mExecutor;
/* package */ final File mFile;
@@ -99,8 +98,10 @@ public class ResearchLog {
* output.
*
* See class comment for details about {@code JsonWriter} construction.
+ *
+ * @param onClosed run after the close() operation has completed asynchronously
*/
- public synchronized void close(final Runnable onClosed) {
+ private synchronized void close(final Runnable onClosed) {
mExecutor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
@@ -131,15 +132,24 @@ public class ResearchLog {
mExecutor.shutdown();
}
- private boolean mIsAbortSuccessful;
+ /**
+ * Block until the research log has shut down and spooled out all output or {@code timeout}
+ * occurs.
+ *
+ * @param timeout time to wait for close in milliseconds
+ */
+ public void blockingClose(final long timeout) {
+ close(null);
+ awaitTermination(timeout, TimeUnit.MILLISECONDS);
+ }
/**
- * Waits for publication requests to finish, closes the {@link JsonWriter}, but then deletes the
- * backing file used for output.
+ * Waits for publication requests to finish, closes the JsonWriter, but then deletes the backing
+ * output file.
*
- * See class comment for details about {@code JsonWriter} construction.
+ * @param onAbort run after the abort() operation has completed asynchronously
*/
- public synchronized void abort() {
+ private synchronized void abort(final Runnable onAbort) {
mExecutor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
@@ -151,7 +161,10 @@ public class ResearchLog {
}
} finally {
if (mFile != null) {
- mIsAbortSuccessful = mFile.delete();
+ mFile.delete();
+ }
+ if (onAbort != null) {
+ onAbort.run();
}
}
return null;
@@ -161,14 +174,25 @@ public class ResearchLog {
mExecutor.shutdown();
}
- public boolean blockingAbort() throws InterruptedException {
- abort();
- mExecutor.awaitTermination(ABORT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS);
- return mIsAbortSuccessful;
+ /**
+ * Block until the research log has aborted or {@code timeout} occurs.
+ *
+ * @param timeout time to wait for close in milliseconds
+ */
+ public void blockingAbort(final long timeout) {
+ abort(null);
+ awaitTermination(timeout, TimeUnit.MILLISECONDS);
}
- public void awaitTermination(int delay, TimeUnit timeUnit) throws InterruptedException {
- mExecutor.awaitTermination(delay, timeUnit);
+ @UsedForTesting
+ public void awaitTermination(final long delay, final TimeUnit timeUnit) {
+ try {
+ if (!mExecutor.awaitTermination(delay, timeUnit)) {
+ Log.e(TAG, "ResearchLog executor timed out while awaiting terminaion");
+ }
+ } catch (final InterruptedException e) {
+ Log.e(TAG, "ResearchLog executor interrupted while awaiting terminaion", e);
+ }
}
/* package */ synchronized void flush() {
@@ -214,10 +238,10 @@ public class ResearchLog {
return null;
}
});
- } catch (RejectedExecutionException e) {
+ } catch (final RejectedExecutionException e) {
// TODO: Add code to record loss of data, and report.
if (DEBUG) {
- Log.d(TAG, "ResearchLog.publish() rejecting scheduled execution");
+ Log.d(TAG, "ResearchLog.publish() rejecting scheduled execution", e);
}
}
}