Click here to Skip to main content
12,297,706 members (22,741 online)
Click here to Skip to main content

Stats

45.2K views
742 downloads
58 bookmarked
Posted

Timeout Functions

, 11 Dec 2008 CPOL
This article explains about executing a function within a time limit. Also includes a helper class which helps to implement timeout functions easily. This article deals with running multiple timeout processes each with time limit.
namespace TimeoutFunctions.Tests
{
    using System;
    using System.Collections.Generic;
    using NUnit.Framework;
    using TimeoutFunctions;
    using System.Threading;
    using System.Diagnostics;
    using System.IO;

    [TestFixture]
    public class TimeoutProcessTests
    {
        [Test]
        public void CanTimeoutRunningProcess() {
            WaitCallback waitCallBack = GetLongRunningProcess();
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            bool status = timeoutProcess.Start(null, TimeSpan.FromSeconds(2), waitCallBack);
            Assert.That(!status, "Process is not timing out");
        }

        [Test]
        public void CanFinishRunningProcessWithMoreTimeoutLimit() {
            WaitCallback waitCallBack = GetAFastRunningProcess();
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            bool status = timeoutProcess.Start(null, TimeSpan.FromSeconds(30), waitCallBack);
            Assert.That(status, "Process is not completing");
        }

        [Test]
        public void CanTimeoutBlockedProcess() {
            WaitCallback waitCallBack = GetBlockedProcess(5000);
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            bool success = timeoutProcess.Start(null, TimeSpan.FromSeconds(2), waitCallBack);
            Assert.That(!success);
        }

        EventWaitHandle nUnitHandle = new AutoResetEvent(false);
        [Test]
        public void AsyncProcessTimeoutTest() {
            WaitCallback waitCallBack = GetLongRunningProcess();
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            timeoutProcess.AsyncProcessCompleted += timeoutProcess_AsyncProcessTimedout;
            timeoutProcess.StartAsync(null, TimeSpan.FromSeconds(2), waitCallBack);
            nUnitHandle.WaitOne();
        }

        void timeoutProcess_AsyncProcessTimedout(object sender, AsyncProcessEventArgs e) {
            Assert.That(!e.HasProcessCompleted, "Async operation not timed out");
            Assert.AreEqual(false, e.IsThreadRunning, "Operation timedout, but thread still running");
            nUnitHandle.Set();
        }

        EventWaitHandle[] waitHandles = null;
        [Test]
        public void MultipleAsyncProcessTimingout() {
            const int waitHandleCount = 10;
            waitHandles = new EventWaitHandle[waitHandleCount];
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            timeoutProcess.AsyncProcessCompleted += timeoutProcess_MultipleAsyncProcessTimedout;
            for (int i = 0; i < waitHandleCount; i++) {
                waitHandles[i] = new AutoResetEvent(false);
                WaitCallback waitCallBack = GetLongRunningProcess();
                timeoutProcess.StartAsync(i, TimeSpan.FromSeconds(2), waitCallBack);
            }
            Assert.AreEqual(10, timeoutProcess.AsyncProcessCount, "Process count is not matching");
            Assert.That(timeoutProcess.IsAsyncProcessExecuting, "AsyncProcessExecuting flag is setting");
            WaitHandle.WaitAll(waitHandles);
        }

        void timeoutProcess_MultipleAsyncProcessTimedout(object sender, AsyncProcessEventArgs e) {
            int processIndex = (int)e.UserState;
            Assert.That(!e.HasProcessCompleted, string.Format("Async operation {0} not timed out", processIndex));
            waitHandles[processIndex].Set();
        }

        [Test]
        public void MultipleAsyncProcessCompletedSuccessfully() {
            const int waitHandleCount = 10;
            waitHandles = new EventWaitHandle[waitHandleCount];
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            timeoutProcess.AsyncProcessCompleted += timeoutProcess_MultipleAsyncProcessCompleted;
            for (int i = 0; i < waitHandleCount; i++) {
                waitHandles[i] = new AutoResetEvent(false);
                WaitCallback waitCallBack = GetAFastRunningProcess();
                timeoutProcess.StartAsync(i, TimeSpan.FromMinutes(1), waitCallBack);
            }
            WaitHandle.WaitAll(waitHandles);
            Assert.AreEqual(0, timeoutProcess.AsyncProcessCount, "Async process count is not matching");
            Assert.That(!timeoutProcess.IsAsyncProcessExecuting, "Async process executing flag is not resetting");
        }

        void timeoutProcess_MultipleAsyncProcessCompleted(object sender, AsyncProcessEventArgs e) {
            int processIndex = (int)e.UserState;
            Assert.That(e.HasProcessCompleted, string.Format("Async operation {0} not completed", processIndex));
            waitHandles[processIndex].Set();
        }

