2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
13 #include "webrtc/base/event.h"
14 #include "webrtc/base/filelock.h"
15 #include "webrtc/base/fileutils.h"
16 #include "webrtc/base/gunit.h"
17 #include "webrtc/base/pathutils.h"
18 #include "webrtc/base/scoped_ptr.h"
19 #include "webrtc/base/thread.h"
20 #include "webrtc/test/testsupport/gtest_disable.h"
24 const static std::string kLockFile = "TestLockFile";
25 const static int kTimeoutMS = 5000;
27 class FileLockTest : public testing::Test, public Runnable {
29 FileLockTest() : done_(false, false), thread_lock_failed_(false) {
32 virtual void Run(Thread* t) {
33 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
34 // The lock is already owned by the main thread of
35 // this test, therefore the TryLock(...) call should fail.
36 thread_lock_failed_ = lock.get() == NULL;
41 virtual void SetUp() {
42 thread_lock_failed_ = false;
43 Filesystem::GetAppTempFolder(&temp_dir_);
44 temp_file_ = Pathname(temp_dir_.pathname(), kLockFile);
49 done_.Wait(kTimeoutMS);
54 bool thread_lock_failed_;
59 TEST_F(FileLockTest, TestLockFileDeleted) {
60 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
61 EXPECT_TRUE(lock.get() != NULL);
62 EXPECT_FALSE(Filesystem::IsAbsent(temp_file_.pathname()));
64 EXPECT_TRUE(Filesystem::IsAbsent(temp_file_.pathname()));
67 TEST_F(FileLockTest, TestLock) {
68 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
69 EXPECT_TRUE(lock.get() != NULL);
72 TEST_F(FileLockTest, TestLockX2) {
73 scoped_ptr<FileLock> lock1(FileLock::TryLock(temp_file_.pathname()));
74 EXPECT_TRUE(lock1.get() != NULL);
76 scoped_ptr<FileLock> lock2(FileLock::TryLock(temp_file_.pathname()));
77 EXPECT_TRUE(lock2.get() == NULL);
80 TEST_F(FileLockTest, DISABLED_ON_MAC(TestThreadedLock)) {
81 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
82 EXPECT_TRUE(lock.get() != NULL);
85 EXPECT_TRUE(thread_lock_failed_);