简体   繁体   English

文档列表API客户端用于C#超时

[英]Documents List API client for C# timing out

I'm working on a simple wrapper for the google docs api in c#. 我正在为c#中的google docs api编写一个简单的包装器。 The problem I'm running into is my tests are timing out. 我遇到的问题是我的测试超时了。 Sometimes. 有时。 When I run all of my tests (only 12 of them) then it usually hangs up on the 8th one, which is testing the delete function. 当我运行我的所有测试(其中只有12个)时,它通常会挂起第8个,即测试删除功能。 After about 6.5 minutes, it continues on, but every test after it also times out after 6.5 minutes for each test. 大约6.5分钟后,它继续,但每次测试后,每次测试后的每次测试都会在6.5分钟后超时。 If I run the tests individually then it works fine every time. 如果我单独运行测试,那么每次都可以正常运行。

Here is the first method that times out: 这是超时的第一种方法:

Updated to show exception handling 已更新以显示异常处理

[TestMethod]
public void CanDeleteFile()
{
    var api = CreateApi();
    api.UploadFile("pic.jpg", "..\\..\\..\\pic.jpg", "image/jpeg");
    try
    {
        var files = api.GetDocuments();
        api.DeleteFile("pic.jpg");
        var lessFiles = api.GetDocuments();
        Assert.AreEqual(files.Count - 1, lessFiles.Count);
    }
    catch (Google.GData.Client.GDataRequestException ex)
    {
        using (StreamWriter writer = new StreamWriter("..\\..\\..\\errors.log", true))
        {
            string time = DateTime.Now.ToString();
            writer.WriteLine(time + ":\r\n\t" + ex.ResponseString);
        }
        throw ex;
    }

}

It times out on var lessFiles = api.GetDocuments(); 它在var lessFiles = api.GetDocuments(); The second call to that method. 第二次调用该方法。 I have other methods that call that method twice, and they don't time out, but this one does. 我有其他方法调用该方法两次,并且它们没有超时,但是这个方法确实如此。

The method that all the test methods use that times out: 所有测试方法使用的方法超时:

    public AtomEntryCollection GetDocuments(DocumentType type = DocumentType.All, bool includeFolders = false)
    {
        checkToken();
        DocumentsListQuery query = getQueryByType(type);
        query.ShowFolders = includeFolders;
        DocumentsFeed feed = service.Query(query);
        return feed.Entries;
    }

It times out on this line DocumentsFeed feed = service.Query(query); 它在这一行超时DocumentsFeed feed = service.Query(query); . This would be closer to acceptable if I was requesting insane numbers of files. 如果我要求疯狂数量的文件,这将更接近可接受。 I'm not. 我不是。 I'm requesting 5 - 6 depending on what test I'm running. 我要求5 - 6取决于我正在运行的测试。

Things I've tried: 我试过的事情:

  • Deleting all files from my google docs account, leaving only 1-2 files depending on the test for it to retrieve. 从我的google docs帐户中删除所有文件,只留下1-2个文件,具体取决于要检索的测试。
  • Running the tests individually (they all pass and nothing times out, but I shouldn't have to do this) 单独运行测试(它们都通过,没有时间,但我不应该这样做)
  • Checking my network speed to make sure it's not horribly slow (15mbps down 4.5mbps up) 检查我的网络速度,以确保它不是非常慢(15mbps下降4.5mbps)

I'm out of ideas. 我没有想法。 If anyone knows why it might start timing out on me? 如果有人知道为什么它可能会开始超时我? Any suggestions are welcome. 欢迎任何建议。

edit 编辑

As @gowansg suggested, I implemented exponential backoff in my code. 正如@gowansg建议的那样,我在代码中实现了指数退避。 It started failing at the same point with the same exception. 它开始在同一点失败,但同样的例外。 I then wrote a test to send 10000 requests for a complete list of all documents in my drive. 然后,我编写了一个测试,发送10000个请求,以获取驱动器中所有文档的完整列表。 It passed without any issues without using exponential backoff. 没有使用指数退避就没有任何问题。 Next I modified my test class so it would keep track of how many requests were sent. 接下来,我修改了我的测试类,以便跟踪发送的请求数。 My tests crash on request 11. 我的测试因请求11而崩溃。

The complete exception: 完整的例外情况:

Google.GData.Client.GDataRequestException was unhandled by user code
Message=Execution of request failed: https://docs.google.com/feeds/default/private/full
Source=GoogleDrive
StackTrace:
     at GoogleDrive.GoogleDriveApi.GetDocuments(DocumentType type, Boolean includeFolders) in C:\Users\nlong\Desktop\projects\GoogleDrive\GoogleDrive\GoogleDriveApi.cs:line 105
     at GoogleDrive.Test.GoogleDriveTests.CanDeleteFile() in C:\Users\nlong\Desktop\projects\GoogleDrive\GoogleDrive.Test\GoogleDriveTests.cs:line 77
InnerException: System.Net.WebException
     Message=The operation has timed out
     Source=System
     StackTrace:
          at System.Net.HttpWebRequest.GetResponse()
          at Google.GData.Client.GDataRequest.Execute()
     InnerException: 

another edit 另一个编辑

It seems that I only crash after requesting the number of documents after the second upload. 似乎我在第二次上传后请求了多少文件后才崩溃。 I'm not sure why that is, but I'm definitely going to look into my upload method. 我不确定为什么会这样,但我肯定会查看我的上传方法。

If your tests pass when ran individually, but not when ran consecutively then you may be hitting a request rate limit. 如果您的测试在单独运行时通过,而不是在连续运行时通过,那么您可能会达到请求速率限制。 I noticed in your comment to JotaBe's answer you mentioned were getting request timeout exceptions. 我在你的评论中注意到你提到的JotaBe的答案是获得请求超时异常。 You should take a look at the http status code to figure out what to do . 您应该查看http状态代码以确定要执行的操作 In the case of a 503 you should implement exceptional back off . 在503的情况下,你应该实现特殊的退避

Updated Suggestion 更新了建议

Place a try-catch around the line that is throwing the exception and catch the Google.GData.Client.GDataRequestException . 在抛出异常的行周围放置一个try-catch并捕获Google.GData.Client.GDataRequestException According to the source there are two properties that may be of value to you: 根据消息来源 ,有两个属性可能对您有价值:

    /// <summary>
    /// this is the error message returned by the server
    /// </summary>
    public string ResponseString
    { ... }

and

    //////////////////////////////////////////////////////////////////////
    /// <summary>Read only accessor for response</summary>
    //////////////////////////////////////////////////////////////////////
    public WebResponse Response
    { ... }

Hopefully these contain some useful information for you, eg, an HTTP Status Code. 希望这些包含一些有用的信息,例如HTTP状态代码。

You can explicitly set the time-outs for your unit tests. 您可以明确设置单元测试的超时。 Here you have extensive information on it: 在这里您可以获得大量信息:

You can include Thread.Sleep(miliseconds) in your unit tests before the offending methods. 您可以在违规方法之前在单元测试中包含Thread.Sleep(miliseconds) Probably your requests are being rejected from google docs for being too may in too short a time. 可能你的请求被谷歌文档拒绝,因为太长时间也可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM