Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: components/tracing/common/process_metrics_memory_dump_provider_unittest.cc

Issue 2753723003: Fix crash in InvokeOnMemoryDump when tracing (Closed)
Patch Set: be more explicit Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/tracing/common/process_metrics_memory_dump_provider.h" 5 #include "components/tracing/common/process_metrics_memory_dump_provider.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/process/process_metrics.h" 13 #include "base/process/process_metrics.h"
14 #include "base/trace_event/memory_dump_manager.h"
14 #include "base/trace_event/process_memory_dump.h" 15 #include "base/trace_event/process_memory_dump.h"
15 #include "base/trace_event/process_memory_maps.h" 16 #include "base/trace_event/process_memory_maps.h"
16 #include "base/trace_event/process_memory_totals.h" 17 #include "base/trace_event/process_memory_totals.h"
17 #include "base/trace_event/trace_event_argument.h" 18 #include "base/trace_event/trace_event_argument.h"
19 #include "testing/gmock/include/gmock/gmock.h"
Primiano Tucci (use gerrit) 2017/03/15 12:44:40 I think this is a leftover, right? you are not usi
hjd 2017/03/15 14:23:12 Done.
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 #if defined(OS_MACOSX) 22 #if defined(OS_MACOSX)
21 #include <libgen.h> 23 #include <libgen.h>
22 #include <mach-o/dyld.h> 24 #include <mach-o/dyld.h>
23 #endif 25 #endif
24 26
25 #if defined(OS_WIN) 27 #if defined(OS_WIN)
26 #include <base/strings/sys_string_conversions.h> 28 #include <base/strings/sys_string_conversions.h>
27 #endif 29 #endif
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 }); 388 });
387 uint64_t last_address = 0; 389 uint64_t last_address = 0;
388 for (const VMRegion& region : regions) { 390 for (const VMRegion& region : regions) {
389 EXPECT_GE(region.start_address, last_address); 391 EXPECT_GE(region.start_address, last_address);
390 last_address = region.start_address + region.size_in_bytes; 392 last_address = region.start_address + region.size_in_bytes;
391 } 393 }
392 } 394 }
393 395
394 #endif // defined(OS_MACOSX) 396 #endif // defined(OS_MACOSX)
395 397
398 class MockMemoryDumpProvider : public ProcessMetricsMemoryDumpProvider {
399 public:
400 MOCK_METHOD2(OnMemoryDump,
Primiano Tucci (use gerrit) 2017/03/15 12:44:40 are you really using this mocked method? I think t
hjd 2017/03/15 14:23:12 Done.
401 bool(const base::trace_event::MemoryDumpArgs& args,
402 base::trace_event::ProcessMemoryDump* pmd));
403
404 MockMemoryDumpProvider(base::ProcessId process);
405 ~MockMemoryDumpProvider() override;
406 };
407
408 std::unordered_set<MockMemoryDumpProvider*> live_mocks;
Primiano Tucci (use gerrit) 2017/03/15 12:44:40 nit: g_ -> g_live_mocks (same with dead) as these
hjd 2017/03/15 14:23:12 Done.
409 std::unordered_set<MockMemoryDumpProvider*> dead_mocks;
410
411 MockMemoryDumpProvider::MockMemoryDumpProvider(base::ProcessId process)
412 : ProcessMetricsMemoryDumpProvider(process) {
413 live_mocks.insert(this);
414 }
415
416 MockMemoryDumpProvider::~MockMemoryDumpProvider() {
417 // ASSERT_TRUE(live_mocks.find(this) != live_mocks.end());
418 live_mocks.erase(this);
419 dead_mocks.insert(this);
420 }
421
422 TEST(ProcessMetricsMemoryDumpProviderTest, DoubleRegister) {
423 auto factory = [](base::ProcessId process) {
424 return std::unique_ptr<ProcessMetricsMemoryDumpProvider>(
425 new MockMemoryDumpProvider(process));
426 };
427 ProcessMetricsMemoryDumpProvider::factory_for_testing = factory;
428 ProcessMetricsMemoryDumpProvider::RegisterForProcess(1);
429 ProcessMetricsMemoryDumpProvider::RegisterForProcess(1);
430 EXPECT_EQ(1u, live_mocks.size());
Primiano Tucci (use gerrit) 2017/03/15 12:44:40 I'd make this an ASSERT_EQ and remove the if(...==
hjd 2017/03/15 14:23:12 Done.
431 EXPECT_EQ(1u, dead_mocks.size());
432 auto* manager = base::trace_event::MemoryDumpManager::GetInstance();
433 if (live_mocks.size() == 1u) {
434 MockMemoryDumpProvider* live_mock = *live_mocks.begin();
435 EXPECT_TRUE(manager->IsDumpProviderRegisteredForTesting(live_mock));
436 }
437 if (dead_mocks.size() == 1u) {
438 auto* dead_mock = *dead_mocks.begin();
439 EXPECT_FALSE(manager->IsDumpProviderRegisteredForTesting(dead_mock));
440 }
441 ProcessMetricsMemoryDumpProvider::UnregisterForProcess(1);
442 live_mocks.clear();
443 dead_mocks.clear();
444 }
445
396 } // namespace tracing 446 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698