Search code examples
c#asp.nettestcase

Write Test case in c# at a time 50 people call same process in asp.net c#


i want to create test case which allow to hit 50 times at a time get their results individually.

want to know what happen if 50 people come at a time


Solution

  • You didn't mention why you don't want to use foreach, but here is a way to do it with LINQ, assuming you have a single-user test that can be invoked with a function named ExecuteTestForAUser() (yours may differ):

    const numberOfTests = 50;
    var tasks = Enumerable.Range(1, numberOfTests)
        .Select
        ( 
            i => Task.Run( () => ExecuteTestForAUser() ) 
        )
        .ToArray();
    Task.WaitAll( tasks );
    

    This code produces a list of 50 integers using Enumerable.Range. It then converts the list into a list of 50 tasks using Select. Task.Run() will put each task on the thread pool. Then Task.WaitAll() will block execution until they have all completed.

    You will need to make sure ExecuteTestForAUser() is thread-safe.