Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit f0e5b47

Browse files
committed
add defferred cleanup test file
1 parent eab31bd commit f0e5b47

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

src/cleanup_deferred_unittest.cc

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) 2024, Google Inc.
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are
6+
// met:
7+
//
8+
// * Redistributions of source code must retain the above copyright
9+
// notice, this list of conditions and the following disclaimer.
10+
// * Redistributions in binary form must reproduce the above
11+
// copyright notice, this list of conditions and the following disclaimer
12+
// in the documentation and/or other materials provided with the
13+
// distribution.
14+
// * Neither the name of Google Inc. nor the names of its
15+
// contributors may be used to endorse or promote products derived from
16+
// this software without specific prior written permission.
17+
//
18+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
#include "base/commandlineflags.h"
31+
#include "glog/logging.h"
32+
#include "glog/raw_logging.h"
33+
#include "googletest.h"
34+
35+
#ifdef GLOG_USE_GFLAGS
36+
# include <gflags/gflags.h>
37+
using namespace GFLAGS_NAMESPACE;
38+
#endif
39+
40+
#ifdef HAVE_LIB_GMOCK
41+
# include <gmock/gmock.h>
42+
43+
# include "mock-log.h"
44+
// Introduce several symbols from gmock.
45+
using google::glog_testing::ScopedMockLog;
46+
using testing::_;
47+
using testing::AllOf;
48+
using testing::AnyNumber;
49+
using testing::HasSubstr;
50+
using testing::InitGoogleMock;
51+
using testing::StrictMock;
52+
using testing::StrNe;
53+
#endif
54+
55+
using namespace google;
56+
57+
TEST(CleanDeferred, logging) {
58+
using namespace std::chrono_literals;
59+
// create an array of two strings
60+
const string dest =
61+
FLAGS_test_tmpdir + "/test_cleanup_enable_generic_logs";
62+
google::EnableLogCleanerForGenericLogs(1h);
63+
google::SetLogDestination(GLOG_INFO, dest.c_str());
64+
for (unsigned i = 0; i < 10; ++i) {
65+
LOG(INFO) << "cleanup test";
66+
}
67+
68+
google::DisableLogCleanerForGenericLogs();
69+
// delete the file
70+
CHECK(unlink(dest.c_str()) == 0) << ": " << strerror(errno);
71+
}
72+
73+
int main(int argc, char** argv) {
74+
FLAGS_colorlogtostderr = false;
75+
FLAGS_timestamp_in_logfile_name = false;
76+
FLAGS_logcleansecs = 1;
77+
#ifdef GLOG_USE_GFLAGS
78+
ParseCommandLineFlags(&argc, &argv, true);
79+
#endif
80+
// Make sure stderr is not buffered as stderr seems to be buffered
81+
// on recent windows.
82+
setbuf(stderr, nullptr);
83+
84+
// Test some basics before InitGoogleLogging:
85+
CaptureTestStderr();
86+
const string early_stderr = GetCapturedTestStderr();
87+
88+
EXPECT_FALSE(IsGoogleLoggingInitialized());
89+
90+
InitGoogleLogging(argv[0]);
91+
92+
EXPECT_TRUE(IsGoogleLoggingInitialized());
93+
94+
InitGoogleTest(&argc, argv);
95+
#ifdef HAVE_LIB_GMOCK
96+
InitGoogleMock(&argc, argv);
97+
#endif
98+
99+
// so that death tests run before we use threads
100+
CHECK_EQ(RUN_ALL_TESTS(), 0);
101+
}

0 commit comments

Comments
 (0)