|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include "mock.h" |
| 3 | +#include "utils.h" |
| 4 | +#include "configservice.h" |
| 5 | +#include "configcat/configcatoptions.h" |
| 6 | +#include "configcat/configcatclient.h" |
| 7 | + |
| 8 | + |
| 9 | +using namespace configcat; |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +TEST(ConfigCacheTest, CacheKey) { |
| 13 | + EXPECT_EQ("147c5b4c2b2d7c77e1605b1a4309f0ea6684a0c6", ConfigService::generateCacheKey("test1")); |
| 14 | + EXPECT_EQ("c09513b1756de9e4bc48815ec7a142b2441ed4d5", ConfigService::generateCacheKey("test2")); |
| 15 | +} |
| 16 | + |
| 17 | +TEST(ConfigCacheTest, CachePayload) { |
| 18 | + double nowInSeconds = 1686756435.8449; |
| 19 | + std::string etag = "test-etag"; |
| 20 | + ConfigEntry entry(Config::fromJson(kTestJsonString), etag, kTestJsonString, nowInSeconds); |
| 21 | + EXPECT_EQ("1686756435844\n" + etag + "\n" + kTestJsonString, entry.serialize()); |
| 22 | +} |
| 23 | + |
| 24 | +TEST(ConfigCatTest, InvalidCacheContent) { |
| 25 | + static constexpr char kTestJsonFormat[] = R"({ "f": { "testKey": { "v": %s, "p": [], "r": [] } } })"; |
| 26 | + HookCallbacks hookCallbacks; |
| 27 | + auto hooks = make_shared<Hooks>(); |
| 28 | + hooks->addOnError([&](const string& error) { hookCallbacks.onError(error); }); |
| 29 | + auto configJsonString = string_format(kTestJsonFormat, R"("test")"); |
| 30 | + auto configCache = make_shared<SingleValueCache>(ConfigEntry( |
| 31 | + Config::fromJson(configJsonString), |
| 32 | + "test-etag", |
| 33 | + configJsonString, |
| 34 | + getUtcNowSecondsSinceEpoch()).serialize() |
| 35 | + ); |
| 36 | + |
| 37 | + ConfigCatOptions options; |
| 38 | + options.pollingMode = PollingMode::manualPoll(); |
| 39 | + options.configCache = configCache; |
| 40 | + options.hooks = hooks; |
| 41 | + auto client = ConfigCatClient::get("test", &options); |
| 42 | + |
| 43 | + EXPECT_EQ("test", client->getValue("testKey", "default")); |
| 44 | + EXPECT_EQ(0, hookCallbacks.errorCallCount); |
| 45 | + |
| 46 | + // Invalid fetch time in cache |
| 47 | + configCache->value = "text\n"s + "test-etag\n" + string_format(kTestJsonFormat, R"("test2")"); |
| 48 | + EXPECT_EQ("test", client->getValue("testKey", "default")); |
| 49 | + EXPECT_TRUE(hookCallbacks.error.find("Error occurred while reading the cache. Invalid fetch time: text") != std::string::npos); |
| 50 | + |
| 51 | + // Number of values is fewer than expected |
| 52 | + configCache->value = std::to_string(getUtcNowSecondsSinceEpoch()) + "\n" + string_format(kTestJsonFormat, R"("test2")"); |
| 53 | + EXPECT_EQ("test", client->getValue("testKey", "default")); |
| 54 | + EXPECT_TRUE(hookCallbacks.error.find("Error occurred while reading the cache. Number of values is fewer than expected.") != std::string::npos); |
| 55 | + |
| 56 | + // Invalid config JSON |
| 57 | + configCache->value = std::to_string(getUtcNowSecondsSinceEpoch()) + "\n" + "test-etag\n" + "wrong-json"; |
| 58 | + EXPECT_EQ("test", client->getValue("testKey", "default")); |
| 59 | + EXPECT_TRUE(hookCallbacks.error.find("Error occurred while reading the cache. Invalid config JSON: wrong-json.") != std::string::npos); |
| 60 | + |
| 61 | + ConfigCatClient::close(client); |
| 62 | +} |
0 commit comments