Click here to Skip to main content
12,243,522 members (49,685 online)
Click here to Skip to main content

Tagged as

Stats

6.4K views
166 downloads
6 bookmarked
Posted

Event traffic calming

, 26 Jan 2013 CPOL
How to build an object to calm down event traffic.
using System;
using System.Linq.Expressions;
using System.Threading;
using NUnit.Framework;

namespace TrafficCalming.Tests
{
    [TestFixture]
    public class EventCalmerFixture
    {
        private static readonly TimeSpan WaitTime = TimeSpan.FromMilliseconds(100);

        private EventCalmer _calmer;

        [SetUp]
        public void Setup()
        {
            _calmer = new EventCalmer(WaitTime);
        }

        [Test]
        public void Can_create_instances()
        {
            Assert.That(_calmer, Is.Not.Null);
        }

        [Test]
        public void HandleEvent_triggers_EventOcurred_as_soon_as_posible()
        {
            var untilEventHasBeenTriggered = new ManualResetEvent(false);
            _calmer.EventOccurred += (s,e) => 
                untilEventHasBeenTriggered.Set();

            _calmer.HandleEvent(this, EventArgs.Empty);

            var didNotTimedOut = untilEventHasBeenTriggered.WaitOne(1000);
            Assert.That(didNotTimedOut);
        }

        [Test]
        public void HandleEvent_twice_triggers_EventOcurred_only_once_in_time_frame_smaller_than_waitime()
        {
            var eventOcurredTriggeredCount = 0;
            _calmer.EventOccurred += (s, e) =>
                eventOcurredTriggeredCount++;

            _calmer.HandleEvent(this, EventArgs.Empty);
            _calmer.HandleEvent(this, EventArgs.Empty);

            Assert.That(eventOcurredTriggeredCount, Is.LessThan(2));
        }

        [Test]
        public void HandleEvent_does_not_block_main_thread()
        {
            _calmer.EventOccurred += (s, e) =>
                Thread.Sleep(200);

            ExecuteWithTimeout(() => 
                _calmer.HandleEvent(this, EventArgs.Empty), 10);
        }

        private static void ExecuteWithTimeout(Expression<Action> expression, int timeoutMillis)
        {
            var untilActionFinishes = new ManualResetEvent(false);
            var action = expression.Compile();
            Action actionAsync = 
                () =>
                {
                    action();
                    untilActionFinishes.Set();
                };

            actionAsync.BeginInvoke(null, null);
            if (!untilActionFinishes.WaitOne(timeoutMillis))
                throw new Exception("Execution timed out for expression: '" + expression + "'");
        }
    }
}

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

Erich Ledesma
Architect SunHotels
Spain Spain
I Received a Bachelor's Degree in Computer Science at the Mathematics and Computer Science Faculty, University of Havana, Cuba.

I mainly work in web applications using C# and some Javascript. Some very few times do some Java.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 26 Jan 2013
Article Copyright 2013 by Erich Ledesma
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid