blob: eecbf07da6951c0b5230d6ff6fbf1cbd3c241187 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
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
21namespace rtc {
22
23const static std::string kLockFile = "TestLockFile";
24const static int kTimeoutMS = 5000;
25
26class FileLockTest : public testing::Test, public Runnable {
27 public:
28 FileLockTest() : done_(false, false), thread_lock_failed_(false) {
29 }
30
31 virtual void Run(Thread* t) {
32 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
33 // The lock is already owned by the main thread of
34 // this test, therefore the TryLock(...) call should fail.
35 thread_lock_failed_ = lock.get() == NULL;
36 done_.Set();
37 }
38
39 protected:
40 virtual void SetUp() {
41 thread_lock_failed_ = false;
42 Filesystem::GetAppTempFolder(&temp_dir_);
43 temp_file_ = Pathname(temp_dir_.pathname(), kLockFile);
44 }
45
46 void LockOnThread() {
47 locker_.Start(this);
48 done_.Wait(kTimeoutMS);
49 }
50
51 Event done_;
52 Thread locker_;
53 bool thread_lock_failed_;
54 Pathname temp_dir_;
55 Pathname temp_file_;
56};
57
58TEST_F(FileLockTest, TestLockFileDeleted) {
59 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
60 EXPECT_TRUE(lock.get() != NULL);
61 EXPECT_FALSE(Filesystem::IsAbsent(temp_file_.pathname()));
62 lock->Unlock();
63 EXPECT_TRUE(Filesystem::IsAbsent(temp_file_.pathname()));
64}
65
66TEST_F(FileLockTest, TestLock) {
67 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
68 EXPECT_TRUE(lock.get() != NULL);
69}
70
71TEST_F(FileLockTest, TestLockX2) {
72 scoped_ptr<FileLock> lock1(FileLock::TryLock(temp_file_.pathname()));
73 EXPECT_TRUE(lock1.get() != NULL);
74
75 scoped_ptr<FileLock> lock2(FileLock::TryLock(temp_file_.pathname()));
76 EXPECT_TRUE(lock2.get() == NULL);
77}
78
79TEST_F(FileLockTest, TestThreadedLock) {
80 scoped_ptr<FileLock> lock(FileLock::TryLock(temp_file_.pathname()));
81 EXPECT_TRUE(lock.get() != NULL);
82
83 LockOnThread();
84 EXPECT_TRUE(thread_lock_failed_);
85}
86
87} // namespace rtc