This source file includes following definitions.
- SetUp
- TearDown
- IsLocked
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <errno.h>
#include <fcntl.h>
#include <sys/file.h>
#include "chrome/browser/process_singleton.h"
#include "base/file_util.h"
#include "base/files/scoped_file.h"
#include "base/path_service.h"
#include "base/posix/eintr_wrapper.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/platform_test.h"
namespace {
class ProcessSingletonMacTest : public PlatformTest {
public:
virtual void SetUp() {
PlatformTest::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
lock_path_ = temp_dir_.path().Append(chrome::kSingletonLockFilename);
}
virtual void TearDown() {
PlatformTest::TearDown();
EXPECT_FALSE(IsLocked());
}
bool IsLocked() {
base::ScopedFD fd(HANDLE_EINTR(open(lock_path_.value().c_str(), O_RDONLY)));
if (!fd.is_valid()) {
EXPECT_EQ(ENOENT, errno) << "Unexpected error opening lockfile.";
return false;
}
int rc = HANDLE_EINTR(flock(fd.get(), LOCK_EX|LOCK_NB));
if (rc != -1)
return false;
if (errno == EWOULDBLOCK)
return true;
EXPECT_EQ(EWOULDBLOCK, errno) << "Unexpected error acquiring lock.";
return false;
}
base::ScopedTempDir temp_dir_;
base::FilePath lock_path_;
};
TEST_F(ProcessSingletonMacTest, Basic) {
ProcessSingleton ps(temp_dir_.path(),
ProcessSingleton::NotificationCallback());
EXPECT_FALSE(IsLocked());
EXPECT_TRUE(ps.Create());
EXPECT_TRUE(IsLocked());
ps.Cleanup();
EXPECT_FALSE(IsLocked());
}
TEST_F(ProcessSingletonMacTest, DestructorReleases) {
EXPECT_FALSE(IsLocked());
{
ProcessSingleton ps(temp_dir_.path(),
ProcessSingleton::NotificationCallback());
EXPECT_TRUE(ps.Create());
EXPECT_TRUE(IsLocked());
}
EXPECT_FALSE(IsLocked());
}
TEST_F(ProcessSingletonMacTest, Interlock) {
ProcessSingleton ps1(temp_dir_.path(),
ProcessSingleton::NotificationCallback());
ProcessSingleton ps2(temp_dir_.path(),
ProcessSingleton::NotificationCallback());
LOG(ERROR) << "Expect two failures to obtain the lock.";
EXPECT_FALSE(IsLocked());
EXPECT_TRUE(ps1.Create());
EXPECT_TRUE(IsLocked());
EXPECT_FALSE(ps2.Create());
ps1.Cleanup();
EXPECT_FALSE(IsLocked());
EXPECT_TRUE(ps2.Create());
EXPECT_TRUE(IsLocked());
EXPECT_FALSE(ps1.Create());
ps2.Cleanup();
EXPECT_FALSE(IsLocked());
}
TEST_F(ProcessSingletonMacTest, NotifyOtherProcessOrCreate) {
ProcessSingleton ps1(temp_dir_.path(),
ProcessSingleton::NotificationCallback());
ProcessSingleton ps2(temp_dir_.path(),
ProcessSingleton::NotificationCallback());
LOG(ERROR) << "Expect two failures to obtain the lock.";
EXPECT_FALSE(IsLocked());
EXPECT_EQ(
ProcessSingleton::PROCESS_NONE,
ps1.NotifyOtherProcessOrCreate());
EXPECT_TRUE(IsLocked());
EXPECT_EQ(
ProcessSingleton::PROFILE_IN_USE,
ps2.NotifyOtherProcessOrCreate());
ps1.Cleanup();
EXPECT_FALSE(IsLocked());
EXPECT_EQ(
ProcessSingleton::PROCESS_NONE,
ps2.NotifyOtherProcessOrCreate());
EXPECT_TRUE(IsLocked());
EXPECT_EQ(
ProcessSingleton::PROFILE_IN_USE,
ps1.NotifyOtherProcessOrCreate());
ps2.Cleanup();
EXPECT_FALSE(IsLocked());
}
}