• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/profiling/memory/client.h"
18 
19 #include <thread>
20 
21 #include "perfetto/base/thread_utils.h"
22 #include "perfetto/ext/base/unix_socket.h"
23 #include "src/profiling/memory/wire_protocol.h"
24 #include "test/gtest_and_gmock.h"
25 
26 namespace perfetto {
27 namespace profiling {
28 namespace {
29 
TEST(ClientTest,GetThreadStackBase)30 TEST(ClientTest, GetThreadStackBase) {
31   std::thread th([] {
32     const char* stackbase = GetThreadStackBase();
33     ASSERT_NE(stackbase, nullptr);
34     // The implementation assumes the stack grows from higher addresses to
35     // lower. We will need to rework once we encounter architectures where the
36     // stack grows the other way.
37     EXPECT_GT(stackbase, __builtin_frame_address(0));
38   });
39   th.join();
40 }
41 
TEST(ClientTest,IsMainThread)42 TEST(ClientTest, IsMainThread) {
43   // Our code relies on the fact that getpid() == GetThreadId() if this
44   // process/thread is the main thread of the process. This test ensures that is
45   // true.
46   auto pid = getpid();
47   auto main_thread_id = base::GetThreadId();
48   EXPECT_EQ(pid, main_thread_id);
49   std::thread th(
50       [main_thread_id] { EXPECT_NE(main_thread_id, base::GetThreadId()); });
51   th.join();
52 }
53 
TEST(ClientTest,GetMaxTriesBlock)54 TEST(ClientTest, GetMaxTriesBlock) {
55   ClientConfiguration cfg = {};
56   cfg.block_client = true;
57   cfg.block_client_timeout_us = 200;
58   EXPECT_EQ(GetMaxTries(cfg), 2u);
59 }
60 
TEST(ClientTest,GetMaxTriesBlockSmall)61 TEST(ClientTest, GetMaxTriesBlockSmall) {
62   ClientConfiguration cfg = {};
63   cfg.block_client = true;
64   cfg.block_client_timeout_us = 99;
65   EXPECT_EQ(GetMaxTries(cfg), 1u);
66 }
67 
TEST(ClientTest,GetMaxTriesBlockVerySmall)68 TEST(ClientTest, GetMaxTriesBlockVerySmall) {
69   ClientConfiguration cfg = {};
70   cfg.block_client = true;
71   cfg.block_client_timeout_us = 1;
72   EXPECT_EQ(GetMaxTries(cfg), 1u);
73 }
74 
TEST(ClientTest,GetMaxTriesBlockInfinite)75 TEST(ClientTest, GetMaxTriesBlockInfinite) {
76   ClientConfiguration cfg = {};
77   cfg.block_client = true;
78   cfg.block_client_timeout_us = 0;
79   EXPECT_EQ(GetMaxTries(cfg), kInfiniteTries);
80 }
81 
TEST(ClientTest,GetMaxTriesNoBlock)82 TEST(ClientTest, GetMaxTriesNoBlock) {
83   ClientConfiguration cfg = {};
84   cfg.block_client = false;
85   cfg.block_client_timeout_us = 200;
86   EXPECT_EQ(GetMaxTries(cfg), 1u);
87 }
88 
89 }  // namespace
90 }  // namespace profiling
91 }  // namespace perfetto
92