Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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]
	[Explicit("All of these tests take a long time to complete.")]
	[Description("These tests are to statistically prove the calmer works correctly.")]
	public class EventCalmerIntegrationFixture
	{
	    private static readonly TimeSpan WaitTime = TimeSpan.FromMilliseconds(100);
	
	    private EventCalmer _calmer;
	
	    [SetUp]
	    public void Setup()
	    {
	        _calmer = new EventCalmer(WaitTime);
	    }
	    
	    [Test]
	    [Repeat(10)]
	    [Description("This test ensures the last event that arrives is handled.")]
	    public void HandleEvents_on_a_loop_always_executes_the_last()
	    {
	        var lastEventOccurredAt = DateTime.Now;
	        var lastEventHandledAt = DateTime.Now;
	        _calmer.EventOccurred += (s, e) =>
	            lastEventOccurredAt = DateTime.Now;
	
	        var loopStartedAt = DateTime.Now;
	
	        for (var i = 0; i < 10; i++)
	        {
	            lastEventHandledAt = DateTime.Now;
	            _calmer.HandleEvent(this, EventArgs.Empty);
	            Thread.Sleep(50);
	        }
	
	        var loopTime = DateTime.Now - loopStartedAt;
	        Thread.Sleep(TimeSpan.FromSeconds(10) - loopTime);
	
	        Assert.That(lastEventOccurredAt, Is.GreaterThanOrEqualTo(lastEventHandledAt));
	    }
	
	    [Test]
	    [Repeat(10)]
	    [Description("This test ensures handled events count is close (+/-1) to (total time)/(handling time).")]
	    public void HandleEvents_on_a_loop_execute_average_events_that_fit()
	    {
	        var eventOcurredTriggeredCount = 0;
	        _calmer.EventOccurred += (s, e) =>
	            eventOcurredTriggeredCount++;
	
	        var loopStartedAt = DateTime.Now;
	
	        for (var i = 0; i < 10; i++)
	        {
	            _calmer.HandleEvent(this, EventArgs.Empty);
	            Thread.Sleep(50);
	        }
	
	        var loopTime = DateTime.Now - loopStartedAt;
	        Thread.Sleep(TimeSpan.FromSeconds(10) - loopTime);
	
	        var averageExecutions = loopTime.TotalMilliseconds/WaitTime.TotalMilliseconds;
	
	        Assert.That(eventOcurredTriggeredCount, 
	                    Is.InRange(averageExecutions - 1, averageExecutions + 1));
	    }}
}

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.

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