|
| 1 | +import Combine |
| 2 | +import ComposableArchitecture |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@MainActor |
| 6 | +final class EffectThrottleTests: BaseTCATestCase { |
| 7 | + let mainQueue = DispatchQueue.test |
| 8 | + |
| 9 | + func testThrottleLatest_Publisher() async { |
| 10 | + let store = TestStore(initialState: ThrottleFeature.State()) { |
| 11 | + ThrottleFeature(id: #function, latest: true) |
| 12 | + } withDependencies: { |
| 13 | + $0.mainQueue = mainQueue.eraseToAnyScheduler() |
| 14 | + } |
| 15 | + |
| 16 | + await store.send(.tap(1)) |
| 17 | + await self.mainQueue.advance() |
| 18 | + await store.receive(.throttledResponse(1)) { |
| 19 | + $0.count = 1 |
| 20 | + } |
| 21 | + |
| 22 | + await store.send(.tap(2)) |
| 23 | + await self.mainQueue.advance() |
| 24 | + await store.skipReceivedActions(strict: false) |
| 25 | + XCTAssertEqual(store.state.count, 1) |
| 26 | + |
| 27 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 28 | + await store.skipReceivedActions(strict: false) |
| 29 | + XCTAssertEqual(store.state.count, 1) |
| 30 | + |
| 31 | + await store.send(.tap(3)) |
| 32 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 33 | + await store.skipReceivedActions(strict: false) |
| 34 | + XCTAssertEqual(store.state.count, 1) |
| 35 | + |
| 36 | + await store.send(.tap(4)) |
| 37 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 38 | + await store.skipReceivedActions(strict: false) |
| 39 | + XCTAssertEqual(store.state.count, 1) |
| 40 | + |
| 41 | + await store.send(.tap(5)) |
| 42 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 43 | + await store.receive(.throttledResponse(5)) { |
| 44 | + $0.count = 5 |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + func testThrottleLatest_Async() async { |
| 49 | + let store = TestStore(initialState: ThrottleFeature.State()) { |
| 50 | + ThrottleFeature(id: #function, latest: true) |
| 51 | + } withDependencies: { |
| 52 | + $0.mainQueue = mainQueue.eraseToAnyScheduler() |
| 53 | + } |
| 54 | + |
| 55 | + await store.send(.tap(1)) |
| 56 | + await self.mainQueue.advance() |
| 57 | + await store.receive(.throttledResponse(1)) { |
| 58 | + $0.count = 1 |
| 59 | + } |
| 60 | + |
| 61 | + await store.send(.tap(2)) |
| 62 | + await self.mainQueue.advance() |
| 63 | + await store.skipReceivedActions(strict: false) |
| 64 | + XCTAssertEqual(store.state.count, 1) |
| 65 | + |
| 66 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 67 | + await store.skipReceivedActions(strict: false) |
| 68 | + XCTAssertEqual(store.state.count, 1) |
| 69 | + |
| 70 | + await store.send(.tap(3)) |
| 71 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 72 | + await store.skipReceivedActions(strict: false) |
| 73 | + XCTAssertEqual(store.state.count, 1) |
| 74 | + |
| 75 | + await store.send(.tap(4)) |
| 76 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 77 | + await store.skipReceivedActions(strict: false) |
| 78 | + XCTAssertEqual(store.state.count, 1) |
| 79 | + |
| 80 | + await store.send(.tap(5)) |
| 81 | + await self.mainQueue.advance(by: .seconds(1)) |
| 82 | + await store.receive(.throttledResponse(5)) { |
| 83 | + $0.count = 5 |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + func testThrottleFirst_Publisher() async { |
| 88 | + let store = TestStore(initialState: ThrottleFeature.State()) { |
| 89 | + ThrottleFeature(id: #function, latest: false) |
| 90 | + } withDependencies: { |
| 91 | + $0.mainQueue = mainQueue.eraseToAnyScheduler() |
| 92 | + } |
| 93 | + |
| 94 | + await store.send(.tap(1)) |
| 95 | + await self.mainQueue.advance() |
| 96 | + await store.receive(.throttledResponse(1)) { |
| 97 | + $0.count = 1 |
| 98 | + } |
| 99 | + |
| 100 | + await store.send(.tap(2)) |
| 101 | + await self.mainQueue.advance() |
| 102 | + await store.skipReceivedActions(strict: false) |
| 103 | + XCTAssertEqual(store.state.count, 1) |
| 104 | + |
| 105 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 106 | + await store.skipReceivedActions(strict: false) |
| 107 | + XCTAssertEqual(store.state.count, 1) |
| 108 | + |
| 109 | + await store.send(.tap(3)) |
| 110 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 111 | + await store.skipReceivedActions(strict: false) |
| 112 | + XCTAssertEqual(store.state.count, 1) |
| 113 | + |
| 114 | + await store.send(.tap(4)) |
| 115 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 116 | + await store.skipReceivedActions(strict: false) |
| 117 | + XCTAssertEqual(store.state.count, 1) |
| 118 | + |
| 119 | + await store.send(.tap(5)) |
| 120 | + await self.mainQueue.advance(by: .seconds(0.25)) |
| 121 | + await store.receive(.throttledResponse(2)) { |
| 122 | + $0.count = 2 |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + func testThrottleAfterInterval_Publisher() async { |
| 127 | + let store = TestStore(initialState: ThrottleFeature.State()) { |
| 128 | + ThrottleFeature(id: #function, latest: true) |
| 129 | + } withDependencies: { |
| 130 | + $0.mainQueue = mainQueue.eraseToAnyScheduler() |
| 131 | + } |
| 132 | + |
| 133 | + await store.send(.tap(1)) |
| 134 | + await self.mainQueue.advance() |
| 135 | + await store.receive(.throttledResponse(1)) { |
| 136 | + $0.count = 1 |
| 137 | + } |
| 138 | + |
| 139 | + await self.mainQueue.advance(by: .seconds(1)) |
| 140 | + await store.send(.tap(2)) |
| 141 | + await self.mainQueue.advance() |
| 142 | + await store.receive(.throttledResponse(2)) { |
| 143 | + $0.count = 2 |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + func testThrottleEmitsFirstValueOnce_Publisher() async { |
| 148 | + let store = TestStore(initialState: ThrottleFeature.State()) { |
| 149 | + ThrottleFeature(id: #function, latest: true) |
| 150 | + } withDependencies: { |
| 151 | + $0.mainQueue = mainQueue.eraseToAnyScheduler() |
| 152 | + } |
| 153 | + |
| 154 | + await store.send(.tap(1)) |
| 155 | + await self.mainQueue.advance() |
| 156 | + await store.receive(.throttledResponse(1)) { |
| 157 | + $0.count = 1 |
| 158 | + } |
| 159 | + |
| 160 | + await self.mainQueue.advance(by: .seconds(1)) |
| 161 | + await store.send(.tap(2)) |
| 162 | + await self.mainQueue.advance() |
| 163 | + await store.receive(.throttledResponse(2)) { |
| 164 | + $0.count = 2 |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +struct ThrottleFeature: Reducer { |
| 170 | + struct State: Equatable { |
| 171 | + var count = 0 |
| 172 | + } |
| 173 | + enum Action: Equatable { |
| 174 | + case tap(Int) |
| 175 | + case throttledResponse(Int) |
| 176 | + } |
| 177 | + let id: String |
| 178 | + let latest: Bool |
| 179 | + @Dependency(\.mainQueue) var mainQueue |
| 180 | + func reduce(into state: inout State, action: Action) -> Effect<Action> { |
| 181 | + switch action { |
| 182 | + case let .tap(value): |
| 183 | + return .send(.throttledResponse(value)) |
| 184 | + .throttle(id: self.id, for: .seconds(1), scheduler: self.mainQueue, latest: self.latest) |
| 185 | + case let .throttledResponse(value): |
| 186 | + state.count = value |
| 187 | + return .none |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments