1

I'm trying to test my site under heavy load. I have found that the incorrect title appears on some occassions which indicates to me that i have a possible threading issue. To help solve this i've setup the following test:

[TestMethod]
public void Test1() {
    var isValid = true;

    for (var i = 0; i < 100; i++) {
        // Send the request (note Http.WebRequest is a utility method which returns the contents of the page)
        var request = Http.WebRequest("http://localhost/SomePage");

        var document = new HtmlDocument(); // Html Agility Pack
        document.LoadHtml(request.Data);

        // Get the required info
        var title = document.DocumentNode.SelectSingleNode("//title").InnerText.Trim();

        // Test if the info is valid
        if (title != "Some Page") {
            isValid = false;
            break;
        }

        Thread.Sleep(100);
    }

    Assert.IsTrue(isValid);
}

Notice i am issuing this web request to the local server. In which case this test always passes as the test is sequential and therefore only 1 request happens at a time. If i change the url to target the live server then the request fails which indicates a problem exists under heavy load.

I was wondering how i could modify this test so that i can replicate the heavy load by with my local server. This way i can experiment with various fixes without having to mess around with the live site. After reading this thread. I managed come up with the following:

[TestMethod]
public void Test1() {
    var isValid = true;
    var threads = new Thread[100];

    for (var i = 0; i < threads.Count(); i++) {
        threads[i] = new Thread(() => {
            for (var j = 0; j < 10; j++) {
                // Send the request
                var request = Http.WebRequest("http://localhost/SomePage");

                var document = new HtmlDocument();
                document.LoadHtml(request.Data);

                // Get the required info
                var title = document.DocumentNode.SelectSingleNode("//title").InnerText.Trim();

                // Test if the info is valid
                if (title != "Some Page") {
                    isValid = false;
                    break;
                }
            }
        });
    }

    foreach (var thread in threads) {
        thread.Start();
    }

    foreach (var thread in threads) {
        thread.Join();
    }

    Assert.IsTrue(isValid);
}

However when i ran this test it threw the error "The agent process was stopped while the test was running.". I'd appreciate it if someone could show me what i'm doing wrong or suggest an alternate solution. Thanks

Community
  • 1
  • 1
nfplee
  • 7,643
  • 12
  • 63
  • 124
  • Cay you use tools such as [ab](http://httpd.apache.org/docs/2.0/programs/ab.html) or [JMeter](http://en.wikipedia.org/wiki/JMeter) ? – gagabu Aug 04 '11 at 11:07

1 Answers1

0

The error "The agent process was stopped while the test was running" can be caused by exceptions thrown by background threads.

I would try adding a try/catch around the code which the background thread executes, and have the catch block trace any exceptions so that you can check if this is the case. For example, when you create a new thread you can do something along the lines of...

threads[i] = new Thread(() => {
    try {
        for (var j = 0; j < 10; j++) {
            // Send the request
            var request = Http.WebRequest("http://localhost/SomePage");
            var document = new HtmlDocument();
            document.LoadHtml(request.Data);

            // Get the required info
            var title = document.DocumentNode.SelectSingleNode("//title").InnerText.Trim();

            // Test if the info is valid
            if (title != "Some Page") {
                isValid = false;
                break;
            }
        }
    }
    catch (Exception ex) {
        Trace.WriteLine(ex);
    }
});

...although at this point you might want to promote that lambda to a private method.

This issue can also be caused by finalizers throwing exceptions, but as your code simply exercises a web request using some well known types, I doubt that is the case.

If you still can't get it to work, you may want to take a look at the following connect bug: http://connect.microsoft.com/VisualStudio/feedback/details/556702/unit-testing-the-agent-process-was-stopped-while-the-test-was-running

Alex Humphrey
  • 6,099
  • 4
  • 26
  • 41