Skip to content

Commit 169ec48

Browse files
committed
Added retry logic to outside_ssl_json
1 parent 3fa4456 commit 169ec48

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

Release/tests/functional/http/client/outside_tests.cpp

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "stdafx.h"
2727
#include "cpprest/rawptrstream.h"
28+
#include "os_utilities.h"
2829
#include <stdexcept>
2930

3031
using namespace web;
@@ -33,6 +34,7 @@ using namespace concurrency;
3334
using namespace web::http;
3435
using namespace web::http::client;
3536

37+
using namespace tests::common::utilities;
3638
using namespace tests::functional::http::utilities;
3739

3840
namespace tests { namespace functional { namespace http { namespace client {
@@ -163,37 +165,53 @@ TEST_FIXTURE(uri_address, outside_ssl_json)
163165

164166
// Send request
165167
web::http::client::http_client playlistClient(playlistUri.to_uri());
166-
playlistClient.request(methods::GET).then([=](http_response playlistResponse) -> pplx::task<json::value>
167-
{
168-
return playlistResponse.extract_json();
169-
}).then([=](json::value v)
170-
{
171-
int count = 0;
172-
auto& obj = v.as_object();
173-
174-
VERIFY_ARE_NOT_EQUAL(obj.find(U("pageInfo")), obj.end());
175-
VERIFY_ARE_NOT_EQUAL(obj.find(U("items")), obj.end());
176-
177-
auto& items = obj[U("items")];
178168

179-
for(auto iter = items.as_array().cbegin(); iter != items.as_array().cend(); ++iter)
169+
// Retry up to 4 times.
170+
for (int i = 0; i < 4; ++i)
171+
{
172+
try
180173
{
181-
const auto& i = *iter;
182-
auto iSnippet = i.as_object().find(U("snippet"));
183-
if (iSnippet == i.as_object().end())
174+
playlistClient.request(methods::GET).then([=](http_response playlistResponse) -> pplx::task < json::value >
184175
{
185-
throw std::runtime_error("snippet key not found");
186-
}
187-
auto iTitle = iSnippet->second.as_object().find(U("title"));
188-
if (iTitle == iSnippet->second.as_object().end())
176+
return playlistResponse.extract_json();
177+
}).then([=](json::value v)
189178
{
190-
throw std::runtime_error("title key not found");
191-
}
192-
auto name = iTitle->second.serialize();
193-
count++;
179+
int count = 0;
180+
auto& obj = v.as_object();
181+
182+
VERIFY_ARE_NOT_EQUAL(obj.find(U("pageInfo")), obj.end());
183+
VERIFY_ARE_NOT_EQUAL(obj.find(U("items")), obj.end());
184+
185+
auto& items = obj[U("items")];
186+
187+
for (auto iter = items.as_array().cbegin(); iter != items.as_array().cend(); ++iter)
188+
{
189+
const auto& i = *iter;
190+
auto iSnippet = i.as_object().find(U("snippet"));
191+
if (iSnippet == i.as_object().end())
192+
{
193+
throw std::runtime_error("snippet key not found");
194+
}
195+
auto iTitle = iSnippet->second.as_object().find(U("title"));
196+
if (iTitle == iSnippet->second.as_object().end())
197+
{
198+
throw std::runtime_error("title key not found");
199+
}
200+
auto name = iTitle->second.serialize();
201+
count++;
202+
}
203+
VERIFY_ARE_EQUAL(3, count); // Update this accordingly, if the number of items changes
204+
}).wait();
205+
break;
194206
}
195-
VERIFY_ARE_EQUAL(3, count); // Update this accordingly, if the number of items changes
196-
}).wait();
207+
catch (web::http::http_exception const& e)
208+
{
209+
if (e.what() != "Error in: WinHttpQueryDataAvailable" || i == 3)
210+
// If we didn't get a "connection broken" error (or we are on the last retry), rethrow it
211+
throw;
212+
os_utilities::sleep(1000);
213+
}
214+
}
197215
}
198216

199217
} // SUITE(outside_tests)

0 commit comments

Comments
 (0)