2
0

ProtectedTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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: XCTestCase {
  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: XCTestCase {
  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 testThatDynamicMembersAreSetSafely() {
  92. // Given
  93. struct Mutable { var value = "value" }
  94. let mutable = Protected<Mutable>(.init())
  95. // When
  96. DispatchQueue.concurrentPerform(iterations: 10_000) { i in
  97. mutable.value = "\(i)"
  98. }
  99. // Then
  100. XCTAssertNotEqual(mutable.wrappedValue.value, "value")
  101. }
  102. }