Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: util/file/file_io_test.cc

Issue 1001673002: Add Locking calls to file_io.h plus implementations and test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/file/file_io.h"
16
17 #include "base/files/file_path.h"
18 #include "gtest/gtest.h"
19 #include "util/test/scoped_temp_dir.h"
20
21 namespace crashpad {
22 namespace test {
23 namespace {
24
25 TEST(FileIO, FileShareMode) {
26 ScopedTempDir temp_dir;
27 base::FilePath shared_file =
28 temp_dir.path().Append(FILE_PATH_LITERAL("shared_file"));
29 auto handle1 = ScopedFileHandle(LoggingOpenFileForWrite(
30 shared_file, FileWriteMode::kReuseOrCreate, FilePermissions::kOwnerOnly));
Mark Mentovai 2015/03/12 04:47:42 The first one should use kCreateOrFail. Same on li
scottmg 2015/03/19 22:06:20 Done.
31 ASSERT_NE(handle1, kInvalidFileHandle);
32 auto handle2 = ScopedFileHandle(LoggingOpenFileForWrite(
33 shared_file, FileWriteMode::kReuseOrCreate, FilePermissions::kOwnerOnly));
34 EXPECT_NE(handle2, kInvalidFileHandle);
35 }
36
37 TEST(FileIO, MultipleSharedLocks) {
38 ScopedTempDir temp_dir;
39 base::FilePath shared_file =
40 temp_dir.path().Append(FILE_PATH_LITERAL("file_to_lock"));
41
42 auto handle1 = ScopedFileHandle(LoggingOpenFileForWrite(
43 shared_file, FileWriteMode::kReuseOrCreate, FilePermissions::kOwnerOnly));
Mark Mentovai 2015/03/12 04:47:42 ASSERT_NE(handle1, kInvalidFileHandle) after this,
scottmg 2015/03/19 22:06:20 Done.
44 EXPECT_TRUE(LoggingLockFile(handle1.get(), FileLocking::kShared));
45
46 auto handle2 = ScopedFileHandle(LoggingOpenFileForWrite(
47 shared_file, FileWriteMode::kReuseOrCreate, FilePermissions::kOwnerOnly));
48 EXPECT_TRUE(LoggingLockFile(handle2.get(), FileLocking::kShared));
49
50 EXPECT_TRUE(LoggingUnlockFile(handle1.get()));
51 EXPECT_TRUE(LoggingUnlockFile(handle2.get()));
52 }
53
54 struct ThreadMainInfo {
55 #if defined(OS_POSIX)
Mark Mentovai 2015/03/12 04:47:42 OK for now. Robert and I were talking about needin
Robert Sesek 2015/03/12 15:28:03 I think it's probably time to create this abstract
scottmg 2015/03/19 22:06:20 Added some version of this to util/test/thread.h.
56 pthread_t pthread;
57 #elif defined(OS_WIN)
58 HANDLE thread;
59 #endif
60 FileHandle file;
Mark Mentovai 2015/03/12 04:47:42 ScopedFileHandle?
scottmg 2015/03/19 22:06:20 Done.
61 int iterations;
62 int* shared_counter;
63 };
64
65 #if defined(OS_POSIX)
66 void*
67 #elif defined(OS_WIN)
68 DWORD WINAPI
69 #endif // OS_POSIX
70 ThreadMain(void* argument) {
71 ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument);
72 for (int i = 0; i < info->iterations; ++i) {
73 EXPECT_TRUE(LoggingLockFile(info->file, FileLocking::kExclusive));
Mark Mentovai 2015/03/12 04:47:42 You haven’t done anything to test that an exclusiv
scottmg 2015/03/19 22:06:20 Done.
74 *info->shared_counter += 1;
Mark Mentovai 2015/03/12 04:47:42 For the case where the the other threads will be u
scottmg 2015/03/19 22:06:20 Done.
75 EXPECT_TRUE(LoggingUnlockFile(info->file));
76 }
77
78 #if defined(OS_POSIX)
79 return nullptr;
80 #elif defined(OS_WIN)
81 return 0;
82 #endif // OS_POSIX
83 }
84
85 void StartThread(ThreadMainInfo* info) {
86 #if defined(OS_POSIX)
87 int rv = pthread_create(&info->pthread, nullptr, ThreadMain, info);
88 ASSERT_EQ(0, rv) << "pthread_create";
Mark Mentovai 2015/03/12 04:47:42 From util/test/errors.h, use ErrnoMessage, like
scottmg 2015/03/19 22:06:19 Done.
89 #elif defined(OS_WIN)
90 info->thread = CreateThread(nullptr, 0, ThreadMain, info, 0, nullptr);
91 ASSERT_NE(nullptr, info->thread) << "CreateThread";
92 #endif // OS_POSIX
93 }
94
95 void JoinThread(ThreadMainInfo* info) {
96 #if defined(OS_POSIX)
97 int rv = pthread_join(info->pthread, nullptr);
98 EXPECT_EQ(0, rv) << "pthread_join";
99 #elif defined(OS_WIN)
100 DWORD result = WaitForSingleObject(info->thread, INFINITE);
101 EXPECT_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject";
102 #endif // OS_POSIX
103 }
104
105 TEST(FileIO, ExclusiveExcludes) {
106 ScopedTempDir temp_dir;
107 base::FilePath shared_file =
108 temp_dir.path().Append(FILE_PATH_LITERAL("file_to_lock"));
109
110 auto initial = ScopedFileHandle(LoggingOpenFileForWrite(
111 shared_file, FileWriteMode::kReuseOrCreate, FilePermissions::kOwnerOnly));
112 ASSERT_TRUE(LoggingLockFile(initial.get(), FileLocking::kExclusive));
113
114 int shared_counter = 0;
115
116 const int kThreads = 10;
117 ThreadMainInfo info[kThreads];
118 int iterations = 0;
Mark Mentovai 2015/03/12 04:47:42 expected_iterations makes it clearer. If shared_co
scottmg 2015/03/19 22:06:20 Done.
119 for (int index = 0; index < kThreads; ++index) {
Mark Mentovai 2015/03/12 04:47:42 #include "base/basictypes.h" and use arraysize(inf
scottmg 2015/03/19 22:06:19 Done.
120 info[index].file = LoggingOpenFileForWrite(shared_file,
121 FileWriteMode::kReuseOrCreate,
122 FilePermissions::kOwnerOnly);
123 ASSERT_NE(info[index].file, kInvalidFileHandle);
124 info[index].iterations = index * 10;
125 info[index].shared_counter = &shared_counter;
126 iterations += info[index].iterations;
127
128 ASSERT_NO_FATAL_FAILURE(StartThread(&info[index]));
129 }
130
131 EXPECT_EQ(0, shared_counter);
132
133 ASSERT_TRUE(LoggingUnlockFile(initial.get()));
134
135 for (int index = 0; index < kThreads; ++index) {
136 JoinThread(&info[index]);
137 }
138
139 EXPECT_EQ(shared_counter, iterations);
Mark Mentovai 2015/03/12 04:47:42 These arguments are swapped.
scottmg 2015/03/19 22:06:20 Done.
140
141 for (int index = 0; index < kThreads; ++index) {
142 EXPECT_TRUE(LoggingCloseFile(info[index].file));
Mark Mentovai 2015/03/12 04:47:42 This could have happened in ThreadMain() instead o
scottmg 2015/03/19 22:06:20 Removed via ScopedFileHandle.
143 }
144 }
145
146 } // namespace
147 } // namespace test
148 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698