|
| 1 | +// |
| 2 | +// Entwine |
| 3 | +// https://github.com/tcldr/Entwine |
| 4 | +// |
| 5 | +// Copyright © 2019 Tristan Celder. All rights reserved. |
| 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 | + |
| 25 | +import XCTest |
| 26 | +import Combine |
| 27 | + |
| 28 | +@testable import Entwine |
| 29 | +@testable import EntwineTest |
| 30 | + |
| 31 | +final class ReferenceCountedTests: XCTestCase { |
| 32 | + |
| 33 | + // MARK: - Properties |
| 34 | + |
| 35 | + private var scheduler: TestScheduler! |
| 36 | + |
| 37 | + // MARK: - Per test set-up and tear-down |
| 38 | + |
| 39 | + override func setUp() { |
| 40 | + scheduler = TestScheduler(initialClock: 0) |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Tests |
| 44 | + |
| 45 | + func testAutoConnectsAndPassesThroughInitialValue() { |
| 46 | + |
| 47 | + let passthrough = PassthroughSubject<Int, Never>() |
| 48 | + let subject = passthrough.prepend(-1).share() |
| 49 | + |
| 50 | + let results1 = scheduler.createTestableSubscriber(Int.self, Never.self) |
| 51 | + |
| 52 | + scheduler.schedule(after: 100) { subject.subscribe(results1) } |
| 53 | + scheduler.schedule(after: 200) { passthrough.send(0) } |
| 54 | + scheduler.schedule(after: 210) { passthrough.send(1) } |
| 55 | + |
| 56 | + scheduler.resume() |
| 57 | + |
| 58 | + let expected: TestSequence<Int, Never> = [ |
| 59 | + (100, .subscription), |
| 60 | + (100, .input(-1)), |
| 61 | + (200, .input( 0)), |
| 62 | + (210, .input( 1)), |
| 63 | + ] |
| 64 | + |
| 65 | + XCTAssertEqual(expected, results1.recordedOutput) |
| 66 | + } |
| 67 | + |
| 68 | + func testPassesThroughInitialValueToFirstSubscriberOnly() { |
| 69 | + |
| 70 | + let passthrough = PassthroughSubject<Int, Never>() |
| 71 | + let subject = passthrough.prepend(-1).share() |
| 72 | + |
| 73 | + let results1 = scheduler.createTestableSubscriber(Int.self, Never.self) |
| 74 | + let results2 = scheduler.createTestableSubscriber(Int.self, Never.self) |
| 75 | + |
| 76 | + scheduler.schedule(after: 100) { subject.subscribe(results1) } |
| 77 | + scheduler.schedule(after: 110) { subject.subscribe(results2) } |
| 78 | + scheduler.schedule(after: 200) { passthrough.send(0) } |
| 79 | + scheduler.schedule(after: 210) { passthrough.send(1) } |
| 80 | + |
| 81 | + scheduler.resume() |
| 82 | + |
| 83 | + let expected2: TestSequence<Int, Never> = [ |
| 84 | + (110, .subscription), |
| 85 | + (200, .input( 0)), |
| 86 | + (210, .input( 1)), |
| 87 | + ] |
| 88 | + |
| 89 | + XCTAssertEqual(expected2, results2.recordedOutput) |
| 90 | + } |
| 91 | + |
| 92 | + func testResetsWhenReferenceCountReachesZero() { |
| 93 | + |
| 94 | + let passthrough = PassthroughSubject<Int, Never>() |
| 95 | + let subject = passthrough.prepend(-1).share() |
| 96 | + |
| 97 | + let results1 = scheduler.createTestableSubscriber(Int.self, Never.self) |
| 98 | + let results2 = scheduler.createTestableSubscriber(Int.self, Never.self) |
| 99 | + let results3 = scheduler.createTestableSubscriber(Int.self, Never.self) |
| 100 | + |
| 101 | + scheduler.schedule(after: 100) { subject.subscribe(results1) } |
| 102 | + scheduler.schedule(after: 110) { subject.subscribe(results2) } |
| 103 | + scheduler.schedule(after: 200) { passthrough.send(0) } |
| 104 | + scheduler.schedule(after: 210) { passthrough.send(1) } |
| 105 | + scheduler.schedule(after: 300) { results1.cancel() } |
| 106 | + scheduler.schedule(after: 310) { results2.cancel() } |
| 107 | + scheduler.schedule(after: 400) { subject.subscribe(results3) } |
| 108 | + scheduler.schedule(after: 500) { passthrough.send(0) } |
| 109 | + scheduler.schedule(after: 510) { passthrough.send(1) } |
| 110 | + |
| 111 | + scheduler.resume() |
| 112 | + |
| 113 | + let expected3: TestSequence<Int, Never> = [ |
| 114 | + (400, .subscription), |
| 115 | + (400, .input(-1)), |
| 116 | + (500, .input( 0)), |
| 117 | + (510, .input( 1)), |
| 118 | + ] |
| 119 | + |
| 120 | + XCTAssertEqual(expected3, results3.recordedOutput) |
| 121 | + } |
| 122 | + |
| 123 | + func testMulticastCreateSubjectCalledWhenSubscriberCountGoesFromZeroToOne() { |
| 124 | + |
| 125 | + var cancellables = Set<AnyCancellable>() |
| 126 | + let factory: () -> PassthroughSubject<Int, Never> = { Swift.print("createSubject()"); return PassthroughSubject() } |
| 127 | + let sut = Just(1) |
| 128 | + sut.multicast(factory).autoconnect().sink { print("A:\($0)") }.store(in: &cancellables) |
| 129 | + cancellables = Set<AnyCancellable>() |
| 130 | + sut.multicast(factory).autoconnect().sink { print("B:\($0)") }.store(in: &cancellables) |
| 131 | + } |
| 132 | +} |
0 commit comments