File: timeout_handler.h

package info (click to toggle)
cpprest 2.10.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,916 kB
  • sloc: cpp: 71,086; sh: 275; makefile: 170; javascript: 147
file content (57 lines) | stat: -rw-r--r-- 1,405 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * Simple utility for handling timeouts with http client test cases.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#pragma once

#include "cpprest/http_client.h"

namespace tests
{
namespace functional
{
namespace http
{
namespace client
{
// helper function to check if failure is due to timeout.
inline bool is_timeout(const std::string& msg)
{
    if (msg.find("The operation timed out") != std::string::npos /* WinHTTP */ ||
        msg.find("The operation was timed out") != std::string::npos /* IXmlHttpRequest2 */)
    {
        return true;
    }
    return false;
}

template<typename Func>
void handle_timeout(const Func& f)
{
    try
    {
        f();
    }
    catch (const web::http::http_exception& e)
    {
        if (is_timeout(e.what()))
        {
            // Since this test depends on an outside server sometimes it sporadically can fail due to timeouts
            // especially on our build machines.
            return;
        }
        throw;
    }
}

} // namespace client
} // namespace http
} // namespace functional
} // namespace tests