cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sn...@apache.org
Subject cassandra git commit: Ninja fix CASSANDRA-9029
Date Mon, 06 Apr 2015 21:00:55 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 70b51ff32 -> b449126fe


Ninja fix CASSANDRA-9029


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/b449126f
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/b449126f
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/b449126f

Branch: refs/heads/trunk
Commit: b449126fee2fd6a9a927846b2b2a4f89222b78b7
Parents: 70b51ff
Author: Ariel Weisberg <ariel@weisberg.ws>
Authored: Mon Apr 6 23:00:00 2015 +0200
Committer: Robert Stupp <snazy@snazy.de>
Committed: Mon Apr 6 23:00:00 2015 +0200

----------------------------------------------------------------------
 .../apache/cassandra/utils/NoSpamLogger.java    |  35 +++---
 .../cassandra/utils/NoSpamLoggerTest.java       | 115 +++++++++++++++----
 2 files changed, 109 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/b449126f/src/java/org/apache/cassandra/utils/NoSpamLogger.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/NoSpamLogger.java b/src/java/org/apache/cassandra/utils/NoSpamLogger.java
index 9f5d5ce..3cc8b5e 100644
--- a/src/java/org/apache/cassandra/utils/NoSpamLogger.java
+++ b/src/java/org/apache/cassandra/utils/NoSpamLogger.java
@@ -103,32 +103,32 @@ public class NoSpamLogger
 
         public void info(long nowNanos, Object... objects)
         {
-            log(Level.INFO, nowNanos, objects);
+            NoSpamLogStatement.this.log(Level.INFO, nowNanos, objects);
         }
 
         public void info(Object... objects)
         {
-            info(CLOCK.nanoTime(), objects);
+            NoSpamLogStatement.this.info(CLOCK.nanoTime(), objects);
         }
 
         public void warn(long nowNanos, Object... objects)
         {
-            log(Level.WARN, nowNanos, objects);
+            NoSpamLogStatement.this.log(Level.WARN, nowNanos, objects);
         }
 
-        public void warn(String s, Object... objects)
+        public void warn(Object... objects)
         {
-            warn(CLOCK.nanoTime(), s, objects);
+            NoSpamLogStatement.this.warn(CLOCK.nanoTime(), objects);
         }
 
         public void error(long nowNanos, Object... objects)
         {
-            log(Level.ERROR, nowNanos, objects);
+            NoSpamLogStatement.this.log(Level.ERROR, nowNanos, objects);
         }
 
         public void error(Object... objects)
         {
-            error(CLOCK.nanoTime(), objects);
+            NoSpamLogStatement.this.error(CLOCK.nanoTime(), objects);
         }
     }
 
@@ -165,7 +165,8 @@ public class NoSpamLogger
         statement.log(level, nowNanos, objects);
     }
 
