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

Side by Side Diff: util/mac/service_management_test.mm

Issue 597533002: Add, test, and use clock utilities (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: (no change) Created 6 years, 2 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
« no previous file with comments | « util/mac/service_management.cc ('k') | util/mach/mach_message_server.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/mac/service_management.h" 15 #include "util/mac/service_management.h"
16 16
17 #import <Foundation/Foundation.h> 17 #import <Foundation/Foundation.h>
18 #include <launch.h> 18 #include <launch.h>
19 #include <time.h>
20 19
21 #include <string> 20 #include <string>
22 #include <vector> 21 #include <vector>
23 22
24 #include "base/mac/foundation_util.h" 23 #include "base/mac/foundation_util.h"
25 #include "base/mac/scoped_cftyperef.h" 24 #include "base/mac/scoped_cftyperef.h"
26 #include "base/strings/stringprintf.h" 25 #include "base/strings/stringprintf.h"
27 #include "base/strings/sys_string_conversions.h" 26 #include "base/strings/sys_string_conversions.h"
28 #include "base/rand_util.h" 27 #include "base/rand_util.h"
29 #include "gtest/gtest.h" 28 #include "gtest/gtest.h"
29 #include "util/misc/clock.h"
30 #include "util/posix/process_util.h" 30 #include "util/posix/process_util.h"
31 #include "util/stdlib/objc.h" 31 #include "util/stdlib/objc.h"
32 32
33 namespace { 33 namespace {
34 34
35 using namespace crashpad; 35 using namespace crashpad;
36 36
37 // Ensures that the process with the specified PID is running, identifying it by 37 // Ensures that the process with the specified PID is running, identifying it by
38 // requiring that its argv[argc - 1] compare equal to last_arg. 38 // requiring that its argv[argc - 1] compare equal to last_arg.
39 void ExpectProcessIsRunning(pid_t pid, std::string& last_arg) { 39 void ExpectProcessIsRunning(pid_t pid, std::string& last_arg) {
40 // The process may not have called exec yet, so loop with a small delay while 40 // The process may not have called exec yet, so loop with a small delay while
41 // looking for the cookie. 41 // looking for the cookie.
42 int outer_tries = 10; 42 int outer_tries = 10;
43 std::vector<std::string> job_argv; 43 std::vector<std::string> job_argv;
44 while (outer_tries--) { 44 while (outer_tries--) {
45 // If the process is in the middle of calling exec, ProcessArgumentsForPID 45 // If the process is in the middle of calling exec, ProcessArgumentsForPID
46 // may fail. Loop with a small retry delay while waiting for the expected 46 // may fail. Loop with a small retry delay while waiting for the expected
47 // successful call. 47 // successful call.
48 int inner_tries = 10; 48 int inner_tries = 10;
49 bool success; 49 bool success;
50 do { 50 do {
51 success = ProcessArgumentsForPID(pid, &job_argv); 51 success = ProcessArgumentsForPID(pid, &job_argv);
52 if (success) { 52 if (success) {
53 break; 53 break;
54 } 54 }
55 if (inner_tries > 0) { 55 if (inner_tries > 0) {
56 timespec sleep_time; 56 SleepNanoseconds(1E6); // 1 millisecond
57 sleep_time.tv_sec = 0;
58 sleep_time.tv_nsec = 1E6; // 1 millisecond
59 nanosleep(&sleep_time, NULL);
60 } 57 }
61 } while (inner_tries--); 58 } while (inner_tries--);
62 ASSERT_TRUE(success); 59 ASSERT_TRUE(success);
63 60
64 ASSERT_TRUE(ProcessArgumentsForPID(pid, &job_argv)); 61 ASSERT_TRUE(ProcessArgumentsForPID(pid, &job_argv));
65 ASSERT_FALSE(job_argv.empty()); 62 ASSERT_FALSE(job_argv.empty());
66 if (job_argv.back() == last_arg) { 63 if (job_argv.back() == last_arg) {
67 break; 64 break;
68 } 65 }
69 66
70 if (outer_tries > 0) { 67 if (outer_tries > 0) {
71 timespec sleep_time; 68 SleepNanoseconds(1E6); // 1 millisecond
72 sleep_time.tv_sec = 0;
73 sleep_time.tv_nsec = 1E6; // 1 millisecond
74 nanosleep(&sleep_time, NULL);
75 } 69 }
76 } 70 }
77 71
78 ASSERT_FALSE(job_argv.empty()); 72 ASSERT_FALSE(job_argv.empty());
79 EXPECT_EQ(last_arg, job_argv.back()); 73 EXPECT_EQ(last_arg, job_argv.back());
80 } 74 }
81 75
82 // Ensures that the process with the specified PID is not running. Because the 76 // Ensures that the process with the specified PID is not running. Because the
83 // PID may be reused for another process, a process is only treated as running 77 // PID may be reused for another process, a process is only treated as running
84 // if its argv[argc - 1] compares equal to last_arg. 78 // if its argv[argc - 1] compares equal to last_arg.
85 void ExpectProcessIsNotRunning(pid_t pid, std::string& last_arg) { 79 void ExpectProcessIsNotRunning(pid_t pid, std::string& last_arg) {
86 // The process may not have exited yet, so loop with a small delay while 80 // The process may not have exited yet, so loop with a small delay while
87 // checking that it has exited. 81 // checking that it has exited.
88 int tries = 10; 82 int tries = 10;
89 std::vector<std::string> job_argv; 83 std::vector<std::string> job_argv;
90 while (tries--) { 84 while (tries--) {
91 if (!ProcessArgumentsForPID(pid, &job_argv)) { 85 if (!ProcessArgumentsForPID(pid, &job_argv)) {
92 // The PID was not found. 86 // The PID was not found.
93 return; 87 return;
94 } 88 }
95 89
96 // The PID was found. It may have been recycled for another process. Make 90 // The PID was found. It may have been recycled for another process. Make
97 // sure that the cookie isn’t found. 91 // sure that the cookie isn’t found.
98 ASSERT_FALSE(job_argv.empty()); 92 ASSERT_FALSE(job_argv.empty());
99 if (job_argv.back() != last_arg) { 93 if (job_argv.back() != last_arg) {
100 break; 94 break;
101 } 95 }
102 96
103 if (tries > 0) { 97 if (tries > 0) {
104 timespec sleep_time; 98 SleepNanoseconds(1E6); // 1 millisecond
105 sleep_time.tv_sec = 0;
106 sleep_time.tv_nsec = 1E6; // 1 millisecond
107 nanosleep(&sleep_time, NULL);
108 } 99 }
109 } 100 }
110 101
111 ASSERT_FALSE(job_argv.empty()); 102 ASSERT_FALSE(job_argv.empty());
112 EXPECT_NE(last_arg, job_argv.back()); 103 EXPECT_NE(last_arg, job_argv.back());
113 } 104 }
114 105
115 TEST(ServiceManagement, SubmitRemoveJob) { 106 TEST(ServiceManagement, SubmitRemoveJob) {
116 @autoreleasepool { 107 @autoreleasepool {
117 std::string cookie; 108 std::string cookie;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 149
159 // Now that the job is unloaded, a subsequent attempt to unload it should be 150 // Now that the job is unloaded, a subsequent attempt to unload it should be
160 // an error. 151 // an error.
161 EXPECT_FALSE(ServiceManagementRemoveJob(kJobLabel, false)); 152 EXPECT_FALSE(ServiceManagementRemoveJob(kJobLabel, false));
162 153
163 ExpectProcessIsNotRunning(job_pid, shell_script); 154 ExpectProcessIsNotRunning(job_pid, shell_script);
164 } 155 }
165 } 156 }
166 157
167 } // namespace 158 } // namespace
OLDNEW
« no previous file with comments | « util/mac/service_management.cc ('k') | util/mach/mach_message_server.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698