Re: BackgroundWorker problems
"Lisle, Ron" <[email protected]>
| Newsgroups | gmane.comp.windows.dotnet.nunit.user |
|---|---|
| Message-ID | <[email protected]> |
I'm sorry, but I'm a bit confused.
My [TestFixtureSetup] routine instantiates the form, connects the DoWork
and WorkCompleted events on the form's BackgroundWorker control, calls
the BackgroundWorker's RunWorkerAsync() method, then loops for awhile
waiting for the WorkCompleted event handler to set a flag. It eventually
times out, since the WorkCompleted event is never fired.
Here is the code:
const string WindowCaption = "ProgressForm Test";
ProgressForm _progressForm = null;
bool _doWorkWasCalled = false;
bool _workCompletedWasCalled = false;
bool _testSuiteSetupDone = false;
[TestFixtureSetUp]
public void DoTestSuiteSetUp()
{
_progressForm = new ProgressForm();
_progressForm.windowCaption = WindowCaption;
_progressForm.labelText = "Please wait while tests are
run...";
_progressForm.DoWork += doWork;
_progressForm.WorkCompleted += runWorkerCompleted;
_progressForm.Show();
_progressForm.backgroundWorker1.RunWorkerAsync();
for(int timeout=0; timeout < 10; timeout++)
{
if (_workCompletedWasCalled == true) break;
Thread.Sleep(10); // Give the other thread a chance to
run
}
_testSuiteSetupDone = true;
}
private bool doWork(System.ComponentModel.BackgroundWorker
backgroundWorker)
{
_doWorkWasCalled = true;
backgroundWorker.ReportProgress(0);
backgroundWorker.ReportProgress(33);
backgroundWorker.ReportProgress(66);
return true;
}
//NOTE: There appears to be a bug in NUnit or VS2005 causing
this to not be called when run under NUnit.
private void runWorkerCompleted()
{
_workCompletedWasCalled = true;
}
When you ask if work is being done, I take it you mean in the product
code, as opposed to this test code. Yes, the worker thread is actually
executing a lot of work, and calling back on the passed backgroundWorker
reference to report progress, which then updates the ProgressBar. That
doesn't happen when running the same form using the NUnit test code
above.
I've tried attaching the test code to NUnit and breaking on the various
routines above. I see the TestFixtureSetup method called, then I get the
break in the doWork method immediately after stepping over the
RunWorkerAsync() call, but never break in runWorkerCompleted() method. I
also have an event handler in the form itself to handle the progress
events, and these are not called either.
I sure appreciate the help. So far I'm stumped. I really want to use TDD
methodology, so I'm willing to rewrite the code to use some other
threading mechanism if needed in order to enable NUnit to work with
this. This is the only place I'm using multithreading in my product, but
I'd like to be able to use this same form in several different places
once it is working and testing ok.
________________________________
From: [email protected]
[mailto:[email protected]] On Behalf Of Nassar,
Anthony
Sent: Tuesday, November 13, 2007 8:43 AM
To: [email protected]
Subject: Re: [Nunit-users] BackgroundWorker problems
Forwarding...
My question was, is there any external evidence, beyond the DoWork event
getting called, that anything is happening? Because both Ian Wood and I
suspect that there is no background thread on which the work could be
done, and therefore nothing is in fact happening. You could try
debugging, and breaking within the code for the background task; you
could also write a simply task like changing the timestamp on a file,
just to verify non-programmatically that something is happening.
________________________________
From: Lisle, Ron [mailto:[email protected]]
Sent: Monday, November 12, 2007 5:42 PM
To: Nassar, Anthony
Subject: RE: [Nunit-users] BackgroundWorker problems
The form was split out into a DLL so that I can run it both in the
product and from the NUnit test harness. The work gets done when running
the same form in the product. When running under NUnit though only the
DoWork event gets called. The only "work" being done in the other 2
events is to set a flag to prove it was called.
You're suggestion regarding the message loop is a possibility. I don't
know the answer to that.
I've also seen references in one forum post stating that "NUnit is a
single threaded test harness", or something to that effect.
Thank you for responding.
________________________________
From: Nassar, Anthony [mailto:[email protected]]
Sent: Monday, November 12, 2007 4:00 PM
To: Lisle, Ron
Subject: RE: [Nunit-users] BackgroundWorker problems
Is the work getting done at all? I mean, do you have any evidence (since
the handlers aren't being called) that the work is getting done? Either
way, could it be because there's no message loop? Do you know how many
threads there actually are? Try to debug the test, and open the threads
panel and check.
________________________________
From: [email protected]
[mailto:[email protected]] On Behalf Of Lisle,
Ron
Sent: Monday, November 12, 2007 4:06 PM
To: [email protected]
Subject: [Nunit-users] BackgroundWorker problems
I am trying to run NUnit tests against a form which includes the
System.ComponentModel.BackgroundWorker control. This control exposes 3
events: DoWork, ProgressChanged, and RunWorkerCompleted. During my NUnit
tests, I setup a handler for each, and set a flag to indicate that the
event was called correctly. However, I appear to only get calls into the
DoWork handler, not the other 2. This same code works correctly in my
product.
I've found via google searching a couple similar questions
posted to forums, but no response to those.
I've verified that I don't have try/catch statements along any
of test paths or event handlers.
Any ideas?
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Nunit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nunit-users