【问题标题】:Task based timeout, activating slower than expected基于任务的超时,激活比预期慢
【发布时间】:2015-05-22 20:46:53
【问题描述】:

我有一个 HttpMessage,它在某些时候需要一个结果。我希望用户能够等待在设置响应后或超时后返回以太的任务。运行我的单元测试时,它可以工作,但前提是我将超时设置得比响应的延迟设置早得多。

为什么会发生这种情况,我可以做些什么来缩短时间。

*注意下面的单元测试有效,但如果将延迟设置为 500ms,它会失败

HttpMessage 代码:

public async Task<bool> ContinueWhenResponseRecieved(int Timeout)
        {
            NotifyTaskCompletionSource = new TaskCompletionSource<bool>();
            bool result = true;
            //If we have already recieved a response return true;
            if (this._response != null)
            {
                return result;
            }
            //If Timeout is not 0 then start a task that after the timeout period will set the completion source to true no matter what
            if (Timeout != 0) 
            {
                TaskEx.Run(async () =>
                {
                    await TaskEx.Delay(Timeout);
                    result = false;
                    try
                    {
                        NotifyTaskCompletionSource.SetResult(true);
                    }
                    catch (Exception ex) { } //ignore race condition issues
                });
            }
            await NotifyTaskCompletionSource.Task;
            return result;
        }
        public override IHttpMessage setResponse(HttpResponse _response)
        {
            if (NotifyTaskCompletionSource != null)
            {
                try
                {
                    NotifyTaskCompletionSource.SetResult(true);
                }catch(Exception ex){} //Ignore race condtion issues
            }
            return base.setResponse(_response);
        }

单元测试:

  [TestMethod]
    public async Task TestNotifiableTimeOuts()
    {
        var msg = new NotifiableHttpMessage();
        TaskEx.Run(() =>
        {
            TaskEx.Delay(1000).Wait();
            msg.setResponse(new Messages.HttpResponse());
        });
        var result= await msg.ContinueWhenResponseRecieved(50);

        Assert.IsFalse(result);
    }

【问题讨论】:

  • 如果您所做的只是同步等待它们完成,那么使用异步方法而不是同步方法是没有意义的。
  • @Servy 您是否认为由于单元测试会出现这种情况?您不能异步运行单元测试,所以我只是使用 .Wait() 作为解决方法。在生产中,一个人会等待 ContinueWhenResponseRecieved(50) 并且它将异步运行。
  • 如果你想测试异步方法,你应该使用确实支持异步方法的测试框架。那里有很多。也就是说,您也在非测试代码中执行同步操作。
  • @Servy Edited HttpMessage,仍然导致同样的问题。如果我不在 Windows Phone 中工作,我会使用更好的测试框架
  • @Servy 你知道我不知道 msTest 现在支持异步方法。感谢那。上面的问题还是个问题

标签: c# windows-phone-8 timeout async-await task


【解决方案1】:

感谢服务

问题在于单元测试不是异步的,并且导致了问题。一个完全刷新的异步测试就成功了。最终结果单元测试:

[TestMethod]
    public async Task TestNotifiableTimeOuts()
    {
        var msg = new NotifiableHttpMessage();
        TaskEx.Run(async () =>
        {
            await TaskEx.Delay(200);
            msg.setResponse(new Messages.HttpResponse());
        });
        var result= await msg.ContinueWhenResponseRecieved(50);

        Assert.IsFalse(result);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多