        [Test]
        public void AsyncProcessCountComingCorrectly() {
            TimeoutProcess process = new TimeoutProcess();
            for (int i = 0; i < 10; i++) {
                WaitCallback waitCallBack = GetLongRunningProcess();
                process.StartAsync(null, TimeSpan.FromMinutes(1), waitCallBack);
            }
            Assert.AreEqual(10, process.AsyncProcessCount, "Async process count is not matching");
        }

        [Test]
        public void AsyncProcessCompletionTestWithAValidObject() {
            List<int> data = new List<int>();
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            timeoutProcess.AsyncProcessCompleted += delegate(object sender, AsyncProcessEventArgs args)
            {
                Assert.AreEqual(true, args.HasProcessCompleted);
                nUnitHandle.Set();
            };
            WaitCallback callBack = delegate(object obj)
            {
                Thread.Sleep(2000);
                for (int i = 0; i < 2000; i++)
                    data.Add(i);
            };
            timeoutProcess.StartAsync(null, TimeSpan.FromMinutes(1), callBack);
            nUnitHandle.WaitOne();
            Assert.AreEqual(0, timeoutProcess.AsyncProcessCount, "Async process not ended");
            Assert.AreEqual(2000, data.Count, "Invalid data count detected");
        }

        [Test]
        public void AsyncProcessTimeoutTestWithAValidObject() {
            List<int> data = new List<int>();
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            timeoutProcess.AsyncProcessCompleted += delegate(object sender, AsyncProcessEventArgs args)
            {
                nUnitHandle.Set();
                Assert.AreEqual(false, args.IsThreadRunning, "Worker still running");
            };
            WaitCallback callBack = delegate(object obj)
            {
                for (int i = 0; i < 2000000; i++) {
                    data.Add(i);
                    Thread.Sleep(100);
                }
            };
            timeoutProcess.StartAsync(null, TimeSpan.FromSeconds(1), callBack);
            nUnitHandle.WaitOne();
            Assert.AreEqual(0, timeoutProcess.AsyncProcessCount, "Async process not ended");
            Assert.AreNotEqual(2000000, data.Count, "Invalid data count detected");
        }

        [Test]
        public void ShouldTimeoutEvenUserHandlesThreadExceptions() {
            WaitCallback callBack = delegate(object obj)
            {

                for (int i = 0; i < 200000; i++) {
                    try {
                        Debug.WriteLine(i);
                    }
                    catch (ThreadAbortException) {
                        // do nothing
                    }
                    catch (ThreadInterruptedException) {
                        // do nothing
                    }
                }

            };
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            bool status = timeoutProcess.Start(null, TimeSpan.FromSeconds(1), callBack);
            Assert.AreEqual(false, status, "User handled thread exception process not timing out");
        }

        [Test]
        public void IsThreadRunningComingTRUE() {
            WaitCallback callBack = delegate(object obj)
            {
                for (int i = 0; i < 200000; i++) {
                    try {
                        Debug.WriteLine(i);
                    }
                    catch (ThreadAbortException) {
                        // don't allow to reset
                        Thread.ResetAbort();
                    }
                }
            };
            TimeoutProcess timeoutProcess = new TimeoutProcess();
            timeoutProcess.AsyncProcessCompleted += delegate(object sender, AsyncProcessEventArgs args)
            {
                Assert.AreEqual(true, args.IsThreadRunning, "Thread running flag is not set");
                Assert.AreEqual(true, args.AsyncWorker != null, "Async worker is NULL");
                nUnitHandle.Set();
            };
            timeoutProcess.StartAsync(null, TimeSpan.FromSeconds(1), callBack);
            nUnitHandle.WaitOne();
        }

        WaitCallback GetLongRunningProcess() {
            return delegate(object obj)
            {
                for (int i = 0; i < 200000; i++) {
                    Debug.WriteLine(i);
                }
            };
        }

        WaitCallback GetBlockedProcess(int duration) {
            return delegate(object obj)
            {
                Thread.Sleep(duration);
            };
        }

        WaitCallback GetAFastRunningProcess() {
            return delegate(object obj)
            {
                for (int i = 0; i < 200; i++) {
                    Debug.WriteLine(i);
                }
            };
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

N a v a n e e t h
Software Developer ThoughtWorks
India India
Call me Navaneeth Some years ago--never mind how long precisely, I was doing programs with C and C++. When .NET came, I started with C#,ASP.NET and SQL Server.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 11 Dec 2008
Article Copyright 2008 by N a v a n e e t h
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid