Permalink
Browse files

Implementing Retry Tests and DummySleeper

  • Loading branch information...
1 parent 7e41fdd commit daadf8e89e2c8afaae29653f018697470b630277 @joefitzgerald joefitzgerald committed Dec 7, 2012
@@ -0,0 +1,56 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="AbstractExceptionTests.cs" company="The original author or authors.">
+// Copyright 2002-2012 the original author or authors.
+//
+// Licensed 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.
+// </copyright>
+// --------------------------------------------------------------------------------------------------------------------
+
+#region Using Directives
+using System;
+using NUnit.Framework;
+#endregion
+
+namespace Spring.Retry.Tests.Retry
+{
+ /// <summary>
+ /// Abstract Exception Test Class.
+ /// </summary>
+ /// <author>Joe Fitzgerald (.NET)</author>
+ public abstract class AbstractExceptionTests
+ {
+ /// <summary>The test exception string.</summary>
+ [Test]
+ public void TestExceptionString()
+ {
+ var exception = this.GetException("foo");
+ Assert.AreEqual("foo", exception.Message);
+ }
+
+ /// <summary>The test exception string exception.</summary>
+ [Test]
+ public void TestExceptionStringException()
+ {
+ var exception = this.GetException("foo", new InvalidOperationException());
+ Assert.AreEqual("foo", exception.Message.Substring(0, 3));
+ }
+
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <returns>The System.Exception.</returns>
+ public abstract Exception GetException(string msg);
+
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <param name="t">The t.</param>
+ /// <returns>The System.Exception.</returns>
+ public abstract Exception GetException(string msg, Exception t);
+ }
+}
@@ -0,0 +1,41 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="BackOffInterruptedExceptionTests.cs" company="The original author or authors.">
+// Copyright 2002-2012 the original author or authors.
+//
+// Licensed 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.
+// </copyright>
+// --------------------------------------------------------------------------------------------------------------------
+
+#region Using Directives
+using System;
+using NUnit.Framework;
+using Spring.Retry.Retry.Backoff;
+#endregion
+
+namespace Spring.Retry.Tests.Retry
+{
+ /// <summary>
+ /// BackOff Interrupted Exception Tests
+ /// </summary>
+ [TestFixture]
+ public class BackOffInterruptedExceptionTests : AbstractExceptionTests
+ {
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg) { return new BackOffInterruptedException(msg); }
+
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <param name="t">The t.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg, Exception t) { return new BackOffInterruptedException(msg, t); }
+ }
+}
@@ -0,0 +1,54 @@
+// -----------------------------------------------------------------------
+// <copyright file="DummySleeper.cs" company="">
+// TODO: Update copyright text.
+// </copyright>
+// -----------------------------------------------------------------------
+
+using Spring.Retry.Retry.Backoff;
+
+namespace Spring.Retry.Tests.Retry.Backoff
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+
+ /// <summary>
+ /// Simple <see cref="ISleeper"/> implementation that just waits on a local object.
+ /// </summary>
+ /// <author>Dave Syer</author>
+ /// <author>Joe Fitzgerald (.NET)</author>
+ public class DummySleeper : ISleeper
+ {
+ private List<long> backOffs = new List<long>();
+
+ /**
+ * Public getter for the long.
+ * @return the lastBackOff
+ */
+ public long GetLastBackOff()
+ {
+ return backOffs[backOffs.Count - 1];
+ }
+
+ public long[] GetBackOffs()
+ {
+ return backOffs.ToArray();
+
+ // var result = new long[backOffs.Count];
+ // var i = 0;
+ // for (Iterator<Long> iterator = backOffs.iterator(); iterator.hasNext(); )
+ // {
+ // Long value = iterator.next();
+ // result[i++] = value.longValue();
+ // }
+
+ // return result;
+ }
+
+ public void Sleep(long backOffPeriod)
+ {
+ this.backOffs.Add(backOffPeriod);
+ }
+ }
+}
@@ -0,0 +1,41 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="ExhaustedRetryExceptionTests.cs" company="The original author or authors.">
+// Copyright 2002-2012 the original author or authors.
+//
+// Licensed 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.
+// </copyright>
+// --------------------------------------------------------------------------------------------------------------------
+
+#region Using Directives
+using System;
+using NUnit.Framework;
+using Spring.Retry.Retry;
+#endregion
+
+namespace Spring.Retry.Tests.Retry
+{
+ /// <summary>
+ /// ExhaustedRetryException Tests
+ /// </summary>
+ [TestFixture]
+ public class ExhaustedRetryExceptionTests : AbstractExceptionTests
+ {
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg) { return new ExhaustedRetryException(msg); }
+
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <param name="t">The t.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg, Exception t) { return new ExhaustedRetryException(msg, t); }
+ }
+}
@@ -0,0 +1,106 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="ResourcelessTransactionManager.cs" company="The original author or authors.">
+// Copyright 2002-2012 the original author or authors.
+//
+// Licensed 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.
+// </copyright>
+// --------------------------------------------------------------------------------------------------------------------
+
+#region Using Directives
+using System.Collections.Generic;
+using Spring.Transaction;
+using Spring.Transaction.Support;
+#endregion
+
+namespace Spring.Retry.Tests.Retry
+{
+ /// <summary>
+ /// Resourceless Transaction Manager
+ /// </summary>
+ public class ResourcelessTransactionManager : AbstractPlatformTransactionManager
+ {
+ /// <summary>The do begin.</summary>
+ /// <param name="transaction">The transaction.</param>
+ /// <param name="definition">The definition.</param>
+ protected override void DoBegin(object transaction, ITransactionDefinition definition) { ((ResourcelessTransaction)transaction).Begin(); }
+
+ /// <summary>The do commit.</summary>
+ /// <param name="status">The status.</param>
+ protected override void DoCommit(DefaultTransactionStatus status) { this.log.Debug("Committing resourceless transaction on [" + status.Transaction + "]"); }
+
+ /// <summary>The do get transaction.</summary>
+ /// <returns>The System.Object.</returns>
+ protected override object DoGetTransaction()
+ {
+ var transaction = new ResourcelessTransaction();
+ Stack<object> resources;
+ if (!TransactionSynchronizationManager.HasResource(this))
+ {
+ resources = new Stack<object>();
+ TransactionSynchronizationManager.BindResource(this, resources);
+ }
+ else
+ {
+ var stack = (Stack<object>)TransactionSynchronizationManager.GetResource(this);
+ resources = stack;
+ }
+
+ resources.Push(transaction);
+ return transaction;
+ }
+
+ /// <summary>The do rollback.</summary>
+ /// <param name="status">The status.</param>
+ protected override void DoRollback(DefaultTransactionStatus status) { this.log.Debug("Rolling back resourceless transaction on [" + status.Transaction + "]"); }
+
+ /// <summary>The is existing transaction.</summary>
+ /// <param name="transaction">The transaction.</param>
+ /// <returns>The System.Boolean.</returns>
+ protected override bool IsExistingTransaction(object transaction)
+ {
+ if (TransactionSynchronizationManager.HasResource(this))
+ {
+ var stack = (Stack<object>)TransactionSynchronizationManager.GetResource(this);
+ return stack.Count > 1;
+ }
+
+ return ((ResourcelessTransaction)transaction).Active;
+ }
+
+ /// <summary>The do set rollback only.</summary>
+ /// <param name="status">The status.</param>
+ protected override void DoSetRollbackOnly(DefaultTransactionStatus status) { }
+
+ /// <summary>The do cleanup after completion.</summary>
+ /// <param name="transaction">The transaction.</param>
+ protected override void DoCleanupAfterCompletion(object transaction)
+ {
+ var list = (Stack<object>)TransactionSynchronizationManager.GetResource(this);
+ var resources = list;
+ resources.Clear();
+ TransactionSynchronizationManager.UnbindResource(this);
+ ((ResourcelessTransaction)transaction).Clear();
+ }
+ }
+
+ internal class ResourcelessTransaction
+ {
+ private bool active;
+
+ /// <summary>Gets a value indicating whether active.</summary>
+ public bool Active { get { return this.active; } }
+
+ /// <summary>The begin.</summary>
+ public void Begin() { this.active = true; }
+
+ /// <summary>The clear.</summary>
+ public void Clear() { this.active = false; }
+ }
+}
@@ -0,0 +1,41 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="RetryExceptionTests.cs" company="The original author or authors.">
+// Copyright 2002-2012 the original author or authors.
+//
+// Licensed 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.
+// </copyright>
+// --------------------------------------------------------------------------------------------------------------------
+
+#region Using Directives
+using System;
+using NUnit.Framework;
+using Spring.Retry.Retry;
+#endregion
+
+namespace Spring.Retry.Tests.Retry
+{
+ /// <summary>
+ /// RetryExceptionTests
+ /// </summary>
+ [TestFixture]
+ public class RetryExceptionTests : AbstractExceptionTests
+ {
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg) { return new RetryException(msg); }
+
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <param name="t">The t.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg, Exception t) { return new RetryException(msg, t); }
+ }
+}
@@ -0,0 +1,41 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="TerminatedRetryExceptionTests.cs" company="The original author or authors.">
+// Copyright 2002-2012 the original author or authors.
+//
+// Licensed 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.
+// </copyright>
+// --------------------------------------------------------------------------------------------------------------------
+
+#region Using Directives
+using System;
+using NUnit.Framework;
+using Spring.Retry.Retry;
+#endregion
+
+namespace Spring.Retry.Tests.Retry
+{
+ /// <summary>
+ /// TerminatedRetryException Tests
+ /// </summary>
+ [TestFixture]
+ public class TerminatedRetryExceptionTests : AbstractExceptionTests
+ {
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg) { return new TerminatedRetryException(msg); }
+
+ /// <summary>The get exception.</summary>
+ /// <param name="msg">The msg.</param>
+ /// <param name="t">The t.</param>
+ /// <returns>The System.Exception.</returns>
+ public override Exception GetException(string msg, Exception t) { return new TerminatedRetryException(msg, t); }
+ }
+}
Oops, something went wrong.

0 comments on commit daadf8e

Please sign in to comment.