ProtectedTests.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // ProtectedTests.swift
  3. //
  4. // Copyright (c) 2020 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. @testable
  25. import Alamofire
  26. import XCTest
  27. final class ProtectedTests: BaseTestCase {
  28. func testThatProtectedValuesAreAccessedSafely() {
  29. // Given
  30. let initialValue = "value"
  31. let protected = Protected<String>(initialValue)
  32. // When
  33. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  34. _ = protected.read { $0 }
  35. protected.write("\(i)")
  36. }
  37. // Then
  38. XCTAssertNotEqual(protected.read { $0 }, initialValue)
  39. }
  40. func testThatProtectedAPIIsSafe() {
  41. // Given
  42. let initialValue = "value"
  43. let protected = Protected<String>(initialValue)
  44. // When
  45. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  46. _ = protected.read { $0 }
  47. protected.write("\(i)")
  48. }
  49. // Then
  50. XCTAssertNotEqual(protected.read { $0 }, initialValue)
  51. }
  52. }
  53. final class ProtectedWrapperTests: BaseTestCase {
  54. let value = Protected("value")
  55. override func setUp() {
  56. super.setUp()
  57. value.write("value")
  58. }
  59. func testThatWrappedValuesAreAccessedSafely() {
  60. // Given
  61. let initialValue = value.read { $0 }
  62. // When
  63. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  64. _ = value.read { $0 }
  65. value.write("\(i)")
  66. }
  67. // Then
  68. XCTAssertNotEqual(value.read { $0 }, initialValue)
  69. }
  70. func testThatDynamicMembersAreSetSafely() {
  71. // Given
  72. struct Mutable { var value = "value" }
  73. let mutable = Protected<Mutable>(.init())
  74. // When
  75. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  76. mutable.value = "\(i)"
  77. }
  78. // Then
  79. XCTAssertNotEqual(mutable.value, "value")
  80. }
  81. }
  82. final class ProtectedHighContentionTests: BaseTestCase {
  83. final class StringContainer {
  84. var totalStrings: Int = 10
  85. var stringArray = ["this", "is", "a", "simple", "set", "of", "test", "strings", "to", "use"]
  86. }
  87. struct StringContainerWriteState {
  88. var results: [Int] = []
  89. var completedWrites = 0
  90. var queue1Complete = false
  91. var queue2Complete = false
  92. }
  93. struct StringContainerReadState {
  94. var results1: [Int] = []
  95. var results2: [Int] = []
  96. var queue1Complete = false
  97. var queue2Complete = false
  98. }
  99. // MARK: - Properties
  100. let stringContainer = Protected(StringContainer())
  101. let stringContainerWrite = Protected(StringContainerWriteState())
  102. let stringContainerRead = Protected(StringContainerReadState())
  103. func testConcurrentReadWriteBlocks() {
  104. // Given
  105. let totalWrites = 4000
  106. let totalReads = 10_000
  107. let writeExpectation = expectation(description: "all parallel writes should complete before timeout")
  108. let readExpectation = expectation(description: "all parallel reads should complete before timeout")
  109. var writerQueueResults: [Int] = []
  110. var completedWritesCount = 0
  111. var readerQueueResults1: [Int] = []
  112. var readerQueueResults2: [Int] = []
  113. // When
  114. executeWriteOperationsInParallel(totalOperationsToExecute: totalWrites) { results, completedOperationCount in
  115. writerQueueResults = results
  116. completedWritesCount = completedOperationCount
  117. writeExpectation.fulfill()
  118. }
  119. executeReadOperationsInParallel(totalOperationsToExecute: totalReads) { results1, results2 in
  120. readerQueueResults1 = results1
  121. readerQueueResults2 = results2
  122. readExpectation.fulfill()
  123. }
  124. waitForExpectations(timeout: timeout, handler: nil)
  125. // Then
  126. XCTAssertEqual(readerQueueResults1.count, totalReads)
  127. XCTAssertEqual(readerQueueResults2.count, totalReads)
  128. XCTAssertEqual(writerQueueResults.count, totalWrites)
  129. XCTAssertEqual(completedWritesCount, totalWrites)
  130. readerQueueResults1.forEach { XCTAssertEqual($0, 10) }
  131. readerQueueResults2.forEach { XCTAssertEqual($0, 10) }
  132. writerQueueResults.forEach { XCTAssertEqual($0, 10) }
  133. }
  134. private func executeWriteOperationsInParallel(totalOperationsToExecute totalOperations: Int,
  135. completion: @escaping ([Int], Int) -> Void) {
  136. let queue1 = DispatchQueue(label: "com.alamofire.testWriterQueue1")
  137. let queue2 = DispatchQueue(label: "com.alamofire.testWriterQueue2")
  138. for _ in 1...totalOperations {
  139. queue1.async {
  140. // Moves the last string element to the beginning of the string array
  141. let result: Int = self.stringContainer.write { stringContainer in
  142. let lastElement = stringContainer.stringArray.removeLast()
  143. stringContainer.totalStrings = stringContainer.stringArray.count
  144. stringContainer.stringArray.insert(lastElement, at: 0)
  145. stringContainer.totalStrings = stringContainer.stringArray.count
  146. return stringContainer.totalStrings
  147. }
  148. self.stringContainerWrite.write { mutableState in
  149. mutableState.results.append(result)
  150. if mutableState.results.count == totalOperations {
  151. mutableState.queue1Complete = true
  152. if mutableState.queue2Complete {
  153. completion(mutableState.results, mutableState.completedWrites)
  154. }
  155. }
  156. }
  157. }
  158. queue2.async {
  159. // Moves the first string element to the end of the string array
  160. self.stringContainer.write { stringContainer in
  161. let firstElement = stringContainer.stringArray.remove(at: 0)
  162. stringContainer.totalStrings = stringContainer.stringArray.count
  163. stringContainer.stringArray.append(firstElement)
  164. stringContainer.totalStrings = stringContainer.stringArray.count
  165. }
  166. self.stringContainerWrite.write { mutableState in
  167. mutableState.completedWrites += 1
  168. if mutableState.completedWrites == totalOperations {
  169. mutableState.queue2Complete = true
  170. if mutableState.queue1Complete {
  171. completion(mutableState.results, mutableState.completedWrites)
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. private func executeReadOperationsInParallel(totalOperationsToExecute totalOperations: Int,
  179. completion: @escaping ([Int], [Int]) -> Void) {
  180. let queue1 = DispatchQueue(label: "com.alamofire.testReaderQueue1")
  181. let queue2 = DispatchQueue(label: "com.alamofire.testReaderQueue1")
  182. for _ in 1...totalOperations {
  183. queue1.async {
  184. // Reads the total string count in the string array
  185. // Using the wrapped value (no $) instead of the wrapper itself triggers the thread sanitizer.
  186. let result = self.stringContainer.totalStrings
  187. self.stringContainerRead.write {
  188. $0.results1.append(result)
  189. if $0.results1.count == totalOperations {
  190. $0.queue1Complete = true
  191. if $0.queue2Complete {
  192. completion($0.results1, $0.results2)
  193. }
  194. }
  195. }
  196. }
  197. queue2.async {
  198. // Reads the total string count in the string array
  199. let result = self.stringContainer.read { $0.totalStrings }
  200. self.stringContainerRead.write {
  201. $0.results2.append(result)
  202. if $0.results2.count == totalOperations {
  203. $0.queue2Complete = true
  204. if $0.queue1Complete {
  205. completion($0.results1, $0.results2)
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }