|
| 1 | +import SubscribingAsyncIterator from "../lib/SubscribingAsyncIterator"; |
| 2 | +import { mock } from "jest-mock-extended"; |
| 3 | +import { Cancellable, Requestable } from "@rsocket/core"; |
| 4 | +import { Codec } from "@rsocket/messaging"; |
| 5 | + |
| 6 | +jest.useFakeTimers(); |
| 7 | + |
| 8 | +class StringCodec implements Codec<string> { |
| 9 | + readonly mimeType: string = "text/plain"; |
| 10 | + |
| 11 | + decode(buffer: Buffer): string { |
| 12 | + return buffer.toString(); |
| 13 | + } |
| 14 | + |
| 15 | + encode(entity: string): Buffer { |
| 16 | + return Buffer.from(entity); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe("SubscribingAsyncIterator", function () { |
| 21 | + it("iterates over emitted values", async function () { |
| 22 | + let subscriber; |
| 23 | + const subscription = mock<Requestable & Cancellable>({ |
| 24 | + request(requestN: number) { |
| 25 | + for (let i = 0; i < requestN; i++) { |
| 26 | + setTimeout(() => { |
| 27 | + subscriber.onNext( |
| 28 | + { |
| 29 | + data: Buffer.from(`${i}`), |
| 30 | + metadata: undefined, |
| 31 | + }, |
| 32 | + i === requestN - 1 |
| 33 | + ); |
| 34 | + }); |
| 35 | + } |
| 36 | + }, |
| 37 | + }); |
| 38 | + const requestSpy = jest.spyOn(subscription, "request"); |
| 39 | + |
| 40 | + const initialRequestN = 3; |
| 41 | + subscriber = new SubscribingAsyncIterator( |
| 42 | + subscription, |
| 43 | + initialRequestN * 2, |
| 44 | + new StringCodec() |
| 45 | + ); |
| 46 | + subscription.request(initialRequestN); |
| 47 | + |
| 48 | + jest.runAllTimers(); |
| 49 | + |
| 50 | + const values = []; |
| 51 | + for await (const value of subscriber) { |
| 52 | + jest.runAllTimers(); |
| 53 | + values.push(value); |
| 54 | + } |
| 55 | + |
| 56 | + expect(values).toStrictEqual(["0", "1", "2"]); |
| 57 | + expect(requestSpy).toBeCalledTimes(1); |
| 58 | + }); |
| 59 | + |
| 60 | + it("iterates over emitted values until onComplete", async function () { |
| 61 | + let subscriber; |
| 62 | + const subscription = mock<Requestable & Cancellable>({ |
| 63 | + request(requestN: number) { |
| 64 | + for (let i = 0; i < requestN; i++) { |
| 65 | + setTimeout(() => { |
| 66 | + if (i === requestN - 1) { |
| 67 | + subscriber.onComplete(); |
| 68 | + } else { |
| 69 | + subscriber.onNext( |
| 70 | + { |
| 71 | + data: Buffer.from(`${i}`), |
| 72 | + metadata: undefined, |
| 73 | + }, |
| 74 | + false |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + }, |
| 80 | + }); |
| 81 | + const requestSpy = jest.spyOn(subscription, "request"); |
| 82 | + |
| 83 | + const initialRequestN = 3; |
| 84 | + subscriber = new SubscribingAsyncIterator( |
| 85 | + subscription, |
| 86 | + initialRequestN * 2, |
| 87 | + new StringCodec() |
| 88 | + ); |
| 89 | + subscription.request(initialRequestN); |
| 90 | + |
| 91 | + jest.runAllTimers(); |
| 92 | + |
| 93 | + const values = []; |
| 94 | + for await (const value of subscriber) { |
| 95 | + jest.runAllTimers(); |
| 96 | + values.push(value); |
| 97 | + } |
| 98 | + |
| 99 | + expect(values).toStrictEqual(["0", "1"]); |
| 100 | + expect(requestSpy).toBeCalledTimes(1); |
| 101 | + }); |
| 102 | + |
| 103 | + it("cancels when break statement reached", async function () { |
| 104 | + let subscriber; |
| 105 | + const subscription = mock<Requestable & Cancellable>({ |
| 106 | + request(requestN: number) { |
| 107 | + for (let i = 0; i < requestN; i++) { |
| 108 | + setTimeout(() => { |
| 109 | + subscriber.onNext( |
| 110 | + { |
| 111 | + data: Buffer.from(`${i}`), |
| 112 | + metadata: undefined, |
| 113 | + }, |
| 114 | + i === requestN - 1 |
| 115 | + ); |
| 116 | + }); |
| 117 | + } |
| 118 | + }, |
| 119 | + }); |
| 120 | + const requestSpy = jest.spyOn(subscription, "request"); |
| 121 | + const cancelSpy = jest.spyOn(subscription, "cancel"); |
| 122 | + |
| 123 | + const initialRequestN = 10; |
| 124 | + subscriber = new SubscribingAsyncIterator( |
| 125 | + subscription, |
| 126 | + initialRequestN * 2, |
| 127 | + new StringCodec() |
| 128 | + ); |
| 129 | + subscription.request(initialRequestN); |
| 130 | + |
| 131 | + jest.runAllTimers(); |
| 132 | + |
| 133 | + const values = []; |
| 134 | + for await (const value of subscriber) { |
| 135 | + if (values.length == 2) { |
| 136 | + break; |
| 137 | + } |
| 138 | + jest.runAllTimers(); |
| 139 | + values.push(value); |
| 140 | + } |
| 141 | + |
| 142 | + expect(values).toStrictEqual(["0", "1"]); |
| 143 | + expect(requestSpy).toBeCalledTimes(1); |
| 144 | + expect(requestSpy).toBeCalledWith(10); |
| 145 | + expect(cancelSpy).toBeCalledTimes(1); |
| 146 | + }); |
| 147 | + |
| 148 | + it("ends and throws with emitted exception", async function () { |
| 149 | + let subscriber; |
| 150 | + const expectedError = new Error("test error"); |
| 151 | + const subscription = mock<Requestable & Cancellable>({ |
| 152 | + request(requestN: number) { |
| 153 | + setTimeout(() => { |
| 154 | + subscriber.onError(expectedError); |
| 155 | + }); |
| 156 | + }, |
| 157 | + }); |
| 158 | + const requestSpy = jest.spyOn(subscription, "request"); |
| 159 | + |
| 160 | + const initialRequestN = 10; |
| 161 | + subscriber = new SubscribingAsyncIterator( |
| 162 | + subscription, |
| 163 | + initialRequestN * 2, |
| 164 | + new StringCodec() |
| 165 | + ); |
| 166 | + subscription.request(initialRequestN); |
| 167 | + |
| 168 | + jest.runAllTimers(); |
| 169 | + |
| 170 | + const values = []; |
| 171 | + |
| 172 | + let capturedError; |
| 173 | + try { |
| 174 | + for await (const value of subscriber) { |
| 175 | + jest.runAllTimers(); |
| 176 | + values.push(value); |
| 177 | + } |
| 178 | + } catch (error) { |
| 179 | + capturedError = error; |
| 180 | + } |
| 181 | + |
| 182 | + expect(capturedError).toBe(expectedError); |
| 183 | + expect(values).toStrictEqual([]); |
| 184 | + expect(requestSpy).toBeCalledWith(10); |
| 185 | + }); |
| 186 | + |
| 187 | + it("cancels on exception processing emitted value", async function () { |
| 188 | + let subscriber; |
| 189 | + const subscription = mock<Requestable & Cancellable>({ |
| 190 | + request(requestN: number) { |
| 191 | + for (let i = 0; i < requestN; i++) { |
| 192 | + setTimeout(() => { |
| 193 | + subscriber.onNext( |
| 194 | + { |
| 195 | + data: Buffer.from(`${i}`), |
| 196 | + metadata: undefined, |
| 197 | + }, |
| 198 | + i === requestN - 1 |
| 199 | + ); |
| 200 | + }); |
| 201 | + } |
| 202 | + }, |
| 203 | + }); |
| 204 | + const requestSpy = jest.spyOn(subscription, "request"); |
| 205 | + const cancelSpy = jest.spyOn(subscription, "cancel"); |
| 206 | + |
| 207 | + const initialRequestN = 10; |
| 208 | + subscriber = new SubscribingAsyncIterator( |
| 209 | + subscription, |
| 210 | + initialRequestN * 2, |
| 211 | + new StringCodec() |
| 212 | + ); |
| 213 | + subscription.request(initialRequestN); |
| 214 | + |
| 215 | + jest.runAllTimers(); |
| 216 | + |
| 217 | + const values = []; |
| 218 | + try { |
| 219 | + for await (const value of subscriber) { |
| 220 | + if (values.length == 2) { |
| 221 | + throw new Error("test error"); |
| 222 | + } |
| 223 | + values.push(value); |
| 224 | + jest.runAllTimers(); |
| 225 | + } |
| 226 | + } catch (e) {} |
| 227 | + |
| 228 | + expect(values).toStrictEqual(["0", "1"]); |
| 229 | + expect(requestSpy).toBeCalledTimes(1); |
| 230 | + expect(requestSpy).toBeCalledWith(10); |
| 231 | + expect(cancelSpy).toBeCalledTimes(1); |
| 232 | + }); |
| 233 | +}); |
0 commit comments