Skip to content

Commit d97dfcf

Browse files
author
Liubov Didkivska
authored
Add expiration cache test for lookup api. (#743)
* Add expiration cache test for lookup api. Create mock cache. Run get data for VersionedLayerClient. Check if lookup api data are stored in cache with expiration time equal 1 hour(3600 seconds). Write core test to check cache expiration. Write data with expiration 1 second. Get data from cache to check if data was stored corectly. Wait 2 seconds and try to get data again. Data should be expired. Relates-To: OLPEDGE-1612 Signed-off-by: Liubov Didkivska <[email protected]>
1 parent aad52cd commit d97dfcf

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

tests/integration/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(OLP_SDK_INTEGRATIONAL_TESTS_SOURCES
3333
./olp-cpp-sdk-dataservice-write/StreamLayerClientTest.cpp
3434
./olp-cpp-sdk-dataservice-write/VolatileLayerClientTest.cpp
3535
./olp-cpp-sdk-core/DefaultNetworkTest.cpp
36+
./olp-cpp-sdk-core/DefaultCacheTest.cpp
3637
)
3738

3839
if (ANDROID OR IOS)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 <gtest/gtest.h>
21+
#include <thread>
22+
23+
#include <olp/core/cache/DefaultCache.h>
24+
25+
namespace {
26+
using namespace olp::cache;
27+
28+
TEST(DefaultCacheTest, DataExpiration) {
29+
const std::string content_key = "test_key";
30+
CacheSettings settings;
31+
settings.max_memory_cache_size = 5; // bytes
32+
settings.disk_path_mutable = "./cache";
33+
const auto expire_time = 1;
34+
35+
{
36+
SCOPED_TRACE("Create a disk cache, write data.");
37+
DefaultCache cache(settings);
38+
EXPECT_EQ(cache.Open(), DefaultCache::StorageOpenResult::Success);
39+
40+
const std::string content = "12345";
41+
auto buffer = std::make_shared<std::vector<unsigned char>>(
42+
std::begin(content), std::end(content));
43+
EXPECT_TRUE(cache.Put(content_key, buffer, expire_time));
44+
// check if data is in cache
45+
auto buffer_out_1 = cache.Get(content_key);
46+
ASSERT_NE(buffer_out_1, nullptr);
47+
cache.Close();
48+
}
49+
50+
std::this_thread::sleep_for(std::chrono::seconds(expire_time + 1));
51+
52+
{
53+
SCOPED_TRACE("Check that data is expired after timeout.");
54+
DefaultCache cache(settings);
55+
EXPECT_EQ(cache.Open(), DefaultCache::StorageOpenResult::Success);
56+
57+
auto buffer = cache.Get(content_key);
58+
ASSERT_EQ(buffer, nullptr);
59+
60+
cache.Close();
61+
}
62+
}
63+
64+
} // namespace

tests/integration/olp-cpp-sdk-dataservice-read/VersionedLayerClientTest.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
#include <string>
2323

2424
#include <matchers/NetworkUrlMatchers.h>
25+
#include <mocks/CacheMock.h>
2526
#include <mocks/NetworkMock.h>
2627
#include <olp/authentication/Settings.h>
28+
#include <olp/core/cache/CacheSettings.h>
29+
#include <olp/core/cache/KeyValueCache.h>
2730
#include <olp/core/client/OlpClientSettings.h>
2831
#include <olp/core/client/OlpClientSettingsFactory.h>
2932
#include <olp/core/porting/make_unique.h>
3033
#include <olp/dataservice/read/VersionedLayerClient.h>
34+
#include <olp/dataservice/read/model/Partitions.h>
3135

3236
#include "HttpResponses.h"
3337

@@ -2912,4 +2916,76 @@ TEST_F(DataserviceReadVersionedLayerClientTest, RemoveFromCacheTileKey) {
29122916
ASSERT_FALSE(response.IsSuccessful());
29132917
}
29142918

2919+
TEST_F(DataserviceReadVersionedLayerClientTest, CheckLookupApiCacheExpiration) {
2920+
olp::client::HRN hrn(GetTestCatalog());
2921+
2922+
// initialize mock cache
2923+
auto cache = std::make_shared<testing::StrictMock<CacheMock>>();
2924+
settings_->cache = cache;
2925+
2926+
auto client = olp::dataservice::read::VersionedLayerClient(
2927+
hrn, "testlayer", 4, *settings_);
2928+
2929+
// check if expiration time is 1 hour(3600 sec)
2930+
time_t expiration_time = 3600;
2931+
EXPECT_CALL(*cache, Get("hrn:here:data::olp-here-test:here-optimized-map-for-"
2932+
"visualization-2::query::v1::api",
2933+
_))
2934+
.Times(1)
2935+
.WillOnce(Return(boost::any()));
2936+
EXPECT_CALL(*cache, Put("hrn:here:data::olp-here-test:here-optimized-map-for-"
2937+
"visualization-2::query::v1::api",
2938+
_, _, expiration_time))
2939+
.Times(1)
2940+
.WillOnce(Return(true));
2941+
EXPECT_CALL(*cache, Get("hrn:here:data::olp-here-test:here-optimized-map-for-"
2942+
"visualization-2::blob::v1::api",
2943+
_))
2944+
.Times(1)
2945+
.WillOnce(Return(boost::any()));
2946+
EXPECT_CALL(*cache, Put("hrn:here:data::olp-here-test:here-optimized-map-for-"
2947+
"visualization-2::blob::v1::api",
2948+
_, _, expiration_time))
2949+
.Times(1)
2950+
.WillOnce(Return(true));
2951+
2952+
EXPECT_CALL(*cache, Get("hrn:here:data::olp-here-test:here-optimized-map-for-"
2953+
"visualization-2::testlayer::269::4::partition",
2954+
_))
2955+
.Times(1)
2956+
.WillOnce(Return(boost::any()));
2957+
EXPECT_CALL(*cache, Put("hrn:here:data::olp-here-test:here-optimized-map-for-"
2958+
"visualization-2::testlayer::269::4::partition",
2959+
_, _, _))
2960+
.Times(1)
2961+
.WillOnce(Return(true));
2962+
2963+
EXPECT_CALL(
2964+
*cache,
2965+
Get("hrn:here:data::olp-here-test:here-optimized-map-for-visualization-2:"
2966+
":testlayer::4eed6ed1-0d32-43b9-ae79-043cb4256432::Data"))
2967+
.Times(1)
2968+
.WillOnce(Return(nullptr));
2969+
EXPECT_CALL(
2970+
*cache,
2971+
Put("hrn:here:data::olp-here-test:here-optimized-map-for-visualization-2:"
2972+
":testlayer::4eed6ed1-0d32-43b9-ae79-043cb4256432::Data",
2973+
_, _))
2974+
.Times(1)
2975+
.WillOnce(Return(true));
2976+
2977+
auto request = olp::dataservice::read::DataRequest().WithPartitionId("269").WithFetchOption(OnlineIfNotFound);
2978+
auto future = client.GetData(request);
2979+
2980+
auto data_response = future.GetFuture().get();
2981+
2982+
ASSERT_TRUE(data_response.IsSuccessful())
2983+
<< ApiErrorToString(data_response.GetError());
2984+
ASSERT_LT(0, data_response.GetResult()->size());
2985+
std::string data_string(data_response.GetResult()->begin(),
2986+
data_response.GetResult()->end());
2987+
ASSERT_EQ("DT_2_0031", data_string);
2988+
2989+
}
2990+
29152991
} // namespace

0 commit comments

Comments
 (0)