ProtectedTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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.wrappedValue
  35. protected.wrappedValue = "\(i)"
  36. }
  37. // Then
  38. XCTAssertNotEqual(protected.wrappedValue, 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 { $0 = "\(i)" }
  48. }
  49. // Then
  50. XCTAssertNotEqual(protected.wrappedValue, initialValue)
  51. }
  52. }
  53. final class ProtectedWrapperTests: BaseTestCase {
  54. @Protected var value = "value"
  55. override func setUp() {
  56. super.setUp()
  57. value = "value"
  58. }
  59. func testThatWrappedValuesAreAccessedSafely() {
  60. // Given
  61. let initialValue = value
  62. // When
  63. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  64. _ = value
  65. value = "\(i)"
  66. }
  67. // Then
  68. XCTAssertNotEqual(value, initialValue)
  69. }
  70. func testThatProjectedAPIIsAccessedSafely() {
  71. // Given
  72. let initialValue = value
  73. // When
  74. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  75. _ = $value.read { $0 }
  76. $value.write { $0 = "\(i)" }
  77. }
  78. // Then
  79. XCTAssertNotEqual(value, initialValue)
  80. }
  81. func testThatDynamicMembersAreAccessedSafely() {
  82. // Given
  83. let count = Protected<Int>(0)
  84. // When
  85. DispatchQueue.concurrentPerform(iterations: 10_000) { _ in
  86. count.wrappedValue = value.count
  87. }
  88. // Then
  89. XCTAssertEqual(count.wrappedValue, 5)
  90. }
  91. func testThatDynamicMemberPropertiesAreAccessedSafely() {
  92. // Given
  93. let string = Protected<String>("test")
  94. let count = Protected<Int>(0)
  95. // When
  96. DispatchQueue.concurrentPerform(iterations: 10_000) { _ in
  97. count.wrappedValue = string.wrappedValue.count
  98. }
  99. // Then
  100. XCTAssertEqual(string.wrappedValue.count, count.wrappedValue)
  101. }
  102. #if swift(>=5.5)
  103. func testThatLocalWrapperInstanceWorkCorrectly() {
  104. // Given
  105. @Protected var string = "test"
  106. @Protected var count = 0
  107. // When
  108. DispatchQueue.concurrentPerform(iterations: 10_000) { _ in
  109. count = string.count
  110. }
  111. // Then
  112. XCTAssertEqual(string.count, count)
  113. }
  114. #endif
  115. func testThatDynamicMembersAreSetSafely() {
  116. // Given
  117. struct Mutable { var value = "value" }
  118. let mutable = Protected<Mutable>(.init())
  119. // When
  120. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  121. mutable.value = "\(i)"
  122. }
  123. // Then
  124. XCTAssertNotEqual(mutable.wrappedValue.value, "value")
  125. }
  126. }
  127. final class ProtectedHighContentionTests: BaseTestCase {
  128. final class StringContainer {
  129. var totalStrings: Int = 10
  130. var stringArray = ["this", "is", "a", "simple", "set", "of", "test", "strings", "to", "use"]
  131. }
  132. struct StringContainerWriteState {
  133. var results: [Int] = []
  134. var completedWrites = 0
  135. var queue1Complete = false
  136. var queue2Complete = false
  137. }
  138. struct StringContainerReadState {
  139. var results1: [Int] = []
  140. var results2: [Int] = []
  141. var queue1Complete = false
  142. var queue2Complete = false
  143. }
  144. // MARK: - Properties
  145. @Protected var stringContainer = StringContainer()
  146. @Protected var stringContainerWrite = StringContainerWriteState()
  147. @Protected var stringContainerRead = StringContainerReadState()
  148. func testConcurrentReadWriteBlocks() {
  149. // Given
  150. let totalWrites = 4000
  151. let totalReads = 10_000
  152. let writeExpectation = expectation(description: "all parallel writes should complete before timeout")
  153. let readExpectation = expectation(description: "all parallel reads should complete before timeout")
  154. var writerQueueResults: [Int] = []
  155. var completedWritesCount = 0
  156. var readerQueueResults1: [Int] = []
  157. var readerQueueResults2: [Int] = []
  158. // When
  159. executeWriteOperationsInParallel(totalOperationsToExecute: totalWrites) { results, completedOperationCount in
  160. writerQueueResults = results
  161. completedWritesCount = completedOperationCount
  162. writeExpectation.fulfill()
  163. }
  164. executeReadOperationsInParallel(totalOperationsToExecute: totalReads) { results1, results2 in
  165. readerQueueResults1 = results1
  166. readerQueueResults2 = results2
  167. readExpectation.fulfill()
  168. }
  169. waitForExpectations(timeout: timeout, handler: nil)
  170. // Then
  171. XCTAssertEqual(readerQueueResults1.count, totalReads)
  172. XCTAssertEqual(readerQueueResults2.count, totalReads)
  173. XCTAssertEqual(writerQueueResults.count, totalWrites)
  174. XCTAssertEqual(completedWritesCount, totalWrites)
  175. readerQueueResults1.forEach { XCTAssertEqual($0, 10) }
  176. readerQueueResults2.forEach { XCTAssertEqual($0, 10) }
  177. writerQueueResults.forEach { XCTAssertEqual($0, 10) }
  178. }
  179. private func executeWriteOperationsInParallel(totalOperationsToExecute totalOperations: Int,
  180. completion: @escaping ([Int], Int) -> Void) {
  181. let queue1 = DispatchQueue(label: "com.alamofire.testWriterQueue1")
  182. let queue2 = DispatchQueue(label: "com.alamofire.testWriterQueue2")
  183. for _ in 1...totalOperations {
  184. queue1.async {
  185. // Moves the last string element to the beginning of the string array
  186. let result: Int = self.$stringContainer.write { stringContainer in
  187. let lastElement = stringContainer.stringArray.removeLast()
  188. stringContainer.totalStrings = stringContainer.stringArray.count
  189. stringContainer.stringArray.insert(lastElement, at: 0)
  190. stringContainer.totalStrings = stringContainer.stringArray.count
  191. return stringContainer.totalStrings
  192. }
  193. self.$stringContainerWrite.write { mutableState in
  194. mutableState.results.append(result)
  195. if mutableState.results.count == totalOperations {
  196. mutableState.queue1Complete = true
  197. if mutableState.queue2Complete {
  198. completion(mutableState.results, mutableState.completedWrites)
  199. }
  200. }
  201. }
  202. }
  203. queue2.async {
  204. // Moves the first string element to the end of the string array
  205. self.$stringContainer.write { stringContainer in
  206. let firstElement = stringContainer.stringArray.remove(at: 0)
  207. stringContainer.totalStrings = stringContainer.stringArray.count
  208. stringContainer.stringArray.append(firstElement)
  209. stringContainer.totalStrings = stringContainer.stringArray.count
  210. }
  211. self.$stringContainerWrite.write { mutableState in
  212. mutableState.completedWrites += 1
  213. if mutableState.completedWrites == totalOperations {
  214. mutableState.queue2Complete = true
  215. if mutableState.queue1Complete {
  216. completion(mutableState.results, mutableState.completedWrites)
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. private func executeReadOperationsInParallel(totalOperationsToExecute totalOperations: Int,
  224. completion: @escaping ([Int], [Int]) -> Void) {
  225. let queue1 = DispatchQueue(label: "com.alamofire.testReaderQueue1")
  226. let queue2 = DispatchQueue(label: "com.alamofire.testReaderQueue1")
  227. for _ in 1...totalOperations {
  228. queue1.async {
  229. // Reads the total string count in the string array
  230. // Using the wrapped value (no $) instead of the wrapper itself triggers the thread sanitizer.
  231. let result = self.$stringContainer.totalStrings
  232. self.$stringContainerRead.write {
  233. $0.results1.append(result)
  234. if $0.results1.count == totalOperations {
  235. $0.queue1Complete = true
  236. if $0.queue2Complete {
  237. completion($0.results1, $0.results2)
  238. }
  239. }
  240. }
  241. }
  242. queue2.async {
  243. // Reads the total string count in the string array
  244. let result = self.$stringContainer.read { $0.totalStrings }
  245. self.$stringContainerRead.write {
  246. $0.results2.append(result)
  247. if $0.results2.count == totalOperations {
  248. $0.queue2Complete = true
  249. if $0.queue1Complete {
  250. completion($0.results1, $0.results2)
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }