Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:12:47+00:00 2026-05-29T15:12:47+00:00

I am unit testing a component that does work on any number of threads

  • 0

I am unit testing a component that does work on any number of threads and raises events on those threads. I have written a simple event register that I use to assert that the events I expect have been raised:

    public class EventRegister
    {
        private readonly object _lock = new object();
        public int Count { get; private set; }

        public void Increment()
        {
            lock (_lock)
            {
                Count++;
                Monitor.Pulse(_lock);
            }
        }

        public static EventRegister operator ++(EventRegister counter)
        {
            counter.Count++;
            return counter;
        }

        public static implicit operator int(EventRegister counter)
        {
            return counter.Count;
        }

        public bool HasFired { get { return Count > 0; } }

        public void Wait(int unitCount)
        {
            Wait(unitCount, TimeSpan.MinValue);
        }

        public void Wait(int unitCount, TimeSpan timeout)
        {
            lock (_lock)
            {
                var start = DateTime.Now;

                while (Count < unitCount)
                {
                    if (timeout != TimeSpan.MinValue && !Debugger.IsAttached)
                    {
                        Monitor.Wait(_lock, timeout);

                        if (DateTime.Now-start>timeout)
                            throw new TimeoutException();
                    }
                    else
                        Monitor.Wait(_lock);
                }
            }
        }
    }

this works fine and I happy enough with it, but have I reinvented the wheel? Does NUnit or MOQ (or any other lib) already include some other mechanism for doing the same thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:12:47+00:00Added an answer on May 29, 2026 at 3:12 pm

    To answer your 2 questions:

    • You have reinvented the wheel. Not quite the same wheel model I have (re) invented myself, but quite similar nonetheless. I am pretty sure a lot of other persons reinvented such a wheel somewhere else as well.

    • Moq does not provide such functionality, neither does MSTest. Last time I did use NUnit, it did not provide it.

    A lot of multi-thread, event-based boiler plate code seems to be written over and over again. Maybe it is time for a framework-agnostic thread and event helper library to be built…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Python have a unit testing framework compatible with the standard xUnit style of
We have an old asp.net application that has no unit tests, integration tests, component
When unit-testing a component, the following question occured to me: There are a number
Unit testing sounds great to me, but I'm not sure I should spend any
I've heard that unit testing is totally awesome, really cool and all manner of
So I know that unit testing is a must. I get the idea that
As a developer, I'm a newbie to Unit testing and have a requirement to
I'm facing a problem while unit testing my forms. The problem is that data
We have a database against which we run unit tests for components that require
I have a PHP application that is written with Zend Framework. It uses Phing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.