summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/persistence/apply_bucket_diff_state_test.cpp
blob: 6e1e62d2080ecf7f9a1f715628dc921055d597ef (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/storage/persistence/apply_bucket_diff_entry_result.h>
#include <vespa/storage/persistence/apply_bucket_diff_state.h>
#include <vespa/storage/persistence/merge_bucket_info_syncer.h>
#include <vespa/document/base/documentid.h>
#include <vespa/document/bucket/bucketid.h>
#include <vespa/document/bucket/bucketidfactory.h>
#include <vespa/document/test/make_document_bucket.h>
#include <vespa/persistence/spi/result.h>
#include <gtest/gtest.h>

using document::DocumentId;
using document::test::makeDocumentBucket;

namespace storage {

namespace {

spi::Result spi_result_ok;
spi::Result spi_result_fail(spi::Result::ErrorType::RESOURCE_EXHAUSTED, "write blocked");
document::BucketIdFactory bucket_id_factory;
const char *test_op = "put";
document::Bucket dummy_document_bucket(makeDocumentBucket(document::BucketId(0, 16)));

class DummyMergeBucketInfoSyncer : public MergeBucketInfoSyncer
{
    uint32_t& _sync_count;
public:
    DummyMergeBucketInfoSyncer(uint32_t& sync_count)
        : MergeBucketInfoSyncer(),
          _sync_count(sync_count)
    {
    }
    void sync_bucket_info(const spi::Bucket& bucket) const override {
        EXPECT_EQ(bucket, spi::Bucket(dummy_document_bucket));
        ++_sync_count;
    }
};

ApplyBucketDiffEntryResult
make_result(spi::Result &spi_result, const DocumentId &doc_id)
{
    std::promise<std::unique_ptr<spi::Result>> result_promise;
    result_promise.set_value(std::make_unique<spi::Result>(spi_result));
    spi::Bucket bucket(makeDocumentBucket(bucket_id_factory.getBucketId(doc_id)));
    return ApplyBucketDiffEntryResult(result_promise.get_future(), bucket, doc_id, test_op);
}

void push_ok(ApplyBucketDiffState &state)
{
    state.push_back(make_result(spi_result_ok, DocumentId("id::test::0")));
    state.push_back(make_result(spi_result_ok, DocumentId("id::test::1")));
}

void push_bad(ApplyBucketDiffState &state)
{
    state.push_back(make_result(spi_result_ok, DocumentId("id::test::0")));
    state.push_back(make_result(spi_result_fail, DocumentId("id::test::1")));
    state.push_back(make_result(spi_result_fail, DocumentId("id::test::2")));
}

}

class ApplyBucketDiffStateTestBase : public ::testing::Test
{
public:
    uint32_t                   sync_count;
    DummyMergeBucketInfoSyncer syncer;

    ApplyBucketDiffStateTestBase()
        : ::testing::Test(),
          sync_count(0u),
          syncer(sync_count)
    {
    }

    std::unique_ptr<ApplyBucketDiffState> make_state() {
        return std::make_unique<ApplyBucketDiffState>(syncer, spi::Bucket(dummy_document_bucket));
    }
};

class ApplyBucketDiffStateTest : public ApplyBucketDiffStateTestBase
{
public:
    std::unique_ptr<ApplyBucketDiffState> state;

    ApplyBucketDiffStateTest()
        : ApplyBucketDiffStateTestBase(),
          state(make_state())
    {
    }

    void reset() {
        state = make_state();
    }

    void check_failure() {
        try {
            state->check();
            FAIL() << "Failed to throw exception for failed result";
        } catch (std::exception &e) {
            EXPECT_EQ("Failed put for id::test::1 in Bucket(0xeb4700c03842cac4): Result(5, write blocked)", std::string(e.what()));
        }
    }

};

TEST_F(ApplyBucketDiffStateTest, ok_results_can_be_checked)
{
    push_ok(*state);
    state->check();
}

TEST_F(ApplyBucketDiffStateTest, failed_result_errors_ignored)
{
    push_bad(*state);
}

TEST_F(ApplyBucketDiffStateTest, first_failed_result_throws_exception)
{
    push_bad(*state);
    ASSERT_NO_FATAL_FAILURE(check_failure());
}

TEST_F(ApplyBucketDiffStateTest, sync_bucket_info_if_needed_on_destruct)
{
    reset();
    EXPECT_EQ(0, sync_count);
    state->mark_stale_bucket_info();
    EXPECT_EQ(0, sync_count);
    reset();
    EXPECT_EQ(1, sync_count);
}

TEST_F(ApplyBucketDiffStateTest, explicit_sync_bucket_info_works)
{
    state->sync_bucket_info();
    EXPECT_EQ(0, sync_count);
    state->mark_stale_bucket_info();
    state->sync_bucket_info();
    EXPECT_EQ(1, sync_count);
    state->sync_bucket_info();
    EXPECT_EQ(1, sync_count);
    reset();
    EXPECT_EQ(1, sync_count);
}

}