Skip to content

Commit e70d261

Browse files
committed
Merge branch 'mad-hatter'
* commit '473489d9d': MB-40493: Move MockBucketLogger to its own file Change-Id: I80a2630bb8ede0ac40f69244f11e33a899399c75
2 parents 6921dc3 + 473489d commit e70d261

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
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/couch-kvstore_test.cc

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "rollback_result.h"
2828
#include "src/internal.h"
2929
#include "test_helpers.h"
30+
#include "tests/mock/mock_bucket_logger.h"
3031
#include "tests/mock/mock_couch_kvstore.h"
3132
#include "tests/test_fileops.h"
3233
#include "tools/couchfile_upgrade/input_couchfile.h"
@@ -466,43 +467,6 @@ TEST_F(CouchKVStoreTest, OpenHistoricalSnapshot) {
466467

467468
using namespace testing;
468469

469-
/**
470-
* The MockBucket Logger is used to verify that the logger is called with
471-
* certain parameters / messages.
472-
*
473-
* The MockBucketLogger calls the log method as normal, and intercepts the
474-
* _sink_it call by overriding it to determine the correctness of the logging
475-
*/
476-
class MockBucketLogger : public BucketLogger {
477-
public:
478-
explicit MockBucketLogger(std::string name) : BucketLogger(name) {
479-
// Set the log level of the BucketLogger to trace to ensure messages
480-
// make it through to the sink it method. Does not alter the logging
481-
// level of the underlying spdlogger so we will not see console
482-
// output during the test.
483-
set_level(spdlog::level::level_enum::trace);
484-
ON_CALL(*this, mlog(_, _))
485-
.WillByDefault(Invoke([](spdlog::level::level_enum sev,
486-
const std::string& msg) {}));
487-
}
488-
489-
// Mock a method taking a logging level and formatted message to test log
490-
// outputs.
491-
MOCK_CONST_METHOD2(mlog,
492-
void(spdlog::level::level_enum severity,
493-
const std::string& message));
494-
495-
protected:
496-
// Override the sink_it_ method to redirect to the mocked method
497-
// Must call the mlog method to check the message details as they are
498-
// bundled in the log_msg object. Beware, msg.raw is not null terminated.
499-
// In these test cases however we just search for a substring within the log
500-
// message so this is okay.
501-
void sink_it_(spdlog::details::log_msg& msg) override {
502-
mlog(msg.level, msg.raw.data());
503-
}
504-
};
505-
506470
/**
507471
* VCE: Verify Couchstore Error
508472
*

0 commit comments

Comments
 (0)