<p>this kind of follows on from another <a href="https://stackoverflow.com/questions/41290/file-access-strategy-in-a-multi-threaded-environment-web-app">question</a> of mine.</p>
<p>Basically, once I have the code to access the file (will review the answers there in a minute) what would be the best way to <strong>test</strong> it?</p>
<p>I am thinking of creating a method which just spawns lots of <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="noreferrer">BackgroundWorker</a>'s or something and tells them all load/save the file, and test with varying file/object sizes. Then, get a response back from the threads to see if it failed/succeeded/made the world implode etc.</p>
<p>Can you  guys offer any suggestions on the best way to approach this? As I said before, this is all kinda new to me :)</p>
<h3>Edit</h3>
<p>Following <a href="https://stackoverflow.com/questions/41568/whats-the-best-way-to-test-a-method-from-multiple-threads#41572">ajmastrean's</a> post:</p>
<p>I am using a console app to test with Debug.Asserts :)</p>
<hr />
<h2>Update</h2>
<p>I originally rolled with using <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="noreferrer">BackgroundWorker</a> to deal with the threading (since I am used to that from Windows dev) I soon realised that when I was performing tests where multiple ops (threads) needed to complete before continuing, I realised it was going to be a bit of a hack to get it to do this.</p>
<p>I then followed up on <a href="https://stackoverflow.com/questions/41568/whats-the-best-way-to-unit-test-from-multiple-threads#41589">ajmastrean</a>'s post and realised I should really be using the <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread(VS.80).aspx" rel="noreferrer">Thread</a> class for working with concurrent operations. I will now refactor using this method (albeit a different approach).</p>

[{'answer_id': 41578, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': -1, 'selected': False, 'text': '<p>Your idea should work fine.  Basically you just want to spawn a bunch of threads, and make sure the ones writing the file take long enough to do it to actually make the readers wait.  If all of your threads return without error, and without blocking forever, then the test succeeds.</p>\n'}, {'answer_id': 41589, 'author': 'Anthony Mastrean', 'author_id': 3619, 'author_profile': 'https://Stackoverflow.com/users/3619', 'pm_score': 5, 'selected': True, 'text': '<p>In .NET, <code>ThreadPool</code> threads won\'t return without setting up <code>ManualResetEvent</code>s or <code>AutoResetEvent</code>s. I find these overkill for a quick test method (not to mention kind of complicated to create, set, and manage). Background worker is a also a bit complex with the callbacks and such.</p>\n\n<p>Something I have found that works is </p>\n\n<ol>\n<li>Create an array of threads.</li>\n<li>Setup the <code>ThreadStart</code> method of each thread.</li>\n<li>Start each thread.</li>\n<li>Join on all threads (blocks the current thread until all other threads complete or abort)  </li>\n</ol>\n\n\n\n<pre class="lang-cs prettyprint-override"><code>public static void MultiThreadedTest()\n{\n    Thread[] threads = new Thread[count];\n\n    for (int i = 0; i &lt; threads.Length; i++)\n    {\n        threads[i] = new Thread(DoSomeWork());\n    }\n\n    foreach(Thread thread in threads)\n    {\n        thread.Start();\n    }\n\n    foreach(Thread thread in threads)\n    {\n        thread.Join();\n    }\n}\n</code></pre>\n'}, {'answer_id': 41619, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 1, 'selected': False, 'text': "<p>@ajmastrean, since unit test result must be predictable we need to synchronize threads somehow. I can't see a simple way to do it without using events.</p>\n<p>I found that <code>ThreadPool.QueueUserWorkItem</code> gives me an easy way to test such use cases</p>\n<pre><code> ThreadPool.QueueUserWorkItem(x =&gt; { \n    File.Open(fileName, FileMode.Open);\n    event1.Set(); // Start 2nd tread;\n    event2.WaitOne(); // Blocking the file;\n});\nThreadPool.QueueUserWorkItem(x =&gt; { \n    try\n    {\n        event1.WaitOne(); // Waiting until 1st thread open file\n        File.Delete(fileName); // Simulating conflict\n    }\n    catch (IOException e)\n    {\n        Debug.Write(&quot;File access denied&quot;);\n    }\n});\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/41568', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/832/']