mercredi 11 novembre 2020

How to call Parallel.For with a method of the one instance but different constructor parameters c#

I am investigating a bug and trying to replicate a SQL Exception - Cannot insert duplicate key row in object with unique index . The duplicate key value. The statement has been terminated

I am doing this within the unit tests to replicate the exception.

Here is the test:

public void Should_Throw_Exception_When_StartExecution_Is_Called_Twice_By_Two_Different_Programs_Belonging_To_Same_Account()
    {
        //Arrange
        var executorContext = new ExecutorContext
        {
            Program = new ProgramObject
            {
                ID = 100,
                Account = RootAccount
            },
            ScheduledExecutionDate = DateTime.Now
        };

        var executorContext2 = new ExecutorContext
        {
            Program = new ProgramObject
            {
                ID = 200,
                Account = RootAccount
            },
            ScheduledExecutionDate = DateTime.Now
        };

        var tracker = new ProgramExecutionTracker(executorContext, _dataContext);

        Parallel.For(0, 5, (i) => tracker.StartExecution());
    }

The Parallel.For is meant to call the start execution method multiple times, however I need to pass in two different executorContext objects when creating ProgramExecutionTracker.

How can I achieve this so that tracker.StartExecution is called by multiple threads with different executor contexts?

Aucun commentaire:

Enregistrer un commentaire