ProductsInfoControllerTests.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // ProductsInfoControllerTests.swift
  3. // SwiftyStoreKit
  4. //
  5. // Copyright (c) 2017 Andrea Bizzotto (bizz84@gmail.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import XCTest
  25. import Foundation
  26. @testable import SwiftyStoreKit
  27. class TestInAppProductRequest: InAppProductRequest {
  28. private let productIds: Set<String>
  29. private let callback: InAppProductRequestCallback
  30. init(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) {
  31. self.productIds = productIds
  32. self.callback = callback
  33. }
  34. func start() {
  35. }
  36. func cancel() {
  37. }
  38. func fireCallback() {
  39. callback(RetrieveResults(retrievedProducts: [], invalidProductIDs: [], error: nil))
  40. }
  41. }
  42. class TestInAppProductRequestBuilder: InAppProductRequestBuilder {
  43. var requests: [ TestInAppProductRequest ] = []
  44. var os_unfair_lock_s = os_unfair_lock()
  45. func request(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) -> InAppProductRequest {
  46. // add locks to make sure the test does not fail in preparation
  47. os_unfair_lock_lock(&self.os_unfair_lock_s)
  48. defer {
  49. os_unfair_lock_unlock(&self.os_unfair_lock_s)
  50. }
  51. let request = TestInAppProductRequest(productIds: productIds, callback: callback)
  52. requests.append(request)
  53. return request
  54. }
  55. func fireCallbacks() {
  56. requests.forEach {
  57. $0.fireCallback()
  58. }
  59. requests = []
  60. }
  61. }
  62. class ProductsInfoControllerTests: XCTestCase {
  63. let sampleProductIdentifiers: Set<String> = ["com.iap.purchase1"]
  64. // Set of in app purchases to ask in different threads
  65. let testProducts: Set<String> = ["com.iap.purchase01",
  66. "com.iap.purchase02",
  67. "com.iap.purchase03",
  68. "com.iap.purchase04",
  69. "com.iap.purchase05",
  70. "com.iap.purchase06",
  71. "com.iap.purchase07",
  72. "com.iap.purchase08",
  73. "com.iap.purchase09",
  74. "com.iap.purchase10"]
  75. func testRetrieveProductsInfo_when_calledOnce_then_completionCalledOnce() {
  76. let requestBuilder = TestInAppProductRequestBuilder()
  77. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  78. var completionCount = 0
  79. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  80. completionCount += 1
  81. }
  82. requestBuilder.fireCallbacks()
  83. XCTAssertEqual(completionCount, 1)
  84. }
  85. func testRetrieveProductsInfo_when_calledTwiceConcurrently_then_eachCompletionCalledOnce() {
  86. let requestBuilder = TestInAppProductRequestBuilder()
  87. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  88. var completionCount = 0
  89. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  90. completionCount += 1
  91. }
  92. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  93. completionCount += 1
  94. }
  95. requestBuilder.fireCallbacks()
  96. XCTAssertEqual(completionCount, 2)
  97. }
  98. func testRetrieveProductsInfo_when_calledTwiceNotConcurrently_then_eachCompletionCalledOnce() {
  99. let requestBuilder = TestInAppProductRequestBuilder()
  100. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  101. var completionCount = 0
  102. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  103. completionCount += 1
  104. }
  105. requestBuilder.fireCallbacks()
  106. XCTAssertEqual(completionCount, 1)
  107. productInfoController.retrieveProductsInfo(sampleProductIdentifiers) { _ in
  108. completionCount += 1
  109. }
  110. requestBuilder.fireCallbacks()
  111. XCTAssertEqual(completionCount, 2)
  112. }
  113. func testRetrieveProductsInfo_when_calledConcurrentlyInDifferentThreads_then_eachcompletionCalledOnce_noCrashes() {
  114. let requestBuilder = TestInAppProductRequestBuilder()
  115. let productInfoController = ProductsInfoController(inAppProductRequestBuilder: requestBuilder)
  116. var completionCallbackCount = 0
  117. // Create the expectation not to let the test finishes before the other threads complete
  118. let expectation = XCTestExpectation(description: "Expect downloads of product informations")
  119. // Create the dispatch group to let the test verifies the assert only when
  120. // everything else finishes.
  121. let group = DispatchGroup()
  122. // Dispatch a request for every product in a different thread
  123. for product in testProducts {
  124. DispatchQueue.global().async {
  125. group.enter()
  126. productInfoController.retrieveProductsInfo([product]) { _ in
  127. completionCallbackCount += 1
  128. group.leave()
  129. }
  130. }
  131. }
  132. DispatchQueue.global().asyncAfter(deadline: .now()+0.1) {
  133. requestBuilder.fireCallbacks()
  134. }
  135. // Fullfil the expectation when every thread finishes
  136. group.notify(queue: DispatchQueue.global()) {
  137. XCTAssertEqual(completionCallbackCount, self.testProducts.count)
  138. expectation.fulfill()
  139. }
  140. wait(for: [expectation], timeout: 10.0)
  141. }
  142. }