huangcm
2024-08-23 d76fb8c8c6d079a3cee81da7072347dcb8bbbc70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "mojo/core/handle_table.h"
 
#include <memory>
 
#include "base/synchronization/lock.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_request_args.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event_argument.h"
#include "mojo/core/dispatcher.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
 
using base::trace_event::MemoryAllocatorDump;
using testing::Contains;
using testing::Eq;
using testing::Contains;
using testing::ByRef;
 
namespace mojo {
namespace core {
namespace {
 
class FakeMessagePipeDispatcher : public Dispatcher {
 public:
  FakeMessagePipeDispatcher() {}
 
  Type GetType() const override { return Type::MESSAGE_PIPE; }
 
  MojoResult Close() override { return MOJO_RESULT_OK; }
 
 private:
  ~FakeMessagePipeDispatcher() override {}
  DISALLOW_COPY_AND_ASSIGN(FakeMessagePipeDispatcher);
};
 
void CheckNameAndValue(base::trace_event::ProcessMemoryDump* pmd,
                       const std::string& name,
                       uint64_t value) {
  base::trace_event::MemoryAllocatorDump* mad = pmd->GetAllocatorDump(name);
  ASSERT_TRUE(mad);
 
  MemoryAllocatorDump::Entry expected(
      "object_count", MemoryAllocatorDump::kUnitsObjects, value);
  EXPECT_THAT(mad->entries(), Contains(Eq(ByRef(expected))));
}
 
}  // namespace
 
TEST(HandleTableTest, OnMemoryDump) {
  HandleTable ht;
 
  {
    base::AutoLock auto_lock(ht.GetLock());
    scoped_refptr<Dispatcher> dispatcher(new FakeMessagePipeDispatcher);
    ht.AddDispatcher(dispatcher);
  }
 
  base::trace_event::MemoryDumpArgs args = {
      base::trace_event::MemoryDumpLevelOfDetail::DETAILED};
  base::trace_event::ProcessMemoryDump pmd(args);
  ht.OnMemoryDump(args, &pmd);
 
  CheckNameAndValue(&pmd, "mojo/message_pipe", 1);
  CheckNameAndValue(&pmd, "mojo/data_pipe_consumer", 0);
}
 
}  // namespace core
}  // namespace mojo