ProtectedTests.swift 11 KB

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