Task.WaitAny degenerate case behaviour -
i can't find documentation behaviour of task.waitany()
under following scenarios:
- given empty array of tasks.
- given array of tasks, of 1 has completed.
- given array of tasks, of have completed.
my assumption 2 & 3 return (#3 picking arbitrary index return, first or last 1 depending on implementation detail not guaranteed.) , i'd sorta expect 1 return true?
some brief unit tests seem confirm this... following pass. if can find formal documentation of behaviour, i'd glad see it. (even if improve ability locate myself next time!)
using system; using system.collections.generic; using system.linq; using system.threading.tasks; using nunit.framework; using system.threading; namespace tantest.threading { [testfixture] public class metatasktests { private const int safetimeoutmillisecs = 1000; [test, timeout(safetimeoutmillisecs)] public void meta_newtaskcompletes() { var task = new task(() => { }); task.start(); thread.sleep(10); assert.that(task.iscompleted, is.true); } [test, timeout(safetimeoutmillisecs)] public void meta_emptysetoftasks_waitsproceed() { var tasks = new list<task>(); task.waitany(tasks.toarray()); task.waitall(tasks.toarray()); } [test, timeout(safetimeoutmillisecs)] public void meta_emptysetoftasks_waitsgiveexpectedoutputs() { var tasks = new list<task>(); assert.that(task.waitany(tasks.toarray(), timespan.frommilliseconds(100)), is.equalto(-1)); assert.that(task.waitall(tasks.toarray(), timespan.frommilliseconds(100)), is.true); } [test, timeout(safetimeoutmillisecs)] public void meta_setofcompletedtasks_waitsproceed() { var tasks = new list<task> { new task(() => { }), new task(() => { }) }; tasks.foreach(task => task.start()); thread.sleep(10); task.waitany(tasks.toarray()); task.waitall(tasks.toarray()); } [test, timeout(safetimeoutmillisecs)] public void meta_setofcompletedtasks_waitsgiveexpectedoutputs() { var tasks = new list<task> { new task(() => { }), new task(() => { }) }; tasks.foreach(task => task.start()); thread.sleep(10); assert.that(task.waitany(tasks.toarray(), timespan.frommilliseconds(100)), is.greaterthan(-1)); assert.that(task.waitall(tasks.toarray(), timespan.frommilliseconds(100)), is.true); } [test, timeout(safetimeoutmillisecs)] public void meta_setofunstartedtasks_waitsdontproceed() { var tasks = new list<task> { new task(() => { }), new task(() => { }) }; thread.sleep(10); assert.that(task.waitany(tasks.toarray(), timespan.frommilliseconds(100)), is.equalto(-1)); assert.that(task.waitall(tasks.toarray(), timespan.frommilliseconds(100)), is.false); } [test, timeout(safetimeoutmillisecs)] public void meta_setoftaskswithonecompleted_anyproceedsandoutputsindex() { var tasks = new list<task> { new task(() => { }), new task(() => { }) }; tasks.first().start(); thread.sleep(10); assert.that(task.waitany(tasks.toarray()), is.greaterthan(-1)); } [test, timeout(safetimeoutmillisecs)] public void meta_setoftaskswithonecompleted_alltimesoutwithexpectedoutput() { var tasks = new list<task> { new task(() => { }), new task(() => { }) }; tasks.first().start(); thread.sleep(10); assert.that(task.waitall(tasks.toarray(), timespan.frommilliseconds(100)), is.false); } } }
Comments
Post a Comment