YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/pstack_watcher-test.cc
Line
Count
Source (jump to first uncovered line)
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 <stdio.h>
33
34
#include <memory>
35
#include <vector>
36
37
#include <gtest/gtest.h>
38
39
#include "yb/util/env.h"
40
#include "yb/util/errno.h"
41
#include "yb/util/pstack_watcher.h"
42
#include "yb/util/status_log.h"
43
#include "yb/util/test_macros.h"
44
45
using std::shared_ptr;
46
using std::string;
47
using strings::Substitute;
48
49
namespace yb {
50
51
1
TEST(TestPstackWatcher, TestPstackWatcherCancellation) {
52
1
  PstackWatcher watcher(MonoDelta::FromSeconds(1000000));
53
1
  watcher.Shutdown();
54
1
}
55
56
1
TEST(TestPstackWatcher, TestWait) {
57
1
  PstackWatcher watcher(MonoDelta::FromMilliseconds(10));
58
1
  watcher.Wait();
59
1
}
60
61
// Disable TestDumpStacks and TestPstackWatcherRunning on macOS because neither gdb or lldb ways
62
// of obtaining stack traces there appear to work. gdb requires codesigning, which is a manual
63
// procedure we have to go through on every build host, and it is not high priority as of 12/2018.
64
0
TEST(TestPstackWatcher, YB_DISABLE_TEST_ON_MACOS(TestDumpStacks)) {
65
0
  ASSERT_OK(PstackWatcher::DumpStacks());
66
0
}
67
68
0
static shared_ptr<FILE> RedirectStdout(string *temp_path) {
69
0
  string temp_dir;
70
0
  CHECK_OK(Env::Default()->GetTestDirectory(&temp_dir));
71
0
  *temp_path = Substitute("$0/pstack_watcher-dump.$1.txt",
72
0
                      temp_dir, getpid());
73
0
  return shared_ptr<FILE>(
74
0
      freopen(temp_path->c_str(), "w", stdout), fclose);
75
0
}
76
77
0
TEST(TestPstackWatcher, YB_DISABLE_TEST_ON_MACOS(TestPstackWatcherRunning)) {
78
0
  string stdout_file;
79
0
  int old_stdout;
80
0
  CHECK_ERR(old_stdout = dup(STDOUT_FILENO));
81
0
  {
82
0
    shared_ptr<FILE> out_fp = RedirectStdout(&stdout_file);
83
0
    PCHECK(out_fp.get());
84
0
    PstackWatcher watcher(MonoDelta::FromMilliseconds(500));
85
0
    while (watcher.IsRunning()) {
86
0
      SleepFor(MonoDelta::FromMilliseconds(1));
87
0
    }
88
0
  }
89
0
  CHECK_ERR(dup2(old_stdout, STDOUT_FILENO));
90
0
  PCHECK(stdout = fdopen(STDOUT_FILENO, "w"));
91
92
0
  faststring contents;
93
0
  CHECK_OK(ReadFileToString(Env::Default(), stdout_file, &contents));
94
0
  ASSERT_STR_CONTAINS(contents.ToString(), "BEGIN STACKS");
95
0
  CHECK_ERR(unlink(stdout_file.c_str()));
96
0
  ASSERT_GE(fprintf(stdout, "%s\n", contents.ToString().c_str()), 0)
97
0
      << "errno=" << errno << ": " << ErrnoToString(errno);
98
0
}
99
100
} // namespace yb