Multithreading unit test

I have a C # method that will be called multiple times using different threads. So I want to create a unit test that will test this method on multiple threads, but I'm not sure if I am doing this correctly.

This is my unit test without streaming:

    [TestMethod]
    public void FromLocalPoints()
    {
        var projectedCoordinates = this.ConvertFromLocalPoints();
        foreach (var projectedCoordinate in projectedCoordinates)
        {
            Debug.Write(projectedCoordinate.X);
            Debug.Write("; ");
            Debug.WriteLine(projectedCoordinate.Y);
        }
    }

this.ConvertFromLocalPoints () calls the actual method I want to test.

I created a delegate, event, and handler:

public delegate void ReprojectCompleteHandler(IEnumerable<Coordinate> projectedCoordinates);
public event ReprojectCompleteHandler ReprojectCompleteEvent;
private void ReprojectHandler(IEnumerable<Coordinate> projectedCoordinates)
{
        Debug.WriteLine("Conversion is complete");
}

In my TestSetup, I listen to my event:

    [TestInitialize]
    public void TestSetup()
    {
        this.ReprojectCompleteEvent += this.ReprojectHandler;
    }

My unit test:

    [TestMethod]
    public void FromLocalPointsThreaded()
    {
        // Call FromLocalPoints multiple times in separate threads to check if it is thread safe
        for (var i = 0; i < 10; i++)
        {
            var myThread = new Thread(this.ConvertFromLocalPointsThreaded);    
        }

        Debug.WriteLine("FromLocalPointsThreaded is done");
    }

    private void ConvertFromLocalPointsThreaded()
    {
        var projectedCoordinates = this.ConvertFromLocalPoints();

        // Send result to delegate/event:
        if (this.ReprojectCompleteEvent != null)
        {
            this.ReprojectCompleteEvent(projectedCoordinates);
        }
    }

When I run this unit test, I get "FromLocalPointsThreaded is done" once in my output, but "Conversion is not completed."

What am I missing to get this to work? Or should I use a different approach?

, . , . , . unit test, , .

+4
2

, . :

    [TestMethod]
    public void FromLocalPointsParallel()
    {
        var loop = new int[10];
        Parallel.ForEach(
            loop,
            item =>
                {
                    var projectedCoordinates = this.ConvertToLocalPoints();
                    foreach (var projectedCoordinate in projectedCoordinates)
                    {
                        var x = projectedCoordinate.X;
                        var y = projectedCoordinate.Y;
                        Assert.IsFalse(double.IsInfinity(x) || double.IsNaN(x), "Projected X is not a valid number");
                        Assert.IsFalse(double.IsInfinity(y) || double.IsNaN(y), "Projected Y is not a valid number");
                    }
                });
    }

unit test , : . , , , , . , , .

.

0

Unit test , . , , , . . , 999 , . , , .

:

, . , Unit test , . , , , - .

. , .

, . , , . , 100% . , .

+1

All Articles