Skip to content

Commit

Permalink
unittest: improve scheduling testing
Browse files Browse the repository at this point in the history
  • Loading branch information
twittemb committed Aug 9, 2020
1 parent 13689e5 commit d7df7de
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 29 deletions.
4 changes: 3 additions & 1 deletion Sources/ReactiveSwift/Executer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import SpinCommon
public class Executer: ExecuterDefinition {
public typealias Executer = Scheduler
public static func defaultSpinExecuter() -> Executer {
QueueScheduler(qos: .default, name: "io.warpfactor.spin.dispatch-queue.\(UUID())")
let queueName = "io.warpfactor.spin.dispatch-queue.\(UUID())"
let dispatchQueue = DispatchQueue(label: queueName)
return QueueScheduler(qos: .default, name: queueName, targeting: dispatchQueue)
}
}
4 changes: 3 additions & 1 deletion Sources/RxSwift/Executer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import SpinCommon
public class Executer: ExecuterDefinition {
public typealias Executer = ImmediateSchedulerType
public static func defaultSpinExecuter() -> Executer {
SerialDispatchQueueScheduler(internalSerialQueueName: "io.warpfactor.spin.dispatch-queue.\(UUID())")
let queueName = "io.warpfactor.spin.dispatch-queue.\(UUID())"
let dispatchQueue = DispatchQueue(label: queueName)
return SerialDispatchQueueScheduler(queue: dispatchQueue, internalSerialQueueName: queueName)
}
}
26 changes: 19 additions & 7 deletions Tests/CombineTests/SpinIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,28 +159,39 @@ final class SpinIntegrationTests: XCTestCase {
let exp = expectation(description: "Scheduling")

let expectedReducerQueueLabel = "SPIN_QUEUE_\(UUID())"
let expectedFeedbackQueueLabel = "FEEDBACK_QUEUE_\(UUID())"
let expectedFeedback1QueueLabel = "FEEDBACK1_QUEUE_\(UUID())"
let expectedFeedback2QueueLabel = "FEEDBACK2_QUEUE_\(UUID())"

var receivedReducerQueueLabel = ""
var receivedFeedbackQueueLabel = ""
var receivedFeedback1QueueLabel = ""
var receivedFeedback2QueueLabel = ""

let spinQueue = DispatchQueue(label: expectedReducerQueueLabel)
let feedbackQueue = DispatchQueue(label: expectedFeedbackQueueLabel)
let feedback1Queue = DispatchQueue(label: expectedFeedback1QueueLabel)
let feedback2Queue = DispatchQueue(label: expectedFeedback2QueueLabel)

let spyReducer: (String, String) -> String = { _, _ in
receivedReducerQueueLabel = DispatchQueue.currentLabel
return ""
}

let spyFeedback: (AnyPublisher<String, Never>) -> AnyPublisher<String, Never> = { states in
let spyFeedback1: (AnyPublisher<String, Never>) -> AnyPublisher<String, Never> = { states in
states.map {
receivedFeedbackQueueLabel = DispatchQueue.currentLabel
receivedFeedback1QueueLabel = DispatchQueue.currentLabel
return $0
}.eraseToAnyPublisher()
}

let spyFeedback2: (AnyPublisher<String, Never>) -> AnyPublisher<String, Never> = { states in
states.map {
receivedFeedback2QueueLabel = DispatchQueue.currentLabel
return $0
}.eraseToAnyPublisher()
}

let sut = Spin<String, String>(initialState: "initialState", executeOn: spinQueue) {
Feedback<String, String>(effect: spyFeedback).execute(on: feedbackQueue.eraseToAnyScheduler())
Feedback<String, String>(effect: spyFeedback1).execute(on: feedback1Queue.eraseToAnyScheduler())
Feedback<String, String>(effect: spyFeedback2).execute(on: feedback2Queue.eraseToAnyScheduler())
Reducer<String, String>(spyReducer)
}

Expand All @@ -192,7 +203,8 @@ final class SpinIntegrationTests: XCTestCase {

waitForExpectations(timeout: 0.5)

XCTAssertEqual(receivedFeedbackQueueLabel, expectedFeedbackQueueLabel)
XCTAssertEqual(receivedFeedback1QueueLabel, expectedFeedback1QueueLabel)
XCTAssertEqual(receivedFeedback2QueueLabel, expectedFeedback2QueueLabel)
XCTAssertEqual(receivedReducerQueueLabel, expectedReducerQueueLabel)
}
}
Expand Down
36 changes: 26 additions & 10 deletions Tests/ReactiveSwiftTests/SpinIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,40 +151,56 @@ final class SpinIntegrationTests: XCTestCase {
let exp = expectation(description: "Scheduling")

let expectedReducerQueueLabel = "SPIN_QUEUE_\(UUID())"
let expectedFeedbackQueueLabel = "FEEDBACK_QUEUE_\(UUID())"
let expectedFeedback1QueueLabel = "FEEDBACK1_QUEUE_\(UUID())"
let expectedFeedback2QueueLabel = "FEEDBACK2_QUEUE_\(UUID())"

var receivedReducerQueueLabel = ""
var receivedFeedbackQueueLabel = ""
var receivedFeedback1QueueLabel = ""
var receivedFeedback2QueueLabel = ""

let spinQueue = QueueScheduler(qos: .default, name: expectedReducerQueueLabel)
let feedbackQueue = QueueScheduler(qos: .default, name: expectedFeedbackQueueLabel)
let spinQueue = DispatchQueue(label: expectedReducerQueueLabel)
let feedback1Queue = DispatchQueue(label: expectedFeedback1QueueLabel)
let feedback2Queue = DispatchQueue(label: expectedFeedback2QueueLabel)

let spinScheduler = QueueScheduler(qos: .default, name: expectedReducerQueueLabel, targeting: spinQueue)
let feedback1Scheduler = QueueScheduler(qos: .default, name: expectedFeedback1QueueLabel, targeting: feedback1Queue)
let feedback2Scheduler = QueueScheduler(qos: .default, name: expectedFeedback2QueueLabel, targeting: feedback2Queue)

let spyReducer: (String, String) -> String = { _, _ in
receivedReducerQueueLabel = DispatchQueue.currentLabel
return ""
}

let spyFeedback: (SignalProducer<String, Never>) -> SignalProducer<String, Never> = { states in
let spyFeedback1: (SignalProducer<String, Never>) -> SignalProducer<String, Never> = { states in
states.map {
receivedFeedback1QueueLabel = DispatchQueue.currentLabel
return $0
}
}

let spyFeedback2: (SignalProducer<String, Never>) -> SignalProducer<String, Never> = { states in
states.map {
receivedFeedbackQueueLabel = DispatchQueue.currentLabel
receivedFeedback2QueueLabel = DispatchQueue.currentLabel
return $0
}
}

let sut = Spin<String, String>(initialState: "initialState", executeOn: spinQueue) {
Feedback<String, String>(effect: spyFeedback).execute(on: feedbackQueue)
let sut = Spin<String, String>(initialState: "initialState", executeOn: spinScheduler) {
Feedback<String, String>(effect: spyFeedback1).execute(on: feedback1Scheduler)
Feedback<String, String>(effect: spyFeedback2).execute(on: feedback2Scheduler)
Reducer<String, String>(spyReducer)
}

SignalProducer
.stream(from: sut)
.take(first: 2)
.take(first: 3)
.startWithCompleted { exp.fulfill() }
.add(to: self.disposables)

waitForExpectations(timeout: 0.5)

XCTAssertEqual(receivedFeedbackQueueLabel, expectedFeedbackQueueLabel)
XCTAssertEqual(receivedFeedback1QueueLabel, expectedFeedback1QueueLabel)
XCTAssertEqual(receivedFeedback2QueueLabel, expectedFeedback2QueueLabel)
XCTAssertEqual(receivedReducerQueueLabel, expectedReducerQueueLabel)
}
}
Expand Down
32 changes: 22 additions & 10 deletions Tests/RxSwiftTests/SpinIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,39 @@ final class SpinIntegrationTests: XCTestCase {
let exp = expectation(description: "Scheduling")

let expectedReducerQueueLabel = "SPIN_QUEUE_\(UUID())"
let expectedFeedbackQueueLabel = "FEEDBACK_QUEUE_\(UUID())"
let expectedFeedback1QueueLabel = "FEEDBACK1_QUEUE_\(UUID())"
let expectedFeedback2QueueLabel = "FEEDBACK2_QUEUE_\(UUID())"

var receivedReducerQueueLabel = ""
var receivedFeedbackQueueLabel = ""
var receivedFeedback1QueueLabel = ""
var receivedFeedback2QueueLabel = ""

let spinQueue = SerialDispatchQueueScheduler(internalSerialQueueName: expectedReducerQueueLabel)
let feedbackQueue = SerialDispatchQueueScheduler(internalSerialQueueName: expectedFeedbackQueueLabel)
let spinScheduler = SerialDispatchQueueScheduler(queue: DispatchQueue(label: expectedReducerQueueLabel), internalSerialQueueName: expectedReducerQueueLabel)
let feedback1Scheduler = SerialDispatchQueueScheduler(queue: DispatchQueue(label: expectedFeedback1QueueLabel), internalSerialQueueName: expectedFeedback1QueueLabel)
let feedback2Scheduler = SerialDispatchQueueScheduler(queue: DispatchQueue(label: expectedFeedback2QueueLabel), internalSerialQueueName: expectedFeedback2QueueLabel)

let spyReducer: (String, String) -> String = { _, _ in
receivedReducerQueueLabel = DispatchQueue.currentLabel
return ""
}

let spyFeedback: (Observable<String>) -> Observable<String> = { states in
let spyFeedback1: (Observable<String>) -> Observable<String> = { states in
states.map {
receivedFeedbackQueueLabel = DispatchQueue.currentLabel
receivedFeedback1QueueLabel = DispatchQueue.currentLabel
return $0
}
}

let sut = Spin<String, String>(initialState: "initialState", executeOn: spinQueue) {
Feedback<String, String>(effect: spyFeedback).execute(on: feedbackQueue)
let spyFeedback2: (Observable<String>) -> Observable<String> = { states in
states.map {
receivedFeedback2QueueLabel = DispatchQueue.currentLabel
return $0
}
}

let sut = Spin<String, String>(initialState: "initialState", executeOn: spinScheduler) {
Feedback<String, String>(effect: spyFeedback1).execute(on: feedback1Scheduler)
Feedback<String, String>(effect: spyFeedback2).execute(on: feedback2Scheduler)
Reducer<String, String>(spyReducer)
}

Expand All @@ -184,7 +195,8 @@ final class SpinIntegrationTests: XCTestCase {

waitForExpectations(timeout: 0.5)

XCTAssertEqual(receivedFeedbackQueueLabel, expectedFeedbackQueueLabel)
XCTAssertEqual(receivedFeedback1QueueLabel, expectedFeedback1QueueLabel)
XCTAssertEqual(receivedFeedback2QueueLabel, expectedFeedback2QueueLabel)
XCTAssertEqual(receivedReducerQueueLabel, expectedReducerQueueLabel)
}
}
Expand Down Expand Up @@ -262,7 +274,7 @@ extension SpinIntegrationTests {
}
return .empty()
}
checkAuthorizationSpin.effects.append(Feedback<CheckAuthorizationSpinState, CheckAuthorizationSpinEvent>(effect: spyEffectCheckAuthorizationSpin).effect)
checkAuthorizationSpin.effects.append(Feedback<CheckAuthorizationSpinState, CheckAuthorizationSpinEvent>(effect: spyEffectCheckAuthorizationSpin).effect)

// When: executing the 2 spins
Observable
Expand Down

0 comments on commit d7df7de

Please sign in to comment.