beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lc...@apache.org
Subject [1/2] incubator-beam git commit: [BEAM-917] ExpectedLogs: clear saved records after each test.
Date Tue, 08 Nov 2016 01:42:37 GMT
Repository: incubator-beam
Updated Branches:
  refs/heads/master c6d9bf297 -> 339dee954


[BEAM-917] ExpectedLogs: clear saved records after each test.


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/6bf729e9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/6bf729e9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/6bf729e9

Branch: refs/heads/master
Commit: 6bf729e90c38e910138d332c994325223c220abd
Parents: c6d9bf2
Author: Pei He <peihe@google.com>
Authored: Fri Nov 4 18:45:47 2016 -0700
Committer: Luke Cwik <lcwik@google.com>
Committed: Mon Nov 7 17:36:00 2016 -0800

----------------------------------------------------------------------
 .../apache/beam/sdk/testing/ExpectedLogs.java   | 15 ++++++----
 .../beam/sdk/testing/ExpectedLogsTest.java      | 30 +++++++++++++++++++-
 2 files changed, 39 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/6bf729e9/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogs.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogs.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogs.java
index a8e3f94..3e51f34 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogs.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogs.java
@@ -268,6 +268,7 @@ public class ExpectedLogs extends ExternalResource {
   protected void after() {
     log.removeHandler(logSaver);
     log.setLevel(previousLevel);
+    logSaver.reset();
   }
 
   private final Logger log;
@@ -285,11 +286,7 @@ public class ExpectedLogs extends ExternalResource {
    */
   @ThreadSafe
   private static class LogSaver extends Handler {
-    Collection<LogRecord> logRecords = new ConcurrentLinkedDeque<>();
-
-    public Collection<LogRecord> getLogs() {
-      return logRecords;
-    }
+    private final Collection<LogRecord> logRecords = new ConcurrentLinkedDeque<>();
 
     @Override
     public void publish(LogRecord record) {
@@ -301,5 +298,13 @@ public class ExpectedLogs extends ExternalResource {
 
     @Override
     public void close() throws SecurityException {}
+
+    private Collection<LogRecord> getLogs() {
+      return logRecords;
+    }
+
+    private void reset() {
+      logRecords.clear();
+    }
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/6bf729e9/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogsTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogsTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogsTest.java
index 84d5584..1762d0d 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogsTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/ExpectedLogsTest.java
@@ -18,6 +18,7 @@
 package org.apache.beam.sdk.testing;
 
 import static org.apache.beam.sdk.testing.SystemNanoTimeSleeper.sleepMillis;
+import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -30,8 +31,10 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.runner.Description;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
+import org.junit.runners.model.Statement;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -39,7 +42,6 @@ import org.slf4j.LoggerFactory;
 @RunWith(JUnit4.class)
 public class ExpectedLogsTest {
   private static final Logger LOG = LoggerFactory.getLogger(ExpectedLogsTest.class);
-
   private Random random = new Random();
 
   @Rule public ExpectedLogs expectedLogs = ExpectedLogs.none(ExpectedLogsTest.class);
@@ -146,6 +148,32 @@ public class ExpectedLogsTest {
     }
   }
 
+  @Test
+  public void testLogsCleared() throws Throwable {
+    final String messageUnexpected = "Message prior to ExpectedLogs.";
+    final String messageExpected = "Message expected.";
+    LOG.info(messageUnexpected);
+
+    expectedLogs = ExpectedLogs.none(ExpectedLogsTest.class);
+    final boolean[] evaluateRan = new boolean[1];
+
+    expectedLogs.apply(
+        new Statement() {
+          @Override
+          public void evaluate() throws Throwable {
+            evaluateRan[0] = true;
+            expectedLogs.verifyNotLogged(messageUnexpected);
+            LOG.info(messageExpected);
+            expectedLogs.verifyInfo(messageExpected);
+          }
+        },
+        Description.EMPTY).evaluate();
+    assertTrue(evaluateRan[0]);
+    // Verify expectedLogs is cleared.
+    expectedLogs.verifyNotLogged(messageExpected);
+    expectedLogs.verifyNotLogged(messageUnexpected);
+  }
+
   // Generates a random fake error message.
   private String generateRandomString() {
     return "Fake error message: " + random.nextInt();


Mime
View raw message