|
| 1 | +import { use as chaiUse, expect, spy } from "chai" |
| 2 | +import { Client, Publisher } from "../../src" |
| 3 | +import { Message } from "../../src/publisher" |
| 4 | +import { Offset } from "../../src/requests/subscribe_request" |
| 5 | +import { createClient, createConsumerRef, createPublisher, createStreamName } from "../support/fake_data" |
| 6 | +import { Rabbit } from "../support/rabbit" |
| 7 | +import { always, eventually, password, username } from "../support/util" |
| 8 | +import { |
| 9 | + creditsOnChunkCompleted, |
| 10 | + creditsOnChunkProgress, |
| 11 | + creditsOnChunkReceived, |
| 12 | +} from "../../src/consumer_credit_policy" |
| 13 | +import spies from "chai-spies" |
| 14 | +chaiUse(spies) |
| 15 | + |
| 16 | +const send = async (publisher: Publisher, chunks: Message[][]) => { |
| 17 | + for (const chunk of chunks) { |
| 18 | + for (const msg of chunk) { |
| 19 | + await publisher.send(msg.content) |
| 20 | + } |
| 21 | + await publisher.flush() |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe("consumer credit flow policies", () => { |
| 26 | + let streamName: string |
| 27 | + const rabbit = new Rabbit(username, password) |
| 28 | + let client: Client |
| 29 | + let publisher: Publisher |
| 30 | + const previousMaxSharedClientInstances = process.env.MAX_SHARED_CLIENT_INSTANCES |
| 31 | + const sandbox = spy.sandbox() |
| 32 | + const chunk: Message[] = [ |
| 33 | + { content: Buffer.from("hello") }, |
| 34 | + { content: Buffer.from("there") }, |
| 35 | + { content: Buffer.from("brave") }, |
| 36 | + { content: Buffer.from("new") }, |
| 37 | + { content: Buffer.from("world") }, |
| 38 | + ] |
| 39 | + const chunks = [chunk, chunk] |
| 40 | + const nMessages = chunk.length * chunks.length |
| 41 | + |
| 42 | + before(() => { |
| 43 | + process.env.MAX_SHARED_CLIENT_INSTANCES = "10" |
| 44 | + }) |
| 45 | + |
| 46 | + after(() => { |
| 47 | + if (previousMaxSharedClientInstances !== undefined) { |
| 48 | + process.env.MAX_SHARED_CLIENT_INSTANCES = previousMaxSharedClientInstances |
| 49 | + return |
| 50 | + } |
| 51 | + delete process.env.MAX_SHARED_CLIENT_INSTANCES |
| 52 | + }) |
| 53 | + |
| 54 | + beforeEach(async () => { |
| 55 | + client = await createClient(username, password) |
| 56 | + streamName = createStreamName() |
| 57 | + await rabbit.createStream(streamName) |
| 58 | + publisher = await createPublisher(streamName, client) |
| 59 | + }) |
| 60 | + |
| 61 | + afterEach(async () => { |
| 62 | + try { |
| 63 | + sandbox.restore() |
| 64 | + await client.close() |
| 65 | + await rabbit.deleteStream(streamName) |
| 66 | + await rabbit.closeAllConnections() |
| 67 | + await rabbit.deleteAllQueues({ match: /my-stream-/ }) |
| 68 | + } catch (_e) {} |
| 69 | + }) |
| 70 | + |
| 71 | + it("NewCreditOnChunkReceived policy requests new credit when chunk is completely received", async () => { |
| 72 | + const consumerRef = createConsumerRef() |
| 73 | + const policy = creditsOnChunkReceived(1, 1) |
| 74 | + sandbox.on(policy, "onChunkReceived") |
| 75 | + const received: Message[] = [] |
| 76 | + await client.declareConsumer( |
| 77 | + { |
| 78 | + stream: streamName, |
| 79 | + offset: Offset.first(), |
| 80 | + singleActive: true, |
| 81 | + consumerRef: consumerRef, |
| 82 | + creditPolicy: policy, |
| 83 | + }, |
| 84 | + (message: Message) => { |
| 85 | + received.push(message) |
| 86 | + } |
| 87 | + ) |
| 88 | + |
| 89 | + await send(publisher, chunks) |
| 90 | + |
| 91 | + await eventually(() => expect(received.length).eql(nMessages)) |
| 92 | + await eventually(() => expect(policy.onChunkReceived).called.exactly(chunks.length)) |
| 93 | + await always(() => expect(policy.onChunkReceived).called.below(chunks.length + 1), 5000) |
| 94 | + }).timeout(10000) |
| 95 | + |
| 96 | + it("NewCreditOnChunkProgress policy requests new credit when chunk handled at 50% progress", async () => { |
| 97 | + const consumerRef = createConsumerRef() |
| 98 | + const policy = creditsOnChunkProgress(1, 0.5, 1) |
| 99 | + sandbox.on(policy, "onChunkProgress") |
| 100 | + const received: Message[] = [] |
| 101 | + await client.declareConsumer( |
| 102 | + { |
| 103 | + stream: streamName, |
| 104 | + offset: Offset.first(), |
| 105 | + singleActive: true, |
| 106 | + consumerRef: consumerRef, |
| 107 | + creditPolicy: policy, |
| 108 | + }, |
| 109 | + (message: Message) => { |
| 110 | + received.push(message) |
| 111 | + } |
| 112 | + ) |
| 113 | + |
| 114 | + await send(publisher, chunks) |
| 115 | + |
| 116 | + await eventually(() => expect(received.length).eql(nMessages)) |
| 117 | + await eventually(() => expect(policy.onChunkProgress).called.exactly(nMessages)) |
| 118 | + await always(() => expect(policy.onChunkProgress).called.below(nMessages + 1), 5000) |
| 119 | + }).timeout(10000) |
| 120 | + |
| 121 | + it("NewCreditOnChunkProgress policy requests new credit when chunk handled at 100% progress", async () => { |
| 122 | + const consumerRef = createConsumerRef() |
| 123 | + const policy = creditsOnChunkProgress(1, 1, 1) |
| 124 | + sandbox.on(policy, "onChunkProgress") |
| 125 | + const received: Message[] = [] |
| 126 | + await client.declareConsumer( |
| 127 | + { |
| 128 | + stream: streamName, |
| 129 | + offset: Offset.first(), |
| 130 | + singleActive: true, |
| 131 | + consumerRef: consumerRef, |
| 132 | + creditPolicy: policy, |
| 133 | + }, |
| 134 | + (message: Message) => { |
| 135 | + received.push(message) |
| 136 | + } |
| 137 | + ) |
| 138 | + |
| 139 | + await send(publisher, chunks) |
| 140 | + |
| 141 | + await eventually(() => expect(received.length).eql(nMessages)) |
| 142 | + await eventually(() => expect(policy.onChunkProgress).called.exactly(nMessages)) |
| 143 | + await always(() => expect(policy.onChunkProgress).called.below(nMessages + 1), 5000) |
| 144 | + }).timeout(10000) |
| 145 | + |
| 146 | + it("NewCreditOnChunkCompleted policy requests new credit when chunk completely handled", async () => { |
| 147 | + const consumerRef = createConsumerRef() |
| 148 | + const policy = creditsOnChunkCompleted(1, 1) |
| 149 | + sandbox.on(policy, "onChunkCompleted") |
| 150 | + const received: Message[] = [] |
| 151 | + await client.declareConsumer( |
| 152 | + { |
| 153 | + stream: streamName, |
| 154 | + offset: Offset.first(), |
| 155 | + singleActive: true, |
| 156 | + consumerRef: consumerRef, |
| 157 | + creditPolicy: policy, |
| 158 | + }, |
| 159 | + async (message: Message) => { |
| 160 | + received.push(message) |
| 161 | + } |
| 162 | + ) |
| 163 | + |
| 164 | + await send(publisher, chunks) |
| 165 | + |
| 166 | + await eventually(() => expect(received.length).eql(nMessages)) |
| 167 | + await eventually(() => expect(policy.onChunkCompleted).called.exactly(chunks.length)) |
| 168 | + await always(() => expect(policy.onChunkCompleted).called.below(chunks.length + 1), 5000) |
| 169 | + }).timeout(10000) |
| 170 | +}) |
0 commit comments