ProtectedTests.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. }
  71. final class ProtectedHighContentionTests: BaseTestCase {
  72. final class StringContainer {
  73. var totalStrings: Int = 10
  74. var stringArray = ["this", "is", "a", "simple", "set", "of", "test", "strings", "to", "use"]
  75. }
  76. struct StringContainerWriteState {
  77. var results: [Int] = []
  78. var completedWrites = 0
  79. var queue1Complete = false
  80. var queue2Complete = false
  81. }
  82. struct StringContainerReadState {
  83. var results1: [Int] = []
  84. var results2: [Int] = []
  85. var queue1Complete = false
  86. var queue2Complete = false
  87. }
  88. // MARK: - Properties
  89. let stringContainer = Protected(StringContainer())
  90. let stringContainerWrite = Protected(StringContainerWriteState())
  91. let stringContainerRead = Protected(StringContainerReadState())
  92. func testConcurrentReadWriteBlocks() {
  93. // Given
  94. let totalWrites = 4000
  95. let totalReads = 10_000
  96. let writeExpectation = expectation(description: "all parallel writes should complete before timeout")
  97. let readExpectation = expectation(description: "all parallel reads should complete before timeout")
  98. var writerQueueResults: [Int] = []
  99. var completedWritesCount = 0
  100. var readerQueueResults1: [Int] = []
  101. var readerQueueResults2: [Int] = []
  102. // When
  103. executeWriteOperationsInParallel(totalOperationsToExecute: totalWrites) { results, completedOperationCount in
  104. writerQueueResults = results
  105. completedWritesCount = completedOperationCount
  106. writeExpectation.fulfill()
  107. }
  108. executeReadOperationsInParallel(totalOperationsToExecute: totalReads) { results1, results2 in
  109. readerQueueResults1 = results1
  110. readerQueueResults2 = results2
  111. readExpectation.fulfill()
  112. }
  113. waitForExpectations(timeout: timeout, handler: nil)
  114. // Then
  115. XCTAssertEqual(readerQueueResults1.count, totalReads)
  116. XCTAssertEqual(readerQueueResults2.count, totalReads)
  117. XCTAssertEqual(writerQueueResults.count, totalWrites)
  118. XCTAssertEqual(completedWritesCount, totalWrites)
  119. readerQueueResults1.forEach { XCTAssertEqual($0, 10) }
  120. readerQueueResults2.forEach { XCTAssertEqual($0, 10) }
  121. writerQueueResults.forEach { XCTAssertEqual($0, 10) }
  122. }
  123. private func executeWriteOperationsInParallel(totalOperationsToExecute totalOperations: Int,
  124. completion: @escaping ([Int], Int) -> Void) {
  125. let queue1 = DispatchQueue(label: "com.alamofire.testWriterQueue1")
  126. let queue2 = DispatchQueue(label: "com.alamofire.testWriterQueue2")
  127. for _ in 1...totalOperations {
  128. queue1.async {
  129. // Moves the last string element to the beginning of the string array
  130. let result: Int = self.stringContainer.write { stringContainer in
  131. let lastElement = stringContainer.stringArray.removeLast()
  132. stringContainer.totalStrings = stringContainer.stringArray.count
  133. stringContainer.stringArray.insert(lastElement, at: 0)
  134. stringContainer.totalStrings = stringContainer.stringArray.count
  135. return stringContainer.totalStrings
  136. }
  137. self.stringContainerWrite.write { mutableState in
  138. mutableState.results.append(result)
  139. if mutableState.results.count == totalOperations {
  140. mutableState.queue1Complete = true
  141. if mutableState.queue2Complete {
  142. completion(mutableState.results, mutableState.completedWrites)
  143. }
  144. }
  145. }
  146. }
  147. queue2.async {
  148. // Moves the first string element to the end of the string array
  149. self.stringContainer.write { stringContainer in
  150. let firstElement = stringContainer.stringArray.remove(at: 0)
  151. stringContainer.totalStrings = stringContainer.stringArray.count
  152. stringContainer.stringArray.append(firstElement)
  153. stringContainer.totalStrings = stringContainer.stringArray.count
  154. }
  155. self.stringContainerWrite.write { mutableState in
  156. mutableState.completedWrites += 1
  157. if mutableState.completedWrites == totalOperations {
  158. mutableState.queue2Complete = true
  159. if mutableState.queue1Complete {
  160. completion(mutableState.results, mutableState.completedWrites)
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. private func executeReadOperationsInParallel(totalOperationsToExecute totalOperations: Int,
  168. completion: @escaping ([Int], [Int]) -> Void) {
  169. let queue1 = DispatchQueue(label: "com.alamofire.testReaderQueue1")
  170. let queue2 = DispatchQueue(label: "com.alamofire.testReaderQueue1")
  171. for _ in 1...totalOperations {
  172. queue1.async {
  173. let result = self.stringContainer.read(\.totalStrings)
  174. self.stringContainerRead.write {
  175. $0.results1.append(result)
  176. if $0.results1.count == totalOperations {
  177. $0.queue1Complete = true
  178. if $0.queue2Complete {
  179. completion($0.results1, $0.results2)
  180. }
  181. }
  182. }
  183. }
  184. queue2.async {
  185. let result = self.stringContainer.read(\.totalStrings)
  186. self.stringContainerRead.write {
  187. $0.results2.append(result)
  188. if $0.results2.count == totalOperations {
  189. $0.queue2Complete = true
  190. if $0.queue1Complete {
  191. completion($0.results1, $0.results2)
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }