From 6e304b30fb66119147034ee5fa54fa48849714ef Mon Sep 17 00:00:00 2001
From: Rob Adams <rob.adams@bigswitch.com>
Date: Wed, 8 Feb 2012 11:05:37 -0800
Subject: [PATCH] Greatly shorten staticflowentrypusher test time

---
 .../StaticFlowEntryPusher.java                | 18 +++++-----
 .../StaticFlowEntryPusherTest.java            | 35 +++++++++++--------
 2 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusher.java b/src/main/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusher.java
index 585c948ad..b1ca17b46 100644
--- a/src/main/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusher.java
+++ b/src/main/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusher.java
@@ -68,7 +68,7 @@ public class StaticFlowEntryPusher implements IStaticFlowEntryPusher, IOFSwitchL
     protected ArrayList<String> flowmodList;
     protected ArrayList<IOFSwitch> activeSwitches;
     protected HashMap<Long, HashMap<String, OFFlowMod>> flowmods;
-    protected int pushEntriesFrequency = 25; // seconds
+    protected int pushEntriesFrequency = 25000; // milliseconds
     protected Runnable pushEntriesTimer;
 
     public StaticFlowEntryPusher() {
@@ -92,17 +92,17 @@ public class StaticFlowEntryPusher implements IStaticFlowEntryPusher, IOFSwitchL
     
     /**
      * Gets the static flow entry push interval
-     * @return The push interval in seconds
+     * @return The push interval in milliseconds
      */
-    public int getFlowPushTimeSeconds() {
+    public int getFlowPushTime() {
         return pushEntriesFrequency;
     }
     
     /**
      * Sets the static flow entry push interval
-     * @param s The interval in seconds to set
+     * @param s The interval in milliseconds to set
      */
-    public void setFlowPushTimeSeconds(int s) {
+    public void setFlowPushTime(int s) {
         pushEntriesFrequency = s;
     }
 
@@ -615,12 +615,14 @@ public class StaticFlowEntryPusher implements IStaticFlowEntryPusher, IOFSwitchL
                     ScheduledExecutorService ses = 
                         floodlightProvider.getScheduledExecutor();
                     ses.schedule(this, 
-                                pushEntriesFrequency, TimeUnit.SECONDS);
+                                pushEntriesFrequency, TimeUnit.MILLISECONDS);
                 }
             }
         };
-        // Initially push entries in 1 second
-        floodlightProvider.getScheduledExecutor().schedule(pushEntriesTimer, 1, TimeUnit.SECONDS);
+        // Initially push entries 
+        floodlightProvider.getScheduledExecutor().schedule(pushEntriesTimer, 
+                                                           pushEntriesFrequency, 
+                                                           TimeUnit.MILLISECONDS);
     }
 
     /**
diff --git a/src/test/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusherTest.java b/src/test/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusherTest.java
index 74f02e0e9..d1316f47d 100644
--- a/src/test/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusherTest.java
+++ b/src/test/java/net/floodlightcontroller/staticflowentry/StaticFlowEntryPusherTest.java
@@ -50,12 +50,16 @@ public class StaticFlowEntryPusherTest extends FloodlightTestCase {
     
     @Test
     public void testAddAndRemoveEntries() throws Exception {
-        StaticFlowEntryPusher staticFlowEntryPusher = new StaticFlowEntryPusher();
+        StaticFlowEntryPusher staticFlowEntryPusher = 
+                new StaticFlowEntryPusher();
+        staticFlowEntryPusher.setFlowPushTime(200);
         IOFSwitch mockSwitch = createMock(IOFSwitch.class);
         long dpid = HexString.toLong(TestSwitch1DPID);
         Capture<OFMessage> writeCapture = new Capture<OFMessage>(CaptureType.ALL);
-        Capture<FloodlightContext> contextCapture = new Capture<FloodlightContext>(CaptureType.ALL);
-        Capture<List<OFMessage>> writeCaptureList = new Capture<List<OFMessage>>(CaptureType.ALL);
+        Capture<FloodlightContext> contextCapture = 
+                new Capture<FloodlightContext>(CaptureType.ALL);
+        Capture<List<OFMessage>> writeCaptureList = 
+                new Capture<List<OFMessage>>(CaptureType.ALL);
         
         mockSwitch.write(capture(writeCapture), capture(contextCapture));
         expectLastCall().anyTimes();
@@ -67,8 +71,6 @@ public class StaticFlowEntryPusherTest extends FloodlightTestCase {
         switchMap.put(dpid, mockSwitch);
         mockFloodlightProvider.setSwitches(switchMap);
         staticFlowEntryPusher.setFloodlightProvider(mockFloodlightProvider);
-        staticFlowEntryPusher.setFlowPushTimeSeconds(2); // speed up push timer for faster testing
-        long timeSfpStart = System.currentTimeMillis();
         staticFlowEntryPusher.startUp();
         
         // if someone calls getId(), return this dpid instead
@@ -86,21 +88,26 @@ public class StaticFlowEntryPusherTest extends FloodlightTestCase {
         
         // Verify that the switch has gotten some flow_mods
         assertEquals(true, writeCapture.getValues().size() == 2);
-        
-        long timeNow = System.currentTimeMillis();
-        // Sleep just long enough for static flow pusher to re-push entires
-        long timeNeededToSleep = (staticFlowEntryPusher.getFlowPushTimeSeconds() * 1000) - (timeNow - timeSfpStart);
-        if (timeNeededToSleep > 0)
-            Thread.sleep(timeNeededToSleep);
+
+        int count = 5;
+        while (count >= 0) {
+            Thread.sleep(staticFlowEntryPusher.getFlowPushTime());
+
+            if (writeCapture.getValues().size() >= 4)
+                break;
+
+            count -= 1;
+        }
+        int newsize = writeCapture.getValues().size();
         // Make sure the entries were pushed again
-        assertEquals(true, writeCapture.getValues().size() == 4);
+        assertTrue(newsize >= 4);
         
         // Remove entries
         staticFlowEntryPusher.removeEntry(flowMod1);
         staticFlowEntryPusher.removeEntry(flowMod2);
         
-        Thread.sleep(staticFlowEntryPusher.getFlowPushTimeSeconds() * 1000);
+        Thread.sleep(staticFlowEntryPusher.getFlowPushTime());
         // Make sure the entries were NOT pushed again
-        assertEquals(true, writeCapture.getValues().size() == 4);
+        assertEquals(newsize, writeCapture.getValues().size());
     }
 }
-- 
GitLab