当ThreadPool.QueueUserWorkItem返回false时

时间:2020-03-06 14:53:47  来源:igfitidea点击:

MSDN指出该方法返回

true if the method is successfully
  queued; NotSupportedException is
  thrown if the work item is not queued.

出于测试目的,如何获取返回" false"的方法?还是仅仅是"次优"的类设计?

解决方案

这可能是"保留以供将来使用"的情况。我们可能希望将其视为失败,但是很难进行测试。

我几乎将此方法视为void / Sub。

可以想象,当任务并行库(TPL)到来时,整个API(线程池)已过时。

在Reflector中查看源代码时,似乎唯一可以返回" false"的部分是对以下内容的调用:

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool AdjustThreadsInPool(uint QueueLength);

true if the method is successfully queued; NotSupportedException is thrown if the work item is not queued.

以与处理NotSupportedException相同的方式来处理return false。

要使其返回false,请使用模拟方法或者对象。
我们想测试自己编写的代码,而不是基础的Windows代码。我敢肯定,微软已经为此做了很多自己的测试。