Skip to content

Commit 7236f34

Browse files
authored
Concurrency network test (#1022)
Adding fv network test. Test goal is to check network requests are concurrent. We make several requests, first and last one are delayed, so their responses received in the end of the test. Resolves: OLPEDGE-2060 Signed-off-by: Kostiantyn Zvieriev <ext-kostiantyn.zvieriev@here.com>
1 parent 0a6f089 commit 7236f34

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

tests/functional/network/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818

1919
set(OLP_SDK_NETWORK_TESTS_SOURCES
20-
./NetworkTestBase.cpp
20+
./ConcurrencyTest.cpp
2121
./DestructionTest.cpp
22+
./NetworkTestBase.cpp
2223
./TimeoutTest.cpp
2324
)
2425

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <memory>
21+
22+
#include <gtest/gtest.h>
23+
#include <olp/core/client/OlpClientSettingsFactory.h>
24+
#include <olp/core/http/Network.h>
25+
#include <olp/core/http/NetworkSettings.h>
26+
27+
#include "NetworkTestBase.h"
28+
#include "ReadDefaultResponses.h"
29+
30+
namespace {
31+
using NetworkRequest = olp::http::NetworkRequest;
32+
using NetworkResponse = olp::http::NetworkResponse;
33+
using RequestId = olp::http::RequestId;
34+
35+
const std::string kUrlBase = "https://some-url.com";
36+
const std::string kApiBase = "/some-api/";
37+
constexpr auto kTimeout = std::chrono::seconds(3);
38+
39+
class ConcurrencyTest : public NetworkTestBase {
40+
public:
41+
void AddExpectation(int i, boost::optional<int32_t> delay_ms = boost::none) {
42+
const auto url = kApiBase + std::to_string(i);
43+
mock_server_client_->MockResponse(
44+
"GET", url, mockserver::ReadDefaultResponses::GenerateData(), 200, true,
45+
delay_ms);
46+
}
47+
48+
RequestId SendRequest(int i) {
49+
const auto url = kUrlBase + kApiBase + std::to_string(i);
50+
const auto request = NetworkRequest(url).WithSettings(settings_).WithVerb(
51+
olp::http::NetworkRequest::HttpVerb::GET);
52+
const auto outcome =
53+
network_->Send(request, nullptr,
54+
std::bind(&ConcurrencyTest::ResponseCallback, this,
55+
std::placeholders::_1));
56+
57+
EXPECT_TRUE(outcome.IsSuccessful());
58+
return outcome.GetRequestId();
59+
}
60+
61+
void ResponseCallback(NetworkResponse response) {
62+
std::unique_lock<std::mutex> lock(result_mutex_);
63+
const auto id = response.GetRequestId();
64+
responses_.push_back(id);
65+
finish_cv_.notify_one();
66+
}
67+
68+
protected:
69+
std::mutex result_mutex_;
70+
std::condition_variable finish_cv_;
71+
std::vector<RequestId> responses_;
72+
};
73+
74+
TEST_F(ConcurrencyTest, ResponseDelay) {
75+
constexpr auto kRequestCount = 10;
76+
77+
// setup network expectation
78+
// first and last requests are 500ms delayed
79+
AddExpectation(0, 500);
80+
for (int i = 1; i < kRequestCount - 1; ++i) {
81+
AddExpectation(i);
82+
}
83+
AddExpectation(kRequestCount - 1, 500);
84+
85+
auto first_request_id = SendRequest(0);
86+
for (int i = 1; i < kRequestCount - 1; ++i) {
87+
SendRequest(i);
88+
}
89+
auto last_request_id = SendRequest(kRequestCount - 1);
90+
91+
{
92+
std::unique_lock<std::mutex> lock(result_mutex_);
93+
ASSERT_TRUE(finish_cv_.wait_for(
94+
lock, kTimeout, [&]() { return responses_.size() == kRequestCount; }));
95+
}
96+
ASSERT_EQ(responses_.size(), kRequestCount);
97+
ASSERT_TRUE(responses_[kRequestCount - 2] == first_request_id ||
98+
responses_[kRequestCount - 2] == last_request_id);
99+
ASSERT_TRUE(responses_[kRequestCount - 1] == first_request_id ||
100+
responses_[kRequestCount - 1] == last_request_id);
101+
}
102+
103+
} // namespace

0 commit comments

Comments
 (0)