1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
@testable import TSCBasic
import TSCTestSupport
class LockTests: XCTestCase {
@available(*, deprecated)
func testBasics() {
// FIXME: Make this a more interesting test once we have concurrency primitives.
let lock = TSCBasic.Lock()
var count = 0
let N = 100
for _ in 0..<N {
lock.withLock {
count += 1
}
}
XCTAssertEqual(count, N)
}
func testFileLock() throws {
// Shared resource file.
try withTemporaryFile { sharedResource in
// Directory where lock file should be created.
try withTemporaryDirectory(removeTreeOnDeinit: true) { tempDirPath in
// Run the same executable multiple times and
// we can expect the final result to be sum of the
// contents we write in the shared file.
let N = 10
let threads = (1...N).map { idx in
return Thread {
let lock = FileLock(at: tempDirPath.appending(component: "TestLock"))
try! lock.withLock {
// Get thr current contents of the file if any.
let currentData: Int
if localFileSystem.exists(sharedResource.path) {
currentData = Int(try localFileSystem.readFileContents(sharedResource.path).description) ?? 0
} else {
currentData = 0
}
// Sum and write back to file.
try localFileSystem.writeFileContents(sharedResource.path, bytes: ByteString(encodingAsUTF8: String(currentData + idx)))
}
}
}
threads.forEach { $0.start() }
threads.forEach { $0.join() }
XCTAssertEqual(try localFileSystem.readFileContents(sharedResource.path).description, String((N * (N + 1) / 2 )))
}
}
}
func testReadWriteFileLock() throws {
try withTemporaryDirectory { tempDir in
let fileA = tempDir.appending(component: "fileA")
let fileB = tempDir.appending(component: "fileB")
// write initial value, since reader may start before writers and files would not exist
try localFileSystem.writeFileContents(fileA, bytes: "0")
try localFileSystem.writeFileContents(fileB, bytes: "0")
let writerThreads = (0..<100).map { _ in
return Thread {
let lock = FileLock(at: tempDir.appending(component: "foo"))
try! lock.withLock(type: .exclusive) {
// Get the current contents of the file if any.
let valueA = Int(try localFileSystem.readFileContents(fileA).description)!
// Sum and write back to file.
try localFileSystem.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
Thread.yield()
// Get the current contents of the file if any.
let valueB = Int(try localFileSystem.readFileContents(fileB).description)!
// Sum and write back to file.
try localFileSystem.writeFileContents(fileB, bytes: ByteString(encodingAsUTF8: String(valueB + 1)))
}
}
}
let readerThreads = (0..<20).map { _ in
return Thread {
let lock = FileLock(at: tempDir.appending(component: "foo"))
try! lock.withLock(type: .shared) {
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
Thread.yield()
try XCTAssertEqual(localFileSystem.readFileContents(fileA), localFileSystem.readFileContents(fileB))
}
}
}
writerThreads.forEach { $0.start() }
readerThreads.forEach { $0.start() }
writerThreads.forEach { $0.join() }
readerThreads.forEach { $0.join() }
try XCTAssertEqual(localFileSystem.readFileContents(fileA), "100")
try XCTAssertEqual(localFileSystem.readFileContents(fileB), "100")
}
}
func testFileLockLocation() throws {
do {
let fileName = UUID().uuidString
let fileToLock = try localFileSystem.homeDirectory.appending(component: fileName)
try localFileSystem.withLock(on: fileToLock, type: .exclusive) {}
// lock file expected at temp when lockFilesDirectory set to nil
// which is the case when going through localFileSystem
let tempDirectory = try localFileSystem.tempDirectory
let lockFile = try localFileSystem.getDirectoryContents(tempDirectory)
.first(where: { $0.contains(fileName) })
XCTAssertNotNil(lockFile, "expected lock file at \(tempDirectory)")
}
do {
let fileName = UUID().uuidString
let fileToLock = try localFileSystem.homeDirectory.appending(component: fileName)
try FileLock.withLock(fileToLock: fileToLock, lockFilesDirectory: nil, body: {})
// lock file expected at temp when lockFilesDirectory set to nil
let tempDirectory = try localFileSystem.tempDirectory
let lockFile = try localFileSystem.getDirectoryContents(tempDirectory)
.first(where: { $0.contains(fileName) })
XCTAssertNotNil(lockFile, "expected lock file at \(tempDirectory)")
}
do {
try withTemporaryDirectory { tempDir in
let fileName = UUID().uuidString
let fileToLock = try localFileSystem.homeDirectory.appending(component: fileName)
try FileLock.withLock(fileToLock: fileToLock, lockFilesDirectory: tempDir, body: {})
// lock file expected at specified directory when lockFilesDirectory is set to a valid directory
let lockFile = try localFileSystem.getDirectoryContents(tempDir)
.first(where: { $0.contains(fileName) })
XCTAssertNotNil(lockFile, "expected lock file at \(tempDir)")
}
}
do {
let fileName = UUID().uuidString
let fileToLock = try localFileSystem.homeDirectory.appending(component: fileName)
let lockFilesDirectory = try localFileSystem.homeDirectory.appending(component: UUID().uuidString)
XCTAssertThrows(FileSystemError(.noEntry, lockFilesDirectory)) {
try FileLock.withLock(fileToLock: fileToLock, lockFilesDirectory: lockFilesDirectory, body: {})
}
}
do {
let fileName = UUID().uuidString
let fileToLock = try localFileSystem.homeDirectory.appending(component: fileName)
let lockFilesDirectory = try localFileSystem.homeDirectory.appending(component: UUID().uuidString)
try localFileSystem.writeFileContents(lockFilesDirectory, bytes: [])
XCTAssertThrows(FileSystemError(.notDirectory, lockFilesDirectory)) {
try FileLock.withLock(fileToLock: fileToLock, lockFilesDirectory: lockFilesDirectory, body: {})
}
}
}
}
|