Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/environment.h" | 6 #include "base/environment.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/test/multiprocess_test.h" | 11 #include "base/test/multiprocess_test.h" |
| 12 #include "base/test/scoped_environment_variable.h" | |
| 12 #include "base/time.h" | 13 #include "base/time.h" |
| 13 #include "chrome/common/multi_process_lock.h" | 14 #include "chrome/common/multi_process_lock.h" |
| 14 #include "testing/multiprocess_func_list.h" | 15 #include "testing/multiprocess_func_list.h" |
| 15 | 16 |
| 16 class MultiProcessLockTest : public base::MultiProcessTest { | 17 class MultiProcessLockTest : public base::MultiProcessTest { |
| 17 public: | 18 public: |
| 18 static const char kLockEnviromentVarName[]; | 19 static const char kLockEnviromentVarName[]; |
| 19 | 20 |
| 20 class ScopedEnvironmentVariable { | |
| 21 public: | |
| 22 ScopedEnvironmentVariable(const std::string &name, | |
| 23 const std::string &value) | |
| 24 : name_(name), environment_(base::Environment::Create()) { | |
|
jar (doing other things)
2012/11/16 20:29:23
nit: you could fix this up to have one initializer
| |
| 25 environment_->SetVar(name_.c_str(), value); | |
| 26 } | |
| 27 ~ScopedEnvironmentVariable() { | |
| 28 environment_->UnSetVar(name_.c_str()); | |
| 29 } | |
| 30 | |
| 31 private: | |
| 32 std::string name_; | |
| 33 scoped_ptr<base::Environment> environment_; | |
| 34 DISALLOW_COPY_AND_ASSIGN(ScopedEnvironmentVariable); | |
| 35 }; | |
| 36 | |
| 37 std::string GenerateLockName(); | 21 std::string GenerateLockName(); |
| 38 void ExpectLockIsLocked(const std::string &name); | 22 void ExpectLockIsLocked(const std::string &name); |
| 39 void ExpectLockIsUnlocked(const std::string &name); | 23 void ExpectLockIsUnlocked(const std::string &name); |
| 40 }; | 24 }; |
| 41 | 25 |
| 42 const char MultiProcessLockTest::kLockEnviromentVarName[] | 26 const char MultiProcessLockTest::kLockEnviromentVarName[] |
| 43 = "MULTI_PROCESS_TEST_LOCK_NAME"; | 27 = "MULTI_PROCESS_TEST_LOCK_NAME"; |
| 44 | 28 |
| 45 std::string MultiProcessLockTest::GenerateLockName() { | 29 std::string MultiProcessLockTest::GenerateLockName() { |
| 46 base::Time now = base::Time::NowFromSystemTime(); | 30 base::Time now = base::Time::NowFromSystemTime(); |
| 47 return base::StringPrintf("multi_process_test_lock %lf%lf", | 31 return base::StringPrintf("multi_process_test_lock %lf%lf", |
| 48 now.ToDoubleT(), base::RandDouble()); | 32 now.ToDoubleT(), base::RandDouble()); |
| 49 } | 33 } |
| 50 | 34 |
| 51 void MultiProcessLockTest::ExpectLockIsLocked(const std::string &name) { | 35 void MultiProcessLockTest::ExpectLockIsLocked(const std::string &name) { |
| 52 ScopedEnvironmentVariable var(kLockEnviromentVarName, name); | 36 base::ScopedEnvironmentVariable var(kLockEnviromentVarName, name); |
| 53 base::ProcessHandle handle = SpawnChild("MultiProcessLockTryFailMain", false); | 37 base::ProcessHandle handle = SpawnChild("MultiProcessLockTryFailMain", false); |
| 54 ASSERT_TRUE(handle); | 38 ASSERT_TRUE(handle); |
| 55 int exit_code = 0; | 39 int exit_code = 0; |
| 56 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 40 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
| 57 EXPECT_EQ(exit_code, 0); | 41 EXPECT_EQ(exit_code, 0); |
| 58 } | 42 } |
| 59 | 43 |
| 60 void MultiProcessLockTest::ExpectLockIsUnlocked( | 44 void MultiProcessLockTest::ExpectLockIsUnlocked( |
| 61 const std::string &name) { | 45 const std::string &name) { |
| 62 ScopedEnvironmentVariable var(kLockEnviromentVarName, name); | 46 base::ScopedEnvironmentVariable var(kLockEnviromentVarName, name); |
| 63 base::ProcessHandle handle = SpawnChild("MultiProcessLockTrySucceedMain", | 47 base::ProcessHandle handle = SpawnChild("MultiProcessLockTrySucceedMain", |
| 64 false); | 48 false); |
| 65 ASSERT_TRUE(handle); | 49 ASSERT_TRUE(handle); |
| 66 int exit_code = 0; | 50 int exit_code = 0; |
| 67 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 51 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
| 68 EXPECT_EQ(exit_code, 0); | 52 EXPECT_EQ(exit_code, 0); |
| 69 } | 53 } |
| 70 | 54 |
| 71 TEST_F(MultiProcessLockTest, BasicCreationTest) { | 55 TEST_F(MultiProcessLockTest, BasicCreationTest) { |
| 72 // Test basic creation/destruction with no lock taken | 56 // Test basic creation/destruction with no lock taken |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 MULTIPROCESS_TEST_MAIN(MultiProcessLockTrySucceedMain) { | 146 MULTIPROCESS_TEST_MAIN(MultiProcessLockTrySucceedMain) { |
| 163 std::string name; | 147 std::string name; |
| 164 scoped_ptr<base::Environment> environment(base::Environment::Create()); | 148 scoped_ptr<base::Environment> environment(base::Environment::Create()); |
| 165 EXPECT_TRUE(environment->GetVar(MultiProcessLockTest::kLockEnviromentVarName, | 149 EXPECT_TRUE(environment->GetVar(MultiProcessLockTest::kLockEnviromentVarName, |
| 166 &name)); | 150 &name)); |
| 167 scoped_ptr<MultiProcessLock> test_lock( | 151 scoped_ptr<MultiProcessLock> test_lock( |
| 168 MultiProcessLock::Create(name)); | 152 MultiProcessLock::Create(name)); |
| 169 EXPECT_TRUE(test_lock->TryLock()); | 153 EXPECT_TRUE(test_lock->TryLock()); |
| 170 return 0; | 154 return 0; |
| 171 } | 155 } |
| OLD | NEW |