c# - How to test concurrency scenarios in .NET? -


i've worked concurrency, don't know way test it.

i want know if there way "force" tasks execute in specific order simulate tests cases.

for example:

  1. client #1 makes request
  2. server starts retrieving data client #1
  3. client #2 makes request while server still responding client #1
  4. assert << >>

i have seen people using custom taskschedulers. make sense?

i had had problem in several occurrences too. have created helper can start bunch of threads execute concurrent actions. helper provides synchronization primitives , logging mechanisms. here code fragment unit test:

[test] public void twocodeblocksinparalleltest() {     // static method runs provided action delegates in parallel using threads     ctesthelper.run(         c =>             {                 thread.sleep(1000); // here should code provide                  ctesthelper.addsequencestep("provide"); // record sequence step expectations after test                 ctesthelper.setevent();             },         c =>             {                 ctesthelper.waitevent(); // wait until can consume provided                 ctesthelper.addsequencestep("consume"); // record sequence step expectations after test             },         timespan.fromseconds(10)); // timeout parameter, if threads deadlocked or take long, threads terminated , timeout exception thrown       // after run() completes can analyze if recorded sequence steps in correct order     expect(ctesthelper.getsequence(), is.equalto(new[] { "provide", "consume" })); } 

it can used test client/server or synchronization in components or run thread timeout. i'll continue improve in next weeks. here project page: concurrency testing helper


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -