Skip to content

Commit 44b2d22

Browse files
authored
DataCallbackTest fv network test. (#1041)
* DataCallbackTest fv network test. Add new DataCallbackTest network test. Test goal is to download some big data in chunks and check data_callback works correctly. For windows, data_callback offset is always 0, so fixing the bug too. Resolves: OLPEDGE-2065 Signed-off-by: Kostiantyn Zvieriev <ext-kostiantyn.zvieriev@here.com>
1 parent 25b3cd6 commit 44b2d22

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

olp-cpp-sdk-core/src/http/winhttp/NetworkWinHttp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ void NetworkWinHttp::RequestCallback(HINTERNET, DWORD_PTR context, DWORD status,
838838
OLP_SDK_LOG_TRACE(kLogTag, "Received " << data_len << " bytes for id="
839839
<< handle->request_id);
840840
if (data_len) {
841-
std::uint64_t total_offset = 0;
841+
std::uint64_t total_offset = request_result.count;
842842

843843
if (handle->data_callback) {
844844
handle->data_callback((const uint8_t*)data_buffer, total_offset,

tests/common/ReadDefaultResponses.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ class ReadDefaultResponses {
7171
return partitions;
7272
}
7373

74-
static std::string GenerateData() {
75-
std::string result =
74+
static std::string GenerateData(size_t length = 64u) {
75+
std::string letters =
7676
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
7777
std::random_device device;
7878
std::mt19937 generator(device());
79-
std::shuffle(result.begin(), result.end(), generator);
79+
std::uniform_int_distribution<unsigned int> dis(0u, letters.length() - 1);
80+
81+
std::string result;
82+
result.resize(length);
83+
for (auto i = 0u; i < length; ++i) {
84+
result[i] += letters[dis(generator)];
85+
}
8086

8187
return result;
8288
}

tests/functional/network/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
set(OLP_SDK_NETWORK_TESTS_SOURCES
2020
./ConcurrencyTest.cpp
21+
./DataCallbackTest.cpp
2122
./DestructionTest.cpp
2223
./NetworkTestBase.cpp
2324
./TimeoutTest.cpp
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 NetworkSettings = olp::http::NetworkSettings;
34+
35+
using DataCallbackTest = NetworkTestBase;
36+
37+
TEST_F(DataCallbackTest, DataChunks) {
38+
const std::string kUrlBase = "https://some-url.com";
39+
const std::string kApiBase = "/some-api";
40+
41+
// generate 1mb of data
42+
constexpr auto data_size = 1u * 1024u * 1024u;
43+
auto data = mockserver::ReadDefaultResponses::GenerateData(data_size);
44+
auto payload = std::make_shared<std::stringstream>();
45+
std::vector<uint8_t> chunk_data(data_size);
46+
47+
// we expect multiple data chunks are received in the test.
48+
int chunk_count = 0;
49+
50+
mock_server_client_->MockResponse("GET", kApiBase, data);
51+
52+
const auto url = kUrlBase + kApiBase;
53+
const auto request = NetworkRequest(url).WithSettings(settings_).WithVerb(
54+
olp::http::NetworkRequest::HttpVerb::GET);
55+
56+
std::promise<NetworkResponse> promise;
57+
const auto outcome = network_->Send(
58+
request, payload,
59+
[&promise](NetworkResponse response) {
60+
promise.set_value(std::move(response));
61+
},
62+
nullptr,
63+
[&chunk_data, &chunk_count](const std::uint8_t* data,
64+
std::uint64_t offset, std::size_t length) {
65+
std::copy(data, data + length, &chunk_data[offset]);
66+
++chunk_count;
67+
});
68+
ASSERT_TRUE(outcome.IsSuccessful());
69+
70+
auto future = promise.get_future();
71+
const auto response = future.get();
72+
const auto& payload_data = payload->str();
73+
74+
ASSERT_EQ(response.GetStatus(), olp::http::HttpStatusCode::OK);
75+
ASSERT_GT(chunk_count, 2);
76+
EXPECT_TRUE(std::equal(data.begin(), data.end(), chunk_data.begin()));
77+
EXPECT_EQ(data, payload_data);
78+
}
79+
80+
} // namespace

0 commit comments

Comments
 (0)