Skip to content

Commit 473489d

Browse files
committed
MB-40493: Move MockBucketLogger to its own file
Change-Id: I0dbe42182081a284637809d179e746eb3e43cb32 Reviewed-on: http://review.couchbase.org/c/kv_engine/+/136912 Well-Formed: Build Bot <[email protected]> Reviewed-by: Ben Huddleston <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent b222b46 commit 473489d

File tree

2 files changed

+62
-38
lines changed

2 files changed

+62
-38
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2+
/*
3+
* Copyright 2020 Couchbase, Inc.
4+
*
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
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
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.
16+
*/
17+
18+
#pragma once
19+
20+
#include "bucket_logger.h"
21+
22+
#include <folly/portability/GMock.h>
23+
24+
/**
25+
* The MockBucket Logger is used to verify that the logger is called with
26+
* certain parameters / messages.
27+
*
28+
* The MockBucketLogger calls the log method as normal, and intercepts the
29+
* _sink_it call by overriding it to determine the correctness of the logging
30+
*/
31+
class MockBucketLogger : public BucketLogger {
32+
public:
33+
MockBucketLogger(std::string name) : BucketLogger(name) {
34+
// Set the log level of the BucketLogger to trace to ensure messages
35+
// make it through to the sink it method. Does not alter the logging
36+
// level of the underlying spdlogger so we will not see console
37+
// output during the test.
38+
set_level(spdlog::level::level_enum::trace);
39+
using namespace testing;
40+
ON_CALL(*this, mlog(_, _))
41+
.WillByDefault(Invoke([](spdlog::level::level_enum sev,
42+
const std::string& msg) {}));
43+
}
44+
45+
// Mock a method taking a logging level and formatted message to test log
46+
// outputs.
47+
MOCK_CONST_METHOD2(mlog,
48+
void(spdlog::level::level_enum severity,
49+
const std::string& message));
50+
51+
protected:
52+
// Override the sink_it_ method to redirect to the mocked method
53+
// Must call the mlog method to check the message details as they are
54+
// bundled in the log_msg object. Beware, msg.raw is not null terminated.
55+
// In these test cases however we just search for a substring within the log
56+
// message so this is okay.
57+
void sink_it_(spdlog::details::log_msg& msg) override {
58+
mlog(msg.level, msg.raw.data());
59+
}
60+
};

engines/ep/tests/module_tests/kvstore_test.cc

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifdef EP_USE_MAGMA
3232
#include "magma-kvstore/magma-kvstore_config.h"
3333
#endif
34+
#include "../mock/mock_bucket_logger.h"
3435
#include "programs/engine_testapp/mock_server.h"
3536
#include "src/internal.h"
3637
#include "src/rollback_result.h"
@@ -44,8 +45,8 @@
4445
#include "vbucket_state.h"
4546
#include "workload.h"
4647

47-
#include <folly/portability/GMock.h>
4848
#include <folly/portability/GTest.h>
49+
4950
#include <kvstore.h>
5051
#include <thread>
5152
#include <unordered_map>
@@ -585,43 +586,6 @@ TEST_F(CouchKVStoreTest, CollectionsOfflineUpgade) {
585586

586587
using namespace testing;
587588

588-
/**
589-
* The MockBucket Logger is used to verify that the logger is called with
590-
* certain parameters / messages.
591-
*
592-
* The MockBucketLogger calls the log method as normal, and intercepts the
593-
* _sink_it call by overriding it to determine the correctness of the logging
594-
*/
595-
class MockBucketLogger : public BucketLogger {
596-
public:
597-
MockBucketLogger(std::string name) : BucketLogger(name) {
598-
// Set the log level of the BucketLogger to trace to ensure messages
599-
// make it through to the sink it method. Does not alter the logging
600-
// level of the underlying spdlogger so we will not see console
601-
// output during the test.
602-
set_level(spdlog::level::level_enum::trace);
603-
ON_CALL(*this, mlog(_, _))
604-
.WillByDefault(Invoke([](spdlog::level::level_enum sev,
605-
const std::string& msg) {}));
606-
}
607-
608-
// Mock a method taking a logging level and formatted message to test log
609-
// outputs.
610-
MOCK_CONST_METHOD2(mlog,
611-
void(spdlog::level::level_enum severity,
612-
const std::string& message));
613-
614-
protected:
615-
// Override the sink_it_ method to redirect to the mocked method
616-
// Must call the mlog method to check the message details as they are
617-
// bundled in the log_msg object. Beware, msg.raw is not null terminated.
618-
// In these test cases however we just search for a substring within the log
619-
// message so this is okay.
620-
void sink_it_(spdlog::details::log_msg& msg) override {
621-
mlog(msg.level, msg.raw.data());
622-
}
623-
};
624-
625589
/**
626590
* VCE: Verify Couchstore Error
627591
*

0 commit comments

Comments
 (0)