james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [2/2] james-project git commit: MPT-36 Introduce TIMER mpt scripts command
Date Mon, 27 Feb 2017 13:20:11 GMT
MPT-36 Introduce TIMER mpt scripts command


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/44728d3f
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/44728d3f
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/44728d3f

Branch: refs/heads/master
Commit: 44728d3fcd1b6948caa30b6d0f2ad337bd1f218a
Parents: 02d415c
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Wed Feb 22 15:36:04 2017 +0100
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Mon Feb 27 11:51:44 2017 +0100

----------------------------------------------------------------------
 .../protocol/FileProtocolSessionBuilder.java    |  9 +++
 .../james/mpt/protocol/ProtocolSession.java     | 82 ++++++++++++++++++++
 .../mpt/protocol/ProtocolSessionBuilder.java    |  2 +
 .../james/imap/scripts/FetchEnvelope.test       | 13 +++-
 4 files changed, 105 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/44728d3f/mpt/core/src/main/java/org/apache/james/mpt/protocol/FileProtocolSessionBuilder.java
----------------------------------------------------------------------
diff --git a/mpt/core/src/main/java/org/apache/james/mpt/protocol/FileProtocolSessionBuilder.java
b/mpt/core/src/main/java/org/apache/james/mpt/protocol/FileProtocolSessionBuilder.java
index 3648a11..8488db5 100644
--- a/mpt/core/src/main/java/org/apache/james/mpt/protocol/FileProtocolSessionBuilder.java
+++ b/mpt/core/src/main/java/org/apache/james/mpt/protocol/FileProtocolSessionBuilder.java
@@ -26,6 +26,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.james.mpt.protocol.ProtocolSession.TimerCommand;
 
 /**
  * A builder which generates a ProtocolSession from a test file.
@@ -41,6 +42,9 @@ public class FileProtocolSessionBuilder extends ProtocolSessionBuilder {
     public static final String WARN = "WARN";
     public static final String ERR = "ERR";
 
+    private static final int TIMER_COMMAND_START = TIMER.length() + 1;
+    private static final int TIMER_COMMAND_END = TIMER_COMMAND_START + 5;
+
     /**
      * Builds a ProtocolSession by reading lines from the test file with the
      * supplied name.
@@ -177,6 +181,11 @@ public class FileProtocolSessionBuilder extends ProtocolSessionBuilder
{
                     }
                     sessionNumber = Integer.parseInt(number);
                 }
+                else if (next.startsWith(TIMER)) {
+                    TimerCommand timerCommand = TimerCommand.from(next.substring(TIMER_COMMAND_START,
TIMER_COMMAND_END));
+                    String timerName = next.substring(TIMER_COMMAND_END + 1);
+                    session.TIMER(timerCommand, timerName);
+                }
                 else {
                     String prefix = next;
                     if (next.length() > 3) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/44728d3f/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSession.java
----------------------------------------------------------------------
diff --git a/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSession.java b/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSession.java
index 5045acb..bd26549 100644
--- a/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSession.java
+++ b/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSession.java
@@ -21,8 +21,11 @@ package org.apache.james.mpt.protocol;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
 
 import org.apache.james.mpt.api.ProtocolInteractor;
@@ -30,6 +33,8 @@ import org.apache.james.mpt.api.Session;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Stopwatch;
+
 /**
  * A protocol session which can be run against a reader and writer, which checks
  * the server response against the expected values. TODO make ProtocolSession
@@ -58,6 +63,8 @@ public class ProtocolSession implements ProtocolInteractor {
 
     private boolean continueAfterFailure = false;
 
+    private Map<String, Stopwatch> timers = new HashMap<String, Stopwatch>();
+
     public final boolean isContinueAfterFailure() {
         return continueAfterFailure;
     }
@@ -207,6 +214,10 @@ public class ProtocolSession implements ProtocolInteractor {
         testElements.add(new ReinitElement(sessionNumber));
     }
 
+    public void TIMER(TimerCommand timerCommand, String timerName) {
+        testElements.add(new TimerElement(timerCommand, timerName));
+    }
+
     /**
      * A client request, which write the specified message to a Writer.
      */
@@ -520,6 +531,77 @@ public class ProtocolSession implements ProtocolInteractor {
         }
     }
 
