summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/ide/completion/name/cache/cached_ut.cpp
blob: e732573ca48d09d3f6823cd39dad76aefef99143 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "cached.h"

#include <yql/essentials/sql/v1/ide/completion/name/cache/local/cache.h>

#include <library/cpp/testing/unittest/registar.h>

#include <library/cpp/time_provider/monotonic_provider.h>

using namespace NSQLComplete;

class TFailableCache: public ICache<TString, TString> {
public:
    NThreading::TFuture<TEntry> Get(const TString& key) const override try {
        if (IsGetFailing) {
            ythrow yexception() << "O_O";
        }
        return NThreading::MakeFuture<TEntry>({.Value = key, .IsExpired = IsExpired});
    } catch (...) {
        return NThreading::MakeErrorFuture<TEntry>(std::current_exception());
    }

    NThreading::TFuture<void> Update(const TString& /* key */, TString /* value */) const override try {
        if (IsUpdateFailing) {
            ythrow yexception() << "O_O";
        }
        return NThreading::MakeFuture();
    } catch (...) {
        return NThreading::MakeErrorFuture<void>(std::current_exception());
    }

    bool IsGetFailing = false;
    bool IsExpired = false;
    bool IsUpdateFailing = false;
};

Y_UNIT_TEST_SUITE(CachedQueryTests) {

Y_UNIT_TEST(OnEmpty_WhenGet_ThenWaitUntilReceived) {
    auto cache = MakeLocalCache<TString, TString>(
        NMonotonic::CreateDefaultMonotonicTimeProvider(), {.TTL = TDuration::Zero()});
    auto cached = TCachedQuery<TString, TString>(cache, [&](const TString& key) {
        return NThreading::MakeFuture<TString>(key);
    });

    TString value = cached("1").GetValueSync();

    UNIT_ASSERT_VALUES_EQUAL(value, "1");
}

Y_UNIT_TEST(OnExpired_WhenApplied_ThenDefferedUpdateAndReturnOld) {
    size_t queried = 0;
    auto cache = MakeLocalCache<TString, TString>(
        NMonotonic::CreateDefaultMonotonicTimeProvider(), {.TTL = TDuration::Zero()});
    auto cached = TCachedQuery<TString, TString>(cache, [&](const TString& key) {
        queried += 1;
        return NThreading::MakeFuture<TString>(key);
    });
    cache->Update("1", "2");

    TString value = cached("1").GetValueSync();

    UNIT_ASSERT_VALUES_EQUAL(value, "2");
    UNIT_ASSERT_VALUES_EQUAL(queried, 1);
    UNIT_ASSERT_VALUES_EQUAL(cached("1").GetValueSync(), "1");
}

Y_UNIT_TEST(OnQueryError_WhenApplied_ThenNoDeadlock) {
    size_t queried = 0;
    auto cache = MakeLocalCache<TString, TString>(
        NMonotonic::CreateDefaultMonotonicTimeProvider(), {.TTL = TDuration::Zero()});
    auto cached = TCachedQuery<TString, TString>(cache, [&](const TString&) {
        queried += 1;
        try {
            ythrow yexception() << "T_T";
        } catch (...) {
            return NThreading::MakeErrorFuture<TString>(std::current_exception());
        }
    });

    UNIT_ASSERT_EXCEPTION(cached("1").GetValueSync(), yexception);
    UNIT_ASSERT_EXCEPTION(cached("1").GetValueSync(), yexception);
    UNIT_ASSERT_VALUES_EQUAL(queried, 2);
}

Y_UNIT_TEST(OnFailingCacheGet_WhenApplied_ThenNoDeadlock) {
    size_t queried = 0;
    auto cache = MakeIntrusive<TFailableCache>();
    auto cached = TCachedQuery<TString, TString>(cache, [&](const TString& key) {
        queried += 1;
        return NThreading::MakeFuture<TString>(key);
    });

    cache->IsGetFailing = true;

    UNIT_ASSERT_EXCEPTION(cached("1").GetValueSync(), yexception);
    UNIT_ASSERT_VALUES_EQUAL(queried, 0);
}

Y_UNIT_TEST(OnFailingCacheUpdate_WhenApplied_ThenNoErrorAndNotCached) {
    size_t queried = 0;
    auto cache = MakeIntrusive<TFailableCache>();
    auto cached = TCachedQuery<TString, TString>(cache, [&](const TString& key) {
        queried += 1;
        return NThreading::MakeFuture<TString>(key);
    });

    cache->IsExpired = true;
    cache->IsUpdateFailing = true;

    UNIT_ASSERT_VALUES_EQUAL(cached("1").GetValueSync(), "1");
    UNIT_ASSERT_VALUES_EQUAL(cached("1").GetValueSync(), "1");
    UNIT_ASSERT_VALUES_EQUAL(queried, 2);
}

} // Y_UNIT_TEST_SUITE(CachedQueryTests)