-    public static NoSpamLogStatement getStatement(Logger logger, String message, long minInterval,
TimeUnit unit) {
+    public static NoSpamLogStatement getStatement(Logger logger, String message, long minInterval,
TimeUnit unit)
+    {
         NoSpamLogger wrapped = getLogger(logger, minInterval, unit);
         return wrapped.getStatement(message);
     }
@@ -182,45 +183,45 @@ public class NoSpamLogger
 
     public void info(long nowNanos, String s, Object... objects)
     {
-        log( Level.INFO, s, nowNanos, objects);
+        NoSpamLogger.this.log( Level.INFO, s, nowNanos, objects);
     }
 
     public void info(String s, Object... objects)
     {
-        info(CLOCK.nanoTime(), s, objects);
+        NoSpamLogger.this.info(CLOCK.nanoTime(), s, objects);
     }
 
     public void warn(long nowNanos, String s, Object... objects)
     {
-        log( Level.WARN, s, nowNanos, objects);
+        NoSpamLogger.this.log( Level.WARN, s, nowNanos, objects);
     }
 
     public void warn(String s, Object... objects)
     {
-        warn(CLOCK.nanoTime(), s, objects);
+        NoSpamLogger.this.warn(CLOCK.nanoTime(), s, objects);
     }
 
     public void error(long nowNanos, String s, Object... objects)
     {
-        log( Level.ERROR, s, nowNanos, objects);
+        NoSpamLogger.this.log( Level.ERROR, s, nowNanos, objects);
     }
 
     public void error(String s, Object... objects)
     {
-        error(CLOCK.nanoTime(), s, objects);
+        NoSpamLogger.this.error(CLOCK.nanoTime(), s, objects);
     }
 
     public void log(Level l, String s, long nowNanos, Object... objects) {
-        getStatement(s, minIntervalNanos).log(l, nowNanos, objects);
+        NoSpamLogger.this.getStatement(s, minIntervalNanos).log(l, nowNanos, objects);
     }
 
     public NoSpamLogStatement getStatement(String s)
     {
-        return getStatement(s, minIntervalNanos);
+        return NoSpamLogger.this.getStatement(s, minIntervalNanos);
     }
 
     public NoSpamLogStatement getStatement(String s, long minInterval, TimeUnit unit) {
-        return getStatement(s, unit.toNanos(minInterval));
+        return NoSpamLogger.this.getStatement(s, unit.toNanos(minInterval));
     }
 
     public NoSpamLogStatement getStatement(String s, long minIntervalNanos)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/b449126f/test/unit/org/apache/cassandra/utils/NoSpamLoggerTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/utils/NoSpamLoggerTest.java b/test/unit/org/apache/cassandra/utils/NoSpamLoggerTest.java
index 3ad7ab8..ca1d6d3 100644
--- a/test/unit/org/apache/cassandra/utils/NoSpamLoggerTest.java
+++ b/test/unit/org/apache/cassandra/utils/NoSpamLoggerTest.java
@@ -20,10 +20,10 @@ package org.apache.cassandra.utils;
 
 import static org.junit.Assert.*;
 
-import java.util.ArrayList;
+import java.util.ArrayDeque;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
+import java.util.Queue;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.cassandra.utils.NoSpamLogger.Level;
@@ -37,7 +37,7 @@ import org.slf4j.helpers.SubstituteLogger;
 
 public class NoSpamLoggerTest
 {
-    Map<Level, List<Pair<String, Object[]>>> logged = new HashMap<>();
+    Map<Level, Queue<Pair<String, Object[]>>> logged = new HashMap<>();
 
    Logger mock = new SubstituteLogger(null)
    {
@@ -45,19 +45,19 @@ public class NoSpamLoggerTest
        @Override
        public void info(String statement, Object... args)
        {
-           logged.get(Level.INFO).add(Pair.create(statement, args));
+           logged.get(Level.INFO).offer(Pair.create(statement, args));
        }
 
        @Override
        public void warn(String statement, Object... args)
        {
-           logged.get(Level.WARN).add(Pair.create(statement, args));
+           logged.get(Level.WARN).offer(Pair.create(statement, args));
        }
 
        @Override
        public void error(String statement, Object... args)
        {
-           logged.get(Level.ERROR).add(Pair.create(statement, args));
+           logged.get(Level.ERROR).offer(Pair.create(statement, args));
        }
 
        @Override
@@ -74,6 +74,8 @@ public class NoSpamLoggerTest
    };
 
 
+   static final String statement = "swizzle{}";
+   static final String param = "";
    static long now;
 
    @BeforeClass
@@ -92,9 +94,10 @@ public class NoSpamLoggerTest
    @Before
    public void setUp() throws Exception
    {
-       logged.put(Level.INFO, new ArrayList<Pair<String, Object[]>>());
-       logged.put(Level.WARN, new ArrayList<Pair<String, Object[]>>());
-       logged.put(Level.ERROR, new ArrayList<Pair<String, Object[]>>());
+       logged.put(Level.INFO, new ArrayDeque<Pair<String, Object[]>>());
+       logged.put(Level.WARN, new ArrayDeque<Pair<String, Object[]>>());
+       logged.put(Level.ERROR, new ArrayDeque<Pair<String, Object[]>>());
+       NoSpamLogger.clearWrappedLoggersForTest();
    }
 
    @Test
@@ -109,19 +112,18 @@ public class NoSpamLoggerTest
    {
        setUp();
        now = 5;
-       NoSpamLogger.clearWrappedLoggersForTest();
 
-       NoSpamLogger.log( mock, l, 5,  TimeUnit.NANOSECONDS, "swizzle{}", "a");
+       NoSpamLogger.log( mock, l, 5,  TimeUnit.NANOSECONDS, statement, param);
 
        assertEquals(1, logged.get(l).size());
 
-       NoSpamLogger.log( mock, l, 5,  TimeUnit.NANOSECONDS, "swizzle{}", "a");
+       NoSpamLogger.log( mock, l, 5,  TimeUnit.NANOSECONDS, statement, param);
 
        assertEquals(1, logged.get(l).size());
 
        now += 5;
 
-       NoSpamLogger.log( mock, l, 5,  TimeUnit.NANOSECONDS, "swizzle{}", "a");
+       NoSpamLogger.log( mock, l, 5,  TimeUnit.NANOSECONDS, statement, param);
 
        assertEquals(2, logged.get(l).size());
    }
@@ -139,20 +141,20 @@ public class NoSpamLoggerTest
        now = 5;
        NoSpamLogger logger = NoSpamLogger.getLogger( mock, 5, TimeUnit.NANOSECONDS);
 
-       logger.info("swizzle{}", "a");
-       logger.info("swizzle{}", "a");
-       logger.warn("swizzle{}", "a");
-       logger.error("swizzle{}", "a");
+       logger.info(statement, param);
+       logger.info(statement, param);
+       logger.warn(statement, param);
+       logger.error(statement, param);
 
        assertLoggedSizes(1, 0, 0);
 
        NoSpamLogStatement statement = logger.getStatement("swizzle2{}", 10, TimeUnit.NANOSECONDS);
-       statement.warn("a");
+       statement.warn(param);
        //now is 5 so it won't log
        assertLoggedSizes(1, 0, 0);
 
        now = 10;
-       statement.warn("a");
+       statement.warn(param);
        assertLoggedSizes(1, 1, 0);
 
    }
@@ -160,15 +162,80 @@ public class NoSpamLoggerTest
    @Test
    public void testNoSpamLoggerStatementDirect() throws Exception
    {
-       NoSpamLogger.NoSpamLogStatement statement = NoSpamLogger.getStatement( mock, "swizzle{}",
5, TimeUnit.NANOSECONDS);
+       NoSpamLogger.NoSpamLogStatement nospam = NoSpamLogger.getStatement( mock, statement,
5, TimeUnit.NANOSECONDS);
 
        now = 5;
 
-       statement.info("swizzle{}", "a");
-       statement.info("swizzle{}", "a");
-       statement.warn("swizzle{}", "a");
-       statement.error("swizzle{}", "a");
+       nospam.info(statement, param);
+       nospam.info(statement, param);
+       nospam.warn(statement, param);
+       nospam.error(statement, param);
 
        assertLoggedSizes(1, 0, 0);
    }
+
+   private void checkMock(Level l)
+   {
+       Pair<String, Object[]> p = logged.get(l).poll();
+       assertNotNull(p);
+       assertEquals(statement, p.left);
+       Object objs[] = p.right;
+       assertEquals(1, objs.length);
+       assertEquals(param, objs[0]);
+       assertTrue(logged.get(l).isEmpty());
+   }
+
+   /*
+    * Make sure that what is passed to the underlying logger is the correct set of objects
+    */
+   @Test
+   public void testLoggedResult() throws Exception
+   {
+       NoSpamLogger.log( mock, Level.INFO, 5,  TimeUnit.NANOSECONDS, statement, param);
+       checkMock(Level.INFO);
+
+       now = 10;
+
+       NoSpamLogger.log( mock, Level.WARN, 5,  TimeUnit.NANOSECONDS, statement, param);
+       checkMock(Level.WARN);
+
+       now = 15;
+
+       NoSpamLogger.log( mock, Level.ERROR, 5,  TimeUnit.NANOSECONDS, statement, param);
+       checkMock(Level.ERROR);
+
+       now = 20;
+
+       NoSpamLogger logger = NoSpamLogger.getLogger(mock, 5, TimeUnit.NANOSECONDS);
+
+       logger.info(statement, param);
+       checkMock(Level.INFO);
+
+       now = 25;
+
+       logger.warn(statement, param);
+       checkMock(Level.WARN);
+
+       now = 30;
+
+       logger.error(statement, param);
+       checkMock(Level.ERROR);
+
+       NoSpamLogger.NoSpamLogStatement nospamStatement = logger.getStatement(statement);
+
+       now = 35;
+
+       nospamStatement.info(param);
+       checkMock(Level.INFO);
+
+       now = 40;
+
+       nospamStatement.warn(param);
+       checkMock(Level.WARN);
+
+       now = 45;
+
+       nospamStatement.error(param);
+       checkMock(Level.ERROR);
+   }
 }


Mime
View raw message