+    protected enum TimerCommand {
+        START, PRINT, RESET;
+
+        public static TimerCommand from(String value) throws InvalidServerResponseException
{
+            if (value.equalsIgnoreCase("start")) {
+                return START;
+            }
+            if (value.equalsIgnoreCase("print")) {
+                return PRINT;
+            }
+            if (value.equalsIgnoreCase("reset")) {
+                return RESET;
+            }
+            throw new InvalidServerResponseException("Invalid TIMER command '" + value +
"'");
+        }
+    }
+
+    private class TimerElement implements ProtocolElement {
+
+        private TimerCommand timerCommand;
+        private String timerName;
+
+        public TimerElement(TimerCommand timerCommand, String timerName) {
+            this.timerCommand = timerCommand;
+            this.timerName = timerName;
+        }
+
+        @Override
+        public void testProtocol(Session[] sessions, boolean continueAfterFailure) throws
Exception {
+            switch(timerCommand) {
+            case START:
+                start();
+                break;
+            case PRINT:
+                print();
+                break;
+            case RESET:
+                reset();
+                break;
+            default:
+                throw new InvalidServerResponseException("Invalid TIMER command '" + timerCommand
+ "' for timer name: '" + timerName + "'");
+            }
+        }
+
+        private void start() {
+            timers.put(timerName, Stopwatch.createStarted());
+        }
+
+        private void print() throws InvalidServerResponseException {
+            Stopwatch stopwatch = timers.get(timerName);
+            if (stopwatch == null) {
+                throw new InvalidServerResponseException("TIMER '" + timerName + "' undefined");
+            }
+            LOGGER.info("Time spent in '" + timerName + "': " + stopwatch.elapsed(TimeUnit.MILLISECONDS)
+ " ms");
+        }
+
+        private void reset() throws InvalidServerResponseException {
+            Stopwatch stopwatch = timers.get(timerName);
+            if (stopwatch == null) {
+                throw new InvalidServerResponseException("TIMER '" + timerName + "' undefined");
+            }
+            stopwatch.reset();
+            stopwatch.start();
+        }
+
+        @Override
+        public boolean isClient() {
+            return false;
+        }
+    }
+
     /**
      * Allow you to wait a given time at a given point of the test script
      */

http://git-wip-us.apache.org/repos/asf/james-project/blob/44728d3f/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSessionBuilder.java
----------------------------------------------------------------------
diff --git a/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSessionBuilder.java
b/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSessionBuilder.java
index 657c64a..ccf50ed 100644
--- a/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSessionBuilder.java
+++ b/mpt/core/src/main/java/org/apache/james/mpt/protocol/ProtocolSessionBuilder.java
@@ -59,6 +59,8 @@ public class ProtocolSessionBuilder {
 
     public static final String REINIT = "REINIT";
 
+    public static final String TIMER = "TIMER";
+
     private final Properties variables;
     
     public ProtocolSessionBuilder() {

http://git-wip-us.apache.org/repos/asf/james-project/blob/44728d3f/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/FetchEnvelope.test
----------------------------------------------------------------------
diff --git a/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/FetchEnvelope.test
b/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/FetchEnvelope.test
index 481f157..8f7067b 100644
--- a/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/FetchEnvelope.test
+++ b/mpt/impl/imap-mailbox/core/src/main/resources/org/apache/james/imap/scripts/FetchEnvelope.test
@@ -17,6 +17,8 @@
 # under the License.                                           #
 ################################################################
 
+TIMER start append
+
 C: A007 APPEND selected {596+}
 C: Date: Mon, 10 Feb 1994 21:52:25 -0800 (PST)
 C: From: mooch@owatagu.siam.edu
@@ -37,10 +39,19 @@ S: \* 5 EXISTS
 S: \* 5 RECENT
 S: A007 OK (\[.+\] )?APPEND completed.
 
+TIMER print append
+
+TIMER start fetch
+
 C: f1 FETCH 1 (ENVELOPE)
 S: \* 1 FETCH \(ENVELOPE \(\"Mon, 7 Feb 1994 21:52:25 -0800 \(PST\)\" \"Test 01\" \(\(\"Fred
Foobar\" NIL \"foobar\" \"Blurdybloop.COM\"\)\) \(\(\"Fred Foobar\" NIL \"foobar\" \"Blurdybloop.COM\"\)\)
\(\(\"Fred Foobar\" NIL \"foobar\" \"Blurdybloop.COM\"\)\) \(\(NIL NIL \"mooch\" \"owatagu.siam.edu\"\)\)
NIL NIL NIL \"<B27397-0100000@Blurdybloop.COM>\"\)\)
 S: f1 OK FETCH completed.
 
+TIMER print fetch
+TIMER reset fetch
+
 C: f2 FETCH 5 (ENVELOPE)
 S: \* 5 FETCH \(ENVELOPE \(\"Mon, 10 Feb 1994 21:52:25 -0800 \(PST\)\" \"Re: Test 05\" \(\(NIL
NIL \"mooch\" \"owatagu.siam.edu\"\)\) \(\(NIL NIL \"mooch\" \"owatagu.siam.edu\"\)\(\"Boss\"
NIL \"boss\" \"owatagu.siam.edu\"\)\) \(\(\"Bin\" NIL \"dev.null\" \"owatagu.siam.edu\"\)\(\"Thin
Air\" NIL \"air\" \"owatagu.siam.edu\"\)\) \(\(\"Fred Foobar\" NIL \"foobar\" \"Blurdybloop.COM\"\)\(\"Sue\"
NIL \"sue.foo\" \"Blurdybloop.COM\"\)\) \(\(\"Moo\" NIL \"moo\" \"example.COM\"\)\(\"Hugh\"
NIL \"hugh\" \"example.COM\"\)\) \(\(\"Secret\" NIL \"spy\" \"owatagu.siam.ed\"\)\(\"Audit\"
NIL \"audit\" \"owatagu.siam.ed\"\)\) \"<B27397-0100000@Blurdybloop.COM>\" \"<12345677890-mooch@owatagu.siam.edu>\"\)\)
-S: f2 OK FETCH completed.
\ No newline at end of file
+S: f2 OK FETCH completed.
+
+TIMER print fetch
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message