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:
- client #1 makes request
- server starts retrieving data client #1
- client #2 makes request while server still responding client #1
- 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
Post a Comment