blob: 62f09093b156b388021edccdb2c5c163e2322071 [file] [log] [blame]
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/profiling/memory/client.h"
#include <thread>
#include "perfetto/base/thread_utils.h"
#include "perfetto/ext/base/unix_socket.h"
#include "src/profiling/memory/wire_protocol.h"
#include "test/gtest_and_gmock.h"
namespace perfetto {
namespace profiling {
namespace {
TEST(ClientTest, GetThreadStackBase) {
std::thread th([] {
const char* stackbase = GetThreadStackBase();
ASSERT_NE(stackbase, nullptr);
// The implementation assumes the stack grows from higher addresses to
// lower. We will need to rework once we encounter architectures where the
// stack grows the other way.
EXPECT_GT(stackbase, __builtin_frame_address(0));
});
th.join();
}
TEST(ClientTest, IsMainThread) {
// Our code relies on the fact that getpid() == GetThreadId() if this
// process/thread is the main thread of the process. This test ensures that is
// true.
auto pid = getpid();
auto main_thread_id = base::GetThreadId();
EXPECT_EQ(pid, main_thread_id);
std::thread th(
[main_thread_id] { EXPECT_NE(main_thread_id, base::GetThreadId()); });
th.join();
}
TEST(ClientTest, GetMaxTriesBlock) {
ClientConfiguration cfg = {};
cfg.block_client = true;
cfg.block_client_timeout_us = 200;
EXPECT_EQ(GetMaxTries(cfg), 2u);
}
TEST(ClientTest, GetMaxTriesBlockSmall) {
ClientConfiguration cfg = {};
cfg.block_client = true;
cfg.block_client_timeout_us = 99;
EXPECT_EQ(GetMaxTries(cfg), 1u);
}
TEST(ClientTest, GetMaxTriesBlockVerySmall) {
ClientConfiguration cfg = {};
cfg.block_client = true;
cfg.block_client_timeout_us = 1;
EXPECT_EQ(GetMaxTries(cfg), 1u);
}
TEST(ClientTest, GetMaxTriesBlockInfinite) {
ClientConfiguration cfg = {};
cfg.block_client = true;
cfg.block_client_timeout_us = 0;
EXPECT_EQ(GetMaxTries(cfg), kInfiniteTries);
}
TEST(ClientTest, GetMaxTriesNoBlock) {
ClientConfiguration cfg = {};
cfg.block_client = false;
cfg.block_client_timeout_us = 200;
EXPECT_EQ(GetMaxTries(cfg), 1u);
}
} // namespace
} // namespace profiling
} // namespace perfetto