Created
October 6, 2018 02:40
-
-
Save toshi0383/3eb30a9f9185bced9d706534e644edfc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
@testable import RxSmartThrottle | |
final class RxSmartThrottleTests: XCTestCase { | |
static var allTests = [ | |
("test_Throttle__customInterval__ExponentialBackoff", test_Throttle__customInterval__ExponentialBackoff), | |
("test_Throttle__customInterval__latest__false", test_Throttle__customInterval__latest__false), | |
("test_Throttle__customInterval__throttleUntil", test_Throttle__customInterval__throttleUntil), | |
] | |
} | |
extension RxSmartThrottleTests { | |
func test_Throttle__customInterval__ExponentialBackoff() { | |
let scheduler = TestScheduler(initialClock: 0) | |
let xs = scheduler.createHotObservable([ | |
.next(120, 0), | |
.next(150, 1), | |
.next(210, 2), | |
.next(250, 3), | |
.next(310, 4), | |
.next(350, 5), | |
.next(570, 6), | |
.next(960, 7), | |
.completed(1000) | |
]) | |
let takeUntilTrigger = Observable<Void>.empty() | |
let res = scheduler.start { | |
xs.throttle(dueTime: { | |
max($1 * 2, 100) | |
}, | |
until: takeUntilTrigger, | |
latest: true, | |
scheduler: scheduler) | |
} | |
let correct = Recorded.events( | |
.next(210, 2), | |
.next(310, 4), | |
.next(510, 5), | |
.next(710, 6), | |
.next(960, 7), | |
.completed(1000) | |
) | |
XCTAssertEqual(res.events, correct) | |
let subscriptions = [ | |
Subscription(200, 1000) | |
] | |
XCTAssertEqual(xs.subscriptions, subscriptions) | |
} | |
func test_Throttle__customInterval__latest__false() { | |
let scheduler = TestScheduler(initialClock: 0) | |
let xs = scheduler.createHotObservable([ | |
.next(120, 0), | |
.next(150, 1), | |
.next(210, 2), | |
.next(250, 3), | |
.next(310, 4), | |
.next(350, 5), | |
.next(570, 6), | |
.next(960, 7), | |
.completed(1000) | |
]) | |
let takeUntilTrigger = Observable<Void>.empty() | |
let res = scheduler.start { | |
xs.throttle(dueTime: { | |
max($1 * 2, 100) | |
}, | |
until: takeUntilTrigger, | |
latest: false, | |
scheduler: scheduler) | |
} | |
let correct = Recorded.events( | |
.next(210, 2), | |
.next(310, 4), | |
.next(570, 6), | |
.completed(1000) | |
) | |
XCTAssertEqual(res.events, correct) | |
let subscriptions = [ | |
Subscription(200, 1000) | |
] | |
XCTAssertEqual(xs.subscriptions, subscriptions) | |
} | |
func test_Throttle__customInterval__throttleUntil() { | |
let scheduler = TestScheduler(initialClock: 0) | |
let xs = scheduler.createHotObservable([ | |
.next(120, 0), | |
.next(150, 1), | |
.next(210, 2), | |
.next(250, 3), | |
.next(310, 4), | |
.next(350, 5), | |
.next(570, 6), | |
.next(580, 7), | |
.next(960, 8), | |
.completed(1000) | |
]) | |
let takeUntilTrigger = scheduler.createHotObservable([ | |
.next(500, 2), | |
.completed(1000) | |
]) | |
let res = scheduler.start { | |
xs.throttle(dueTime: { | |
max($1 * 2, 100) | |
}, | |
until: takeUntilTrigger, | |
latest: true, | |
scheduler: scheduler) | |
} | |
let correct = Recorded.events( | |
.next(210, 2), | |
.next(310, 4), | |
.next(570, 6), | |
.next(670, 7), | |
.next(960, 8), | |
.completed(1000) | |
) | |
XCTAssertEqual(res.events, correct) | |
let subscriptions = [ | |
Subscription(200, 1000) | |
] | |
XCTAssertEqual(xs.subscriptions, subscriptions) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment