YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/rolling_log-test.cc
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  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,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#include <string>
33
#include <vector>
34
35
#include <glog/logging.h>
36
#include <glog/stl_logging.h>
37
38
#include "yb/gutil/strings/util.h"
39
#include "yb/util/env.h"
40
#include "yb/util/path_util.h"
41
#include "yb/util/result.h"
42
#include "yb/util/rolling_log.h"
43
#include "yb/util/test_macros.h"
44
#include "yb/util/test_util.h"
45
46
using std::string;
47
using std::vector;
48
using strings::Substitute;
49
50
namespace yb {
51
52
class RollingLogTest : public YBTest {
53
 public:
54
  RollingLogTest()
55
2
    : log_dir_(GetTestPath("log_dir")) {
56
2
  }
57
58
2
  void SetUp() override {
59
2
    ASSERT_OK(env_->CreateDir(log_dir_));
60
2
  }
61
62
 protected:
63
4
  void AssertLogCount(int expected_count, vector<string>* children) {
64
4
    vector<string> dir_entries;
65
4
    ASSERT_OK(env_->GetChildren(log_dir_, &dir_entries));
66
4
    children->clear();
67
68
12
    for (const string& child : dir_entries) {
69
12
      if (child == "." || child == "..") continue;
70
4
      children->push_back(child);
71
4
      ASSERT_TRUE(HasPrefixString(child, "rolling_log-test."));
72
4
      ASSERT_STR_CONTAINS(child, ".mylog.");
73
74
4
      string pid_suffix = Substitute("$0", getpid());
75
8
      ASSERT_TRUE(HasSuffixString(child, pid_suffix) ||
76
8
                  HasSuffixString(child, pid_suffix + ".gz")) << "bad child: " << child;
77
4
    }
78
8
    ASSERT_EQ(children->size(), expected_count) << *children;
79
4
  }
80
81
  const string log_dir_;
82
};
83
84
// Test with compression off.
85
1
TEST_F(RollingLogTest, TestLog) {
86
1
  RollingLog log(env_.get(), log_dir_, "mylog");
87
1
  log.SetCompressionEnabled(false);
88
1
  log.SetSizeLimitBytes(100);
89
90
  // Before writing anything, we shouldn't open a log file.
91
1
  vector<string> children;
92
1
  ASSERT_NO_FATALS(AssertLogCount(0, &children));
93
94
  // Appending some data should write a new segment.
95
1
  ASSERT_OK(log.Append("Hello world\n"));
96
1
  ASSERT_NO_FATALS(AssertLogCount(1, &children));
97
98
11
  for (int i = 0; i < 10; i++) {
99
10
    ASSERT_OK(log.Append("Hello world\n"));
100
10
  }
101
1
  ASSERT_NO_FATALS(AssertLogCount(2, &children));
102
103
1
  faststring data;
104
1
  string path = JoinPathSegments(log_dir_, children[0]);
105
1
  ASSERT_OK(ReadFileToString(env_.get(), path, &data));
106
2
  ASSERT_TRUE(HasPrefixString(data.ToString(), "Hello world\n"))
107
2
    << "Data missing";
108
2
  ASSERT_LE(data.size(), 100) << "Size limit not respected";
109
1
}
110
111
// Test with compression on.
112
1
TEST_F(RollingLogTest, TestCompression) {
113
1
  RollingLog log(env_.get(), log_dir_, "mylog");
114
1
  ASSERT_OK(log.Open());
115
116
1
  GStringPiece data = "Hello world\n";
117
1
  int raw_size = 0;
118
1.00k
  for (int i = 0; i < 1000; i++) {
119
1.00k
    ASSERT_OK(log.Append(data));
120
1.00k
    raw_size += data.size();
121
1.00k
  }
122
1
  ASSERT_OK(log.Close());
123
124
1
  vector<string> children;
125
1
  ASSERT_NO_FATALS(AssertLogCount(1, &children));
126
1
  ASSERT_TRUE(HasSuffixString(children[0], ".gz"));
127
128
  // Ensure that the output is actually gzipped.
129
1
  uint64_t size = ASSERT_RESULT(env_->GetFileSize(JoinPathSegments(log_dir_, children[0])));
130
1
  ASSERT_LT(size, raw_size / 10);
131
1
  ASSERT_GT(size, 0);
132
1
}
133
134
} // namespace yb