From ecf382c79ebb97b2cf70ade167c25346594d0c7b Mon Sep 17 00:00:00 2001 From: Montak Oleg Date: Mon, 22 Jan 2018 12:38:27 +0300 Subject: [PATCH] Make barrierQueue serial to resolve https://github.com/vadymmarkov/When/issues/26 --- Sources/When/Functions.swift | 2 +- WhenTests/Shared/FunctionsSpec.swift | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Sources/When/Functions.swift b/Sources/When/Functions.swift index c7c6e6f..ac9f5c0 100644 --- a/Sources/When/Functions.swift +++ b/Sources/When/Functions.swift @@ -2,7 +2,7 @@ import Foundation let backgroundQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.background) let instantQueue = DispatchQueue(label: "When.InstantQueue", attributes: []) -let barrierQueue = DispatchQueue(label: "When.BarrierQueue", attributes: DispatchQueue.Attributes.concurrent) +let barrierQueue = DispatchQueue(label: "When.BarrierQueue", attributes: []) public func when(_ p1: Promise, _ p2: Promise) -> Promise<(T, U)> { return when([p1.asVoid(on: instantQueue), p2.asVoid(on: instantQueue)]).then(on: instantQueue) { _ in diff --git a/WhenTests/Shared/FunctionsSpec.swift b/WhenTests/Shared/FunctionsSpec.swift index c2b3732..9dd1775 100644 --- a/WhenTests/Shared/FunctionsSpec.swift +++ b/WhenTests/Shared/FunctionsSpec.swift @@ -54,6 +54,29 @@ class FunctionsSpec: QuickSpec { self.waitForExpectations(timeout: 2.0, handler:nil) } } + + context("with many concurrent tasks") { + it("resolves the promise") { + let promises: [Promise] = (0..<10000).map { _ in + let promise = Promise(queue: DispatchQueue.global(), { resolve in + // +1 needed to avoid https://github.com/vadymmarkov/When/issues/27 + DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 1) { + resolve(1) + } + }) + return promise + } + + let doneExpectation = self.expectation(description: "Done expectation") + + when(promises) + .done({ result in + doneExpectation.fulfill() + }) + + self.waitForExpectations(timeout: 2.0, handler:nil) + } + } } } }