1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright 2016 Couchbase, Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * Unit tests for the KVBucket class.
21 * These tests are instantiated with additional config strings to test over
22 * ephemeral and value and full eviction persistent buckets.
30 #include "ep_bucket.h"
31 #include "ep_engine.h"
34 #include <memcached/engine.h>
35 #include <tests/mock/mock_synchronous_ep_engine.h>
37 #include <gtest/gtest.h>
41 * Test fixture for KVBucket unit tests.
43 * Will create the appropriate subclass of KVBucket (EPBucket /
44 * EphemeralBucket) based on the Configuration passed (specifically the
45 * bucket_type parameter), defaulting to EPBucket if no bucket_type is
48 class KVBucketTest : public ::testing::Test {
50 void SetUp() override;
52 void TearDown() override;
54 // Stores an item into the given vbucket. Returns the item stored.
55 Item store_item(uint16_t vbid,
56 const StoredDocKey& key,
57 const std::string& value,
59 const std::vector<cb::engine_errc>& expected =
60 {cb::engine_errc::success},
61 protocol_binary_datatype_t datatype =
62 PROTOCOL_BINARY_DATATYPE_JSON);
65 * Store multiple items into the vbucket, the given key will have an
66 * iteration appended to it.
68 ::testing::AssertionResult store_items(
72 const std::string& value,
74 protocol_binary_datatype_t datatype =
75 PROTOCOL_BINARY_DATATYPE_JSON);
77 /* Flush the given vbucket to disk, so any outstanding dirty items are
78 * written (and are clean).
80 void flush_vbucket_to_disk(uint16_t vbid, int expected = 1);
82 /* Delete the given item from the given vbucket, verifying it was
83 * successfully deleted.
85 void delete_item(uint16_t vbid, const StoredDocKey& key);
87 /* Evict the given key from memory according to the current eviction
88 * strategy. Verifies it was successfully evicted.
90 void evict_key(uint16_t vbid, const StoredDocKey& key);
92 /// Exposes the normally-protected getInternal method from the store.
93 GetValue getInternal(const StoredDocKey& key,
96 vbucket_state_t allowedState,
97 get_options_t options);
100 * Creates the ItemPager task and adds it to the scheduler. Allows testing
101 * of the item pager from subclasses, without KVBucket having to grant
102 * friendship to many different test classes.
104 void createAndScheduleItemPager();
106 void initializeExpiryPager();
108 static const char test_dbname[];
110 std::string config_string;
112 const uint16_t vbid = 0;
114 // The mock engine (needed to construct the store).
115 std::unique_ptr<SynchronousEPEngine> engine;
117 // The store under test. Wrapped in a mock to expose some normally
118 // protected members. Uses a raw pointer as this is owned by the engine.
121 // The (mock) server cookie.
126 * Test fixture for KVBucket unit tests.
128 * These tests are parameterized over an extra config string to allow them to
129 * be run against ephemeral and value and full eviction persistent buckets.
131 class KVBucketParamTest : public KVBucketTest,
132 public ::testing::WithParamInterface<std::string> {
133 void SetUp() override {
134 config_string += GetParam();
135 KVBucketTest::SetUp();
137 // Have all the objects, activate vBucket zero so we can store data.
138 store->setVBucketState(vbid, vbucket_state_active, false);