activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r1475827 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src: main/csharp/Threads/Scheduler.cs test/csharp/Threads/SchedulerTest.cs
Date Thu, 25 Apr 2013 15:38:15 GMT
Author: tabish
Date: Thu Apr 25 15:38:15 2013
New Revision: 1475827

URL: http://svn.apache.org/r1475827
Log:
Adds some tests and tweaks.

Added:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Threads/SchedulerTest.cs
  (with props)
Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Threads/Scheduler.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Threads/Scheduler.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Threads/Scheduler.cs?rev=1475827&r1=1475826&r2=1475827&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Threads/Scheduler.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Threads/Scheduler.cs
Thu Apr 25 15:38:15 2013
@@ -30,6 +30,7 @@ namespace Apache.NMS.ActiveMQ.Threads
     	private readonly String name;
     	private TimerEx timer;
     	private readonly Dictionary<Object, TimerTask> timerTasks = new Dictionary<object,
TimerTask>();
+		private bool started = false;
 
 		public Scheduler(String name)
 		{
@@ -123,6 +124,7 @@ namespace Apache.NMS.ActiveMQ.Threads
 			lock (this.syncRoot)
 			{
 	        	this.timer = new TimerEx(name, true);
+				this.started = true;
 			}
 	    }
 
@@ -130,6 +132,7 @@ namespace Apache.NMS.ActiveMQ.Threads
 		{
 			lock (this.syncRoot)
 			{
+				this.started = false;
 	        	if (this.timer != null)
             	{
 	            	this.timer.Cancel();
@@ -142,6 +145,17 @@ namespace Apache.NMS.ActiveMQ.Threads
 			get { return this.name; }
 		}
 
+		public bool Started
+		{
+			get 
+			{ 
+				lock (this.syncRoot)
+				{
+					return this.started; 
+				}
+			}
+		}
+
 	    public override string ToString()
 		{
 			return string.Format("[Scheduler][{0}]", name);
@@ -149,7 +163,7 @@ namespace Apache.NMS.ActiveMQ.Threads
 
 		private void CheckStarted()
 		{
-			if (this.timer == null)
+			if (!this.started)
 			{
 				throw new InvalidOperationException("The Schedular has not been started yet");
 			}

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Threads/SchedulerTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Threads/SchedulerTest.cs?rev=1475827&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Threads/SchedulerTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Threads/SchedulerTest.cs
Thu Apr 25 15:38:15 2013
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Threading;
+using Apache.NMS.Util;
+using Apache.NMS.ActiveMQ.Threads;
+using NUnit.Framework;
+
+namespace Apache.NMS.ActiveMQ.Test
+{
+	[TestFixture]
+	public class SchedulerTest
+	{
+		private int counter;
+
+		private void CounterCallback(object arg)
+		{
+			this.counter++;
+		}
+
+		[SetUp]
+		public void SetUp()
+		{
+			counter = 0;
+		}
+
+		[Test]
+		public void TestConstructor()
+		{
+		    Scheduler scheduler = new Scheduler("TestConstructor");
+		    Assert.IsFalse(scheduler.Started);
+		    scheduler.Start();
+		    Assert.IsTrue(scheduler.Started);
+		    scheduler.Stop();
+		    Assert.IsFalse(scheduler.Started);
+		}
+
+		[Test]
+		public void TestNullWaitCallbackThrows()
+		{
+		    Scheduler scheduler = new Scheduler("TestNullWaitCallbackThrows");
+		    scheduler.Start();
+
+			try
+			{
+				scheduler.ExecuteAfterDelay(null, null, 100);
+				Assert.Fail("Should have thrown an exception");
+			}
+			catch (ArgumentNullException)
+			{
+			}
+
+			try
+			{
+				scheduler.ExecuteAfterDelay(null, null, TimeSpan.FromMilliseconds(100));
+				Assert.Fail("Should have thrown an exception");
+			}
+			catch (ArgumentNullException)
+			{
+			}
+
+			try
+			{
+				scheduler.ExecutePeriodically(null, null, 100);
+				Assert.Fail("Should have thrown an exception");
+			}
+			catch (ArgumentNullException)
+			{
+			}
+
+			try
+			{
+				scheduler.ExecutePeriodically(null, null, TimeSpan.FromMilliseconds(100));
+				Assert.Fail("Should have thrown an exception");
+			}
+			catch (ArgumentNullException)
+			{
+			}
+		}
+
+		[Test]
+		public void TestExecutePeriodically()
+		{
+		    {
+		        Scheduler scheduler = new Scheduler("TestExecutePeriodically");
+		        scheduler.Start();
+		        WaitCallback task = new WaitCallback(CounterCallback);
+		        scheduler.ExecutePeriodically(task, null, 500);
+		        Assert.AreEqual(0, counter);
+		        Thread.Sleep(700);
+		        Assert.IsTrue(counter >= 1, "Should have executed at least once.");
+		        Thread.Sleep(600);
+		        Assert.IsTrue(counter >= 2, "Should have executed at least twice.");
+		        Assert.IsTrue(counter < 5, "Should have executed less than five times.");
+		        scheduler.Stop();
+		    }
+
+			this.counter = 0;
+
+		    {
+		        Scheduler scheduler = new Scheduler("TestExecutePeriodically");
+		        scheduler.Start();
+		        WaitCallback task = new WaitCallback(CounterCallback);
+		        scheduler.ExecutePeriodically(task, null, 1000);
+		        Assert.AreEqual(0, counter);
+		        scheduler.Cancel(task);
+		        Thread.Sleep(1200);
+		        Assert.AreEqual(0, counter);
+		        scheduler.Stop();
+		    }
+		}
+
+		[Test]
+		public void TestExecuteAfterDelay()
+		{
+	        Scheduler scheduler = new Scheduler("TestExecuteAfterDelay");
+	        scheduler.Start();
+	        WaitCallback task = new WaitCallback(CounterCallback);
+	        scheduler.ExecuteAfterDelay(task, null, 500);
+	        Assert.AreEqual(0, counter);
+	        Thread.Sleep(700);
+	        Assert.IsTrue(counter == 1, "Should have executed at least once.");
+	        Thread.Sleep(600);
+	        Assert.IsTrue(counter == 1, "Should have executed no more than once.");
+	        scheduler.Stop();
+		}
+
+		[Test]
+		public void TestCancel()
+		{
+	        Scheduler scheduler = new Scheduler("TestCancel");
+	        scheduler.Start();
+	        WaitCallback task = new WaitCallback(CounterCallback);
+	        scheduler.ExecutePeriodically(task, null, 1000);
+	        Assert.AreEqual(0, counter);
+	        scheduler.Cancel(task);
+	        Thread.Sleep(1200);
+	        Assert.AreEqual(0, counter);
+	        scheduler.Stop();
+		}
+
+		[Test]
+		public void TestStop()
+		{
+	        Scheduler scheduler = new Scheduler("TestStop");
+	        scheduler.Start();
+	        WaitCallback task = new WaitCallback(CounterCallback);
+	        scheduler.ExecutePeriodically(task, null, 1000);
+	        Assert.AreEqual(0, counter);
+	        scheduler.Stop ();
+	        Thread.Sleep(1200);
+	        Assert.AreEqual(0, counter);
+		}
+	}
+}
+

Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Threads/SchedulerTest.cs
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message