OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
6 | 6 |
7 #include "base/multiprocess_test.h" | 7 #include "base/multiprocess_test.h" |
8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
13 #include <windows.h> | 13 #include <windows.h> |
14 #elif defined(OS_LINUX) | 14 #elif defined(OS_LINUX) |
15 #include <dlfcn.h> | 15 #include <dlfcn.h> |
16 #endif | 16 #endif |
17 | 17 |
18 namespace { | 18 namespace base { |
| 19 |
19 class ProcessUtilTest : public MultiProcessTest { | 20 class ProcessUtilTest : public MultiProcessTest { |
20 }; | 21 }; |
21 | 22 |
22 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { | 23 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { |
23 return 0; | 24 return 0; |
24 } | 25 } |
25 | 26 |
26 TEST_F(ProcessUtilTest, SpawnChild) { | 27 TEST_F(ProcessUtilTest, SpawnChild) { |
27 ProcessHandle handle = this->SpawnChild(L"SimpleChildProcess"); | 28 ProcessHandle handle = this->SpawnChild(L"SimpleChildProcess"); |
28 | 29 |
29 ASSERT_NE(static_cast<ProcessHandle>(NULL), handle); | 30 ASSERT_NE(static_cast<ProcessHandle>(NULL), handle); |
30 EXPECT_TRUE(process_util::WaitForSingleProcess(handle, 1000)); | 31 EXPECT_TRUE(WaitForSingleProcess(handle, 1000)); |
31 } | 32 } |
32 | 33 |
33 MULTIPROCESS_TEST_MAIN(SlowChildProcess) { | 34 MULTIPROCESS_TEST_MAIN(SlowChildProcess) { |
34 // Sleep until file "SlowChildProcess.die" is created. | 35 // Sleep until file "SlowChildProcess.die" is created. |
35 FILE *fp; | 36 FILE *fp; |
36 do { | 37 do { |
37 PlatformThread::Sleep(100); | 38 PlatformThread::Sleep(100); |
38 fp = fopen("SlowChildProcess.die", "r"); | 39 fp = fopen("SlowChildProcess.die", "r"); |
39 } while (!fp); | 40 } while (!fp); |
40 fclose(fp); | 41 fclose(fp); |
41 remove("SlowChildProcess.die"); | 42 remove("SlowChildProcess.die"); |
42 exit(0); | 43 exit(0); |
43 return 0; | 44 return 0; |
44 } | 45 } |
45 | 46 |
46 #if defined(OS_WIN) | 47 #if defined(OS_WIN) |
47 #define EXE_SUFFIX L".exe" | 48 #define EXE_SUFFIX L".exe" |
48 #else | 49 #else |
49 #define EXE_SUFFIX L"" | 50 #define EXE_SUFFIX L"" |
50 #endif | 51 #endif |
51 | 52 |
52 // TODO(port): finish port on Mac | 53 // TODO(port): finish port on Mac |
53 #if !defined(OS_MACOSX) | 54 #if !defined(OS_MACOSX) |
54 TEST_F(ProcessUtilTest, KillSlowChild) { | 55 TEST_F(ProcessUtilTest, KillSlowChild) { |
55 remove("SlowChildProcess.die"); | 56 remove("SlowChildProcess.die"); |
56 int oldcount = process_util::GetProcessCount(L"base_unittests" EXE_SUFFIX, 0); | 57 int oldcount = GetProcessCount(L"base_unittests" EXE_SUFFIX, 0); |
57 | 58 |
58 ProcessHandle handle = this->SpawnChild(L"SlowChildProcess"); | 59 ProcessHandle handle = this->SpawnChild(L"SlowChildProcess"); |
59 | 60 |
60 ASSERT_NE(static_cast<ProcessHandle>(NULL), handle); | 61 ASSERT_NE(static_cast<ProcessHandle>(NULL), handle); |
61 EXPECT_EQ(oldcount+1, process_util::GetProcessCount(L"base_unittests" EXE_SUFF
IX, 0)); | 62 EXPECT_EQ(oldcount+1, GetProcessCount(L"base_unittests" EXE_SUFFIX, 0)); |
62 FILE *fp = fopen("SlowChildProcess.die", "w"); | 63 FILE *fp = fopen("SlowChildProcess.die", "w"); |
63 fclose(fp); | 64 fclose(fp); |
64 // TODO(port): do something less racy here | 65 // TODO(port): do something less racy here |
65 PlatformThread::Sleep(1000); | 66 PlatformThread::Sleep(1000); |
66 EXPECT_EQ(oldcount, process_util::GetProcessCount(L"base_unittests" EXE_SUFFIX
, 0)); | 67 EXPECT_EQ(oldcount, GetProcessCount(L"base_unittests" EXE_SUFFIX, 0)); |
67 } | 68 } |
68 #endif | 69 #endif |
69 | 70 |
70 // TODO(estade): if possible, port these 2 tests. | 71 // TODO(estade): if possible, port these 2 tests. |
71 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
72 TEST_F(ProcessUtilTest, EnableLFH) { | 73 TEST_F(ProcessUtilTest, EnableLFH) { |
73 ASSERT_TRUE(process_util::EnableLowFragmentationHeap()); | 74 ASSERT_TRUE(EnableLowFragmentationHeap()); |
74 if (IsDebuggerPresent()) { | 75 if (IsDebuggerPresent()) { |
75 // Under these conditions, LFH can't be enabled. There's no point to test | 76 // Under these conditions, LFH can't be enabled. There's no point to test |
76 // anything. | 77 // anything. |
77 const char* no_debug_env = getenv("_NO_DEBUG_HEAP"); | 78 const char* no_debug_env = getenv("_NO_DEBUG_HEAP"); |
78 if (!no_debug_env || strcmp(no_debug_env, "1")) | 79 if (!no_debug_env || strcmp(no_debug_env, "1")) |
79 return; | 80 return; |
80 } | 81 } |
81 HANDLE heaps[1024] = { 0 }; | 82 HANDLE heaps[1024] = { 0 }; |
82 unsigned number_heaps = GetProcessHeaps(1024, heaps); | 83 unsigned number_heaps = GetProcessHeaps(1024, heaps); |
83 EXPECT_GT(number_heaps, 0u); | 84 EXPECT_GT(number_heaps, 0u); |
(...skipping 10 matching lines...) Expand all Loading... |
94 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not | 95 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not |
95 // supported on the LFH. | 96 // supported on the LFH. |
96 | 97 |
97 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag. | 98 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag. |
98 EXPECT_LE(flag, 2u); | 99 EXPECT_LE(flag, 2u); |
99 EXPECT_NE(flag, 1u); | 100 EXPECT_NE(flag, 1u); |
100 } | 101 } |
101 } | 102 } |
102 | 103 |
103 TEST_F(ProcessUtilTest, CalcFreeMemory) { | 104 TEST_F(ProcessUtilTest, CalcFreeMemory) { |
104 process_util::ProcessMetrics* metrics = | 105 ProcessMetrics* metrics = |
105 process_util::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess()); | 106 ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess()); |
106 ASSERT_TRUE(NULL != metrics); | 107 ASSERT_TRUE(NULL != metrics); |
107 | 108 |
108 // Typical values here is ~1900 for total and ~1000 for largest. Obviously | 109 // Typical values here is ~1900 for total and ~1000 for largest. Obviously |
109 // it depends in what other tests have done to this process. | 110 // it depends in what other tests have done to this process. |
110 process_util::FreeMBytes free_mem1 = {0}; | 111 FreeMBytes free_mem1 = {0}; |
111 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1)); | 112 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1)); |
112 EXPECT_LT(10u, free_mem1.total); | 113 EXPECT_LT(10u, free_mem1.total); |
113 EXPECT_LT(10u, free_mem1.largest); | 114 EXPECT_LT(10u, free_mem1.largest); |
114 EXPECT_GT(2048u, free_mem1.total); | 115 EXPECT_GT(2048u, free_mem1.total); |
115 EXPECT_GT(2048u, free_mem1.largest); | 116 EXPECT_GT(2048u, free_mem1.largest); |
116 EXPECT_GE(free_mem1.total, free_mem1.largest); | 117 EXPECT_GE(free_mem1.total, free_mem1.largest); |
117 EXPECT_TRUE(NULL != free_mem1.largest_ptr); | 118 EXPECT_TRUE(NULL != free_mem1.largest_ptr); |
118 | 119 |
119 // Allocate 20M and check again. It should have gone down. | 120 // Allocate 20M and check again. It should have gone down. |
120 const int kAllocMB = 20; | 121 const int kAllocMB = 20; |
121 char* alloc = new char[kAllocMB * 1024 * 1024]; | 122 char* alloc = new char[kAllocMB * 1024 * 1024]; |
122 EXPECT_TRUE(NULL != alloc); | 123 EXPECT_TRUE(NULL != alloc); |
123 | 124 |
124 size_t expected_total = free_mem1.total - kAllocMB; | 125 size_t expected_total = free_mem1.total - kAllocMB; |
125 size_t expected_largest = free_mem1.largest; | 126 size_t expected_largest = free_mem1.largest; |
126 | 127 |
127 process_util::FreeMBytes free_mem2 = {0}; | 128 FreeMBytes free_mem2 = {0}; |
128 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2)); | 129 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2)); |
129 EXPECT_GE(free_mem2.total, free_mem2.largest); | 130 EXPECT_GE(free_mem2.total, free_mem2.largest); |
130 EXPECT_GE(expected_total, free_mem2.total); | 131 EXPECT_GE(expected_total, free_mem2.total); |
131 EXPECT_GE(expected_largest, free_mem2.largest); | 132 EXPECT_GE(expected_largest, free_mem2.largest); |
132 EXPECT_TRUE(NULL != free_mem2.largest_ptr); | 133 EXPECT_TRUE(NULL != free_mem2.largest_ptr); |
133 | 134 |
134 delete[] alloc; | 135 delete[] alloc; |
135 delete metrics; | 136 delete metrics; |
136 } | 137 } |
137 #endif // defined(OS_WIN) | 138 #endif // defined(OS_WIN) |
138 | 139 |
139 } // namespace | 140 } // namespace base |
OLD | NEW |