Revision: 16149
Author: vladimir.ralev
Date: Mon Jan  3 04:15:09 2011
Log: Basic Fault Tolerant Timer Tests
http://code.google.com/p/mobicents/source/detail?r=16149

Added:
 /trunk/servers/cluster/timers/src/test
 /trunk/servers/cluster/timers/src/test/java
 /trunk/servers/cluster/timers/src/test/java/log4j.xml
 /trunk/servers/cluster/timers/src/test/java/org
 /trunk/servers/cluster/timers/src/test/java/org/mobicents
 /trunk/servers/cluster/timers/src/test/java/org/mobicents/cache
/trunk/servers/cluster/timers/src/test/java/org/mobicents/cache/TimerTest.java
 /trunk/servers/cluster/timers/src/test/resources
 /trunk/servers/cluster/timers/src/test/resources/log4j.xml
Modified:
/trunk/servers/cluster/cache/src/main/java/org/mobicents/cache/MobicentsCache.java
 /trunk/servers/cluster/timers/pom.xml

=======================================
--- /dev/null
+++ /trunk/servers/cluster/timers/src/test/java/log4j.xml Mon Jan 3 04:15:09 2011
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";>
+ <appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
+    <param name="file" value="logs/sip-server.log"/>
+    <param name="MaxFileSize" value="2000KB"/>
+    <!-- Keep one backup file -->
+    <param name="MaxBackupIndex" value="20"/>
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%d %p %t %l - %m%n"/>
+    </layout>
+  </appender>
+
+  <appender name="console" class="org.apache.log4j.ConsoleAppender">
+    <param name="Target" value="System.out"/>
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%d %p %t %l - %m%n"/>
+    </layout>
+  </appender>
+  <root>
+    <priority value ="DEBUG" />
+    <appender-ref ref="console" />
+  </root>
+</log4j:configuration>
=======================================
--- /dev/null
+++ /trunk/servers/cluster/timers/src/test/java/org/mobicents/cache/TimerTest.java Mon Jan 3 04:15:09 2011
@@ -0,0 +1,157 @@
+package org.mobicents.cache;
+
+import java.io.Serializable;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.TreeCacheViewMBean;
+import org.mobicents.cluster.DefaultMobicentsCluster;
+import org.mobicents.cluster.election.DefaultClusterElector;
+import org.mobicents.timers.FaultTolerantScheduler;
+import org.mobicents.timers.PeriodicScheduleStrategy;
+import org.mobicents.timers.TimerTaskData;
+import org.mobicents.timers.timer.FaultTolerantTimer;
+import org.mobicents.timers.timer.FaultTolerantTimerTimerTaskFactory;
+
+import junit.framework.TestCase;
+
+public class TimerTest extends TestCase{
+       private DefaultMobicentsCluster startNode(String name) {
+ Cache myCache = org.jboss.cache.DefaultCacheFactory.getInstance().createCache(
+                               
TimerTest.class.getClassLoader().getResourceAsStream(
+                                               
"config-samples/total-replication.xml"));
+               MobicentsCache cache = new MobicentsCache(myCache, name, false);
+               cache.startCache();
+ DefaultMobicentsCluster cluster = new DefaultMobicentsCluster(cache, null, new DefaultClusterElector());
+               return cluster;
+       }
+
+       public void timerTestWithoutScheduler2nodes1timer() throws Exception {
+               DefaultMobicentsCluster c1 = startNode("1");
+               DefaultMobicentsCluster c2 = startNode("2");
+ FaultTolerantTimer t1 = new FaultTolerantTimer("task", c1, (byte) 1, null); + FaultTolerantTimer t2 = new FaultTolerantTimer("task", c2, (byte) 1, null);
+               t1.schedule(new MyTimerTask(), 0, 1000);
+               Thread.sleep(2000);
+               long count = timerCallback;
+
+               // expect at 1 or 2 ticks here
+               assertTrue(count>0);
+
+               // kill the owner
+               c1.getMobicentsCache().stop();
+
+               // wait for another 1 or 2 ticks
+               Thread.sleep(2000);
+
+               // fail if there are none
+               assertTrue(timerCallback>count);
+               count = timerCallback;
+
+               // kill the last node, no nodes are alive now
+               c2.getMobicentsCache().stop();
+               Thread.sleep(2000);
+
+               // expect no additional ticks
+               assertEquals(count, timerCallback);
+       }
+
+       public void timerTestScheduler2nodes1timer() throws Exception {
+               DefaultMobicentsCluster c1 = startNode("1");
+               DefaultMobicentsCluster c2 = startNode("2");
+ FaultTolerantTimerTimerTaskFactory factory1 = new FaultTolerantTimerTimerTaskFactory(); + FaultTolerantTimerTimerTaskFactory factory2 = new FaultTolerantTimerTimerTaskFactory(); + FaultTolerantScheduler scheduler1 = new FaultTolerantScheduler("d", 11, c1, (byte) 1, null, factory1); + FaultTolerantScheduler scheduler2 = new FaultTolerantScheduler("d", 11, c2, (byte) 1, null, factory2);
+               factory1.setScheduler(scheduler1);
+               factory2.setScheduler(scheduler2);
+ scheduler1.schedule(new org.mobicents.timers.TimerTask(new TimerTaskData(UUID.randomUUID().toString(), + System.currentTimeMillis()+1000, 1000, PeriodicScheduleStrategy.withFixedDelay)) {
+
+                       @Override
+                       public void runTask() {
+                               timerCallback++;
+
+                       }
+               });
+               Thread.sleep(2000);
+               long count = timerCallback;
+
+               // expect at 1 or 2 ticks here
+               assertTrue(count>0);
+
+               // kill the owner
+               c1.getMobicentsCache().stop();
+
+               // wait for another 1 or 2 ticks
+               Thread.sleep(2000);
+
+               // fail if there are none
+               assertTrue(timerCallback>count);
+               count = timerCallback;
+
+               // kill the last node, no nodes are alive now
+               c2.getMobicentsCache().stop();
+               Thread.sleep(2000);
+
+               // expect no additional ticks
+               assertEquals(count, timerCallback);
+       }
+
+       public void sysclockChangeObservation1() throws Exception {
+               ScheduledExecutorService s = 
Executors.newScheduledThreadPool(10);
+               s.scheduleAtFixedRate(new Runnable() {
+
+                       public void run() {
+                               System.out.println(System.currentTimeMillis() - 
timerCallback);
+                               timerCallback = System.currentTimeMillis();
+                       }
+               }, 1000, 1000, TimeUnit.MILLISECONDS);
+
+
+               Thread.sleep(4000);
+               s.shutdownNow();
+       }
+
+       public void sysclockChangeObservation2() throws Exception {
+               Timer t = new Timer();
+               t.schedule(new TimerTask() {
+
+                       @Override
+                       public void run() {
+                               System.out.println(System.currentTimeMillis() - 
timerCallback);
+                               timerCallback = System.currentTimeMillis();
+                       }
+               }, 1000, 1000);
+
+               Thread.sleep(4000);
+               t.cancel();
+       }
+
+
+
+       private static long timerCallback = 0;
+
+ private static class MyTimerTask extends TimerTask implements Serializable{
+
+               /**
+                *
+                */
+               private static final long serialVersionUID = 1L;
+
+               @Override
+               public void run() {
+                       timerCallback++;
+
+               }
+       }
+
+       protected void setUp(){}
+       protected void tearDown(){}
+}
=======================================
--- /dev/null
+++ /trunk/servers/cluster/timers/src/test/resources/log4j.xml Mon Jan 3 04:15:09 2011
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";>
+ <appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
+    <param name="file" value="logs/sip-server.log"/>
+    <param name="MaxFileSize" value="2000KB"/>
+    <!-- Keep one backup file -->
+    <param name="MaxBackupIndex" value="20"/>
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%d %p %t %l - %m%n"/>
+    </layout>
+  </appender>
+
+  <appender name="console" class="org.apache.log4j.ConsoleAppender">
+    <param name="Target" value="System.out"/>
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%d %p %t %l - %m%n"/>
+    </layout>
+  </appender>
+  <root>
+    <priority value ="DEBUG" />
+    <appender-ref ref="console" />
+  </root>
+</log4j:configuration>
=======================================
--- /trunk/servers/cluster/cache/src/main/java/org/mobicents/cache/MobicentsCache.java Tue Aug 3 01:55:56 2010 +++ /trunk/servers/cluster/cache/src/main/java/org/mobicents/cache/MobicentsCache.java Mon Jan 3 04:15:09 2011
@@ -76,8 +76,13 @@

        @SuppressWarnings("unchecked")
        public MobicentsCache(Cache cache, String cacheName) {
+               this(cache, cacheName, true);
+       }
+
+       @SuppressWarnings("unchecked")
+       public MobicentsCache(Cache cache, String cacheName, boolean managed) {
                this.jBossCache = cache;
-               this.managedCache = true;
+               this.managedCache = managed;
                startCache();
        }

=======================================
--- /trunk/servers/cluster/timers/pom.xml       Tue Dec 21 11:57:27 2010
+++ /trunk/servers/cluster/timers/pom.xml       Mon Jan  3 04:15:09 2011
@@ -16,7 +16,14 @@
                <artifactId>core</artifactId>
                <version>${pom.version}</version>
                </dependency>
+               <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+                       <version>3.8.1</version>
+                       <scope>test</scope>
+               </dependency>
        </dependencies>
+

        <build>
                <plugins>

Reply via email to