1/* 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. 3 * 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. 9 */ 10 11#include <string> 12 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" 21 22namespace rtc { 23 24const static std::string kLockFile = "TestLockFile"; 25const static int kTimeoutMS = 5000; 26 27class FileLockTest : public testing::Test, public Runnable { 28 public: 29 FileLockTest() : done_(false, false), thread_lock_failed_(false) { 30 } 31 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; 37 done_.Set(); 38 } 39 40 protected: 41 virtual void SetUp() { 42 thread_lock_failed_ = false; 43 Filesystem::GetAppTempFolder(&temp_dir_); 44 temp_file_ = Pathname(temp_dir_.pathname(), kLockFile); 45 } 46 47 void LockOnThread() { 48 locker_.Start(this); 49 done_.Wait(kTimeoutMS); 50 } 51 52 Event done_; 53 Thread locker_; 54 bool thread_lock_failed_; 55 Pathname temp_dir_; 56 Pathname temp_file_; 57}; 58 59TEST_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())); 63 lock->Unlock(); 64 EXPECT_TRUE(Filesystem::IsAbsent(temp_file_.pathname())); 65} 66 67TEST_F(FileLockTest, TestLock) { 68 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname())); 69 EXPECT_TRUE(lock.get() != NULL); 70} 71 72TEST_F(FileLockTest, TestLockX2) { 73 scoped_ptr<FileLock> lock1(FileLock::TryLock(temp_file_.pathname())); 74 EXPECT_TRUE(lock1.get() != NULL); 75 76 scoped_ptr<FileLock> lock2(FileLock::TryLock(temp_file_.pathname())); 77 EXPECT_TRUE(lock2.get() == NULL); 78} 79 80TEST_F(FileLockTest, DISABLED_ON_MAC(TestThreadedLock)) { 81 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname())); 82 EXPECT_TRUE(lock.get() != NULL); 83 84 LockOnThread(); 85 EXPECT_TRUE(thread_lock_failed_); 86} 87 88} // namespace rtc 89