[logback-dev] svn commit: r2089 - logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Dec 18 22:13:36 CET 2008


Author: ceki
Date: Thu Dec 18 22:13:35 2008
New Revision: 2089

Added:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/AppenderTrackerTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/PackageTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/ScenarioBasedAppenderTrackerTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/SiftingAppenderTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/Simulator.java

Log:


Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/AppenderTrackerTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/AppenderTrackerTest.java	Thu Dec 18 22:13:35 2008
@@ -0,0 +1,63 @@
+package ch.qos.logback.core.sift;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import ch.qos.logback.classic.sift.AppenderTracker;
+import ch.qos.logback.classic.sift.AppenderTrackerImpl;
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.read.ListAppender;
+
+public class AppenderTrackerTest {
+
+  
+  Context context = new ContextBase();
+  AppenderTracker<Object, String> appenderTracker = new AppenderTrackerImpl<Object, String>();
+  ListAppender<Object> la = new ListAppender<Object>();
+  
+  @Before
+  public void setUp() {
+    la.setContext(context);
+    la.start();
+  }
+
+  
+  @Test
+  public void empty0() {
+    long now = 3000;
+    appenderTracker.stopStaleAppenders(now);
+    assertEquals(0, appenderTracker.keyList().size());
+  }
+  
+  @Test
+  public void empty1() {
+    long now = 3000;
+    assertNull(appenderTracker.get("a", now++));
+    now += AppenderTrackerImpl.THRESHOLD+1000;
+    appenderTracker.stopStaleAppenders(now);
+    assertNull(appenderTracker.get("a", now++));
+  }
+  
+  @Test
+  public void smoke() {
+    assertTrue(la.isStarted());
+    long now = 3000;
+    appenderTracker.put("a", la, now);
+    assertEquals(la, appenderTracker.get("a", now++));
+    now += AppenderTrackerImpl.THRESHOLD+1000;
+    appenderTracker.stopStaleAppenders(now);
+    assertFalse(la.isStarted());
+    assertNull(appenderTracker.get("a", now++));
+  }
+  
+  @Test
+  public void scenarioBased() {
+    
+  }
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/PackageTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/PackageTest.java	Thu Dec 18 22:13:35 2008
@@ -0,0 +1,19 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.sift;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+ at RunWith(Suite.class)
+ at SuiteClasses({SiftingAppenderTest.class})
+public class PackageTest  {
+}
\ No newline at end of file

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/ScenarioBasedAppenderTrackerTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/ScenarioBasedAppenderTrackerTest.java	Thu Dec 18 22:13:35 2008
@@ -0,0 +1,55 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.sift;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+import ch.qos.logback.classic.sift.AppenderTracker;
+
+public class ScenarioBasedAppenderTrackerTest {
+
+  Simulator simulator;
+
+  void verify() {
+    AppenderTracker at = simulator.appenderTracker;
+    AppenderTracker t_at = simulator.t_appenderTracker;
+    //List<String> resultKeys = at.keyList();
+    //List<String> witnessKeys = t_at.keyList();
+    assertEquals(t_at.keyList(), at.keyList());
+  }
+
+  @Test
+  public void shortTest() {
+    simulator = new Simulator(20, AppenderTracker.THRESHOLD / 2);
+    simulator.buildScenario(200);
+    simulator.simulate();
+    verify();
+  }
+
+  @Test
+  public void mediumTest() {
+    simulator = new Simulator(100, AppenderTracker.THRESHOLD / 2);
+    simulator.buildScenario(20000);
+    simulator.simulate();
+    verify();
+  }
+
+  @Test
+  @Ignore
+  public void longetTest() {
+    simulator = new Simulator(100, AppenderTracker.THRESHOLD / 200);
+    simulator.buildScenario(2000000);
+    simulator.simulate();
+    verify();
+  }
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/SiftingAppenderTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/SiftingAppenderTest.java	Thu Dec 18 22:13:35 2008
@@ -0,0 +1,96 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.sift;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.slf4j.MDC;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.classic.sift.AppenderTracker;
+import ch.qos.logback.classic.sift.HoardingAppender;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.classic.util.TeztConstants;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.read.ListAppender;
+import ch.qos.logback.core.util.StatusPrinter;
+
+public class SiftingAppenderTest {
+
+  static String PREFIX = TeztConstants.TEST_DIR_PREFIX + "input/joran/hoard/";
+  		
+  LoggerContext loggerContext = new LoggerContext();
+  Logger logger = loggerContext.getLogger(this.getClass().getName());
+  Logger root = loggerContext.getLogger(LoggerContext.ROOT_NAME);
+
+  void configure(String file) throws JoranException {
+    JoranConfigurator jc = new JoranConfigurator();
+    jc.setContext(loggerContext);
+    jc.doConfigure(file);
+  }
+
+  @Test
+  public void unsetDefaultValueProperty() throws JoranException {
+    configure(PREFIX + "unsetDefaultValueProperty.xml");
+    logger.debug("hello");
+    HoardingAppender ha = (HoardingAppender) root.getAppender("HOARD");
+    assertFalse(ha.isStarted());
+    
+  }
+
+  @Test
+  public void smoke() throws JoranException {
+    configure(PREFIX + "smoke.xml");
+    logger.debug("smoke");
+    long timestamp = 0;
+    HoardingAppender ha = (HoardingAppender) root.getAppender("HOARD");
+    ListAppender<LoggingEvent> listAppender = (ListAppender<LoggingEvent>) ha.appenderTracker.get("smoke", timestamp);
+    
+    StatusPrinter.print(loggerContext);
+    assertNotNull(listAppender);
+    List<LoggingEvent> eventList = listAppender.list;
+    assertEquals(1, listAppender.list.size());
+    assertEquals("smoke", eventList.get(0).getMessage());
+  }
+
+  @Test
+  public void testWholeCycle() throws JoranException {
+    String mdcKey = "cycle";
+    configure(PREFIX + "completeCycle.xml");
+    MDC.put(mdcKey, "a");
+    logger.debug("smoke");
+    long timestamp = System.currentTimeMillis();
+    HoardingAppender ha = (HoardingAppender) root.getAppender("HOARD");
+    ListAppender<LoggingEvent> listAppender = (ListAppender<LoggingEvent>) ha.appenderTracker.get("a", timestamp);
+    StatusPrinter.print(loggerContext);
+    
+    assertNotNull(listAppender);
+    List<LoggingEvent> eventList = listAppender.list;
+    assertEquals(1, listAppender.list.size());
+    assertEquals("smoke", eventList.get(0).getMessage());
+
+    MDC.remove(mdcKey);
+    LoggingEvent le = new LoggingEvent("x", logger, Level.INFO, "hello", null, null);
+    le.setTimeStamp(timestamp+AppenderTracker.THRESHOLD*2);
+    ha.doAppend(le);
+    assertFalse(listAppender.isStarted());
+    assertEquals(1, ha.appenderTracker.keyList().size());
+    assertEquals("cycleDefault", ha.appenderTracker.keyList().get(0));
+    
+  }
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/Simulator.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/Simulator.java	Thu Dec 18 22:13:35 2008
@@ -0,0 +1,92 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.sift;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import ch.qos.logback.core.Appender;
+import ch.qos.logback.core.appender.NOPAppender;
+import ch.qos.logback.core.sift.tracker.AppenderTrackerTImpl;
+import ch.qos.logback.core.sift.tracker.SimulationEvent;
+
+/**
+ * Simulate use of AppenderTracker by HoardAppender.
+ * 
+ * @author ceki
+ *
+ */
+public class Simulator {
+
+  AppenderTrackerImpl<Object, String> appenderTracker = new AppenderTrackerImpl<Object, String>();
+  AppenderTrackerTImpl t_appenderTracker = new AppenderTrackerTImpl();
+
+  List<String> keySpace = new ArrayList<String>();
+  List<SimulationEvent> scenario = new ArrayList<SimulationEvent>();
+  Random randomKeyGen = new Random(100);
+
+  Random random = new Random(11234);
+
+  final int maxTimestampInc;
+  long timestamp = 30000;
+
+  Simulator(int keySpaceLen, int maxTimestampInc) {
+    this.maxTimestampInc = maxTimestampInc;
+    Map<String, String> checkMap = new HashMap<String, String>();
+    for (int i = 0; i < keySpaceLen; i++) {
+      String k = getRandomKeyStr();
+      if (checkMap.containsKey(k)) {
+        System.out.println("random key collision occured");
+        k += "" + i;
+      }
+      keySpace.add(k);
+      checkMap.put(k, k);
+    }
+
+  }
+
+  private String getRandomKeyStr() {
+    int ri = randomKeyGen.nextInt();
+    String s = String.format("%X", ri);
+    return s;
+  }
+
+  void buildScenario(int simLen) {
+    int keySpaceLen = keySpace.size();
+    for (int i = 0; i < simLen; i++) {
+      int index = random.nextInt(keySpaceLen);
+      timestamp += random.nextInt(maxTimestampInc);
+      String key = keySpace.get(index);
+      scenario.add(new SimulationEvent(key, timestamp));
+    }
+  }
+
+  public void simulate() {
+    for (SimulationEvent simeEvent : scenario) {
+      play(simeEvent, appenderTracker);
+      play(simeEvent, t_appenderTracker);
+    }
+  }
+
+  void play(SimulationEvent simulationEvent,
+      AppenderTracker<Object, String> appenderTracker) {
+    String mdcValue = simulationEvent.key;
+    long timestamp = simulationEvent.timestamp;
+    Appender<Object> appender = appenderTracker.get(mdcValue, timestamp);
+    if (appender == null) {
+      appender = new NOPAppender<Object>();
+      appenderTracker.put(mdcValue, appender, timestamp);
+    }
+    appenderTracker.stopStaleAppenders(timestamp);
+  }
+}


More information about the logback-dev mailing list