|
1 | 1 | import { expect } from "chai" |
2 | 2 | import { randomUUID } from "crypto" |
3 | | -import { Client } from "../../src" |
| 3 | +import { Client, Offset } from "../../src" |
4 | 4 | import { createClient, createStreamName } from "../support/fake_data" |
5 | 5 | import { Rabbit } from "../support/rabbit" |
6 | 6 | import { eventually, username, password } from "../support/util" |
@@ -47,4 +47,103 @@ describe("filtering", () => { |
47 | 47 | expect((await rabbit.getQueueInfo(streamName)).messages).eql(3) |
48 | 48 | }, 10000) |
49 | 49 | }).timeout(10000) |
| 50 | + |
| 51 | + it("published messages are filtered on the consumer side", async () => { |
| 52 | + const filteredMsg: string[] = [] |
| 53 | + const publisher = await client.declarePublisher( |
| 54 | + { stream: streamName, publisherRef: `my-publisher-${randomUUID()}` }, |
| 55 | + (msg) => msg.applicationProperties!["test"].toString() |
| 56 | + ) |
| 57 | + const message1 = "test1" |
| 58 | + const message2 = "test2" |
| 59 | + const message3 = "test3" |
| 60 | + const applicationProperties1 = { test: "A" } |
| 61 | + const applicationProperties2 = { test: "B" } |
| 62 | + const applicationProperties3 = { test: "C" } |
| 63 | + await publisher.send(Buffer.from(message1), { applicationProperties: applicationProperties1 }) |
| 64 | + await publisher.send(Buffer.from(message2), { applicationProperties: applicationProperties2 }) |
| 65 | + await publisher.send(Buffer.from(message3), { applicationProperties: applicationProperties3 }) |
| 66 | + |
| 67 | + await client.declareConsumer( |
| 68 | + { |
| 69 | + stream: streamName, |
| 70 | + offset: Offset.first(), |
| 71 | + filter: { |
| 72 | + values: ["A", "B"], |
| 73 | + postFilterFunc: (msg) => msg.applicationProperties!["test"] === "A", |
| 74 | + matchUnfiltered: true, |
| 75 | + }, |
| 76 | + }, |
| 77 | + (msg) => filteredMsg.push(msg.content.toString("utf-8")) |
| 78 | + ) |
| 79 | + |
| 80 | + await eventually(async () => { |
| 81 | + expect(filteredMsg[0]).eql("test1") |
| 82 | + expect(filteredMsg.length).eql(1) |
| 83 | + }, 10000) |
| 84 | + }).timeout(10000) |
| 85 | + |
| 86 | + it("published messages are filtered on the server side keeping only the ones with filter value", async () => { |
| 87 | + const filteredMsg: string[] = [] |
| 88 | + const publisher = await client.declarePublisher( |
| 89 | + { stream: streamName, publisherRef: `my-publisher-${randomUUID()}` }, |
| 90 | + (msg) => (msg.applicationProperties ? msg.applicationProperties["test"].toString() : undefined) |
| 91 | + ) |
| 92 | + const applicationProperties1 = { test: "A" } |
| 93 | + const applicationProperties2 = { test: "B" } |
| 94 | + for (let i = 0; i < 1000; i++) |
| 95 | + await publisher.send(Buffer.from(`test${i + 1}`), { applicationProperties: applicationProperties1 }) |
| 96 | + for (let i = 0; i < 1000; i++) |
| 97 | + await publisher.send(Buffer.from(`test${i + 1}`), { applicationProperties: applicationProperties2 }) |
| 98 | + for (let i = 0; i < 1000; i++) await publisher.send(Buffer.from(`test${i + 1}`)) |
| 99 | + |
| 100 | + await client.declareConsumer( |
| 101 | + { |
| 102 | + stream: streamName, |
| 103 | + offset: Offset.first(), |
| 104 | + filter: { |
| 105 | + values: ["A", "B"], |
| 106 | + postFilterFunc: (_msg) => true, |
| 107 | + matchUnfiltered: false, |
| 108 | + }, |
| 109 | + }, |
| 110 | + (msg) => filteredMsg.push(msg.content.toString("utf-8")) |
| 111 | + ) |
| 112 | + |
| 113 | + await eventually(async () => { |
| 114 | + expect(filteredMsg.length).eql(2000) |
| 115 | + }, 10000) |
| 116 | + }).timeout(10000) |
| 117 | + |
| 118 | + it("published messages are filtered on the server side keeping even the ones with filter value", async () => { |
| 119 | + const filteredMsg: string[] = [] |
| 120 | + const publisher = await client.declarePublisher( |
| 121 | + { stream: streamName, publisherRef: `my-publisher-${randomUUID()}` }, |
| 122 | + (msg) => (msg.applicationProperties ? msg.applicationProperties["test"].toString() : undefined) |
| 123 | + ) |
| 124 | + const applicationProperties1 = { test: "A" } |
| 125 | + const applicationProperties2 = { test: "B" } |
| 126 | + for (let i = 0; i < 1000; i++) |
| 127 | + await publisher.send(Buffer.from(`test${i + 1}`), { applicationProperties: applicationProperties1 }) |
| 128 | + for (let i = 0; i < 1000; i++) |
| 129 | + await publisher.send(Buffer.from(`test${i + 1}`), { applicationProperties: applicationProperties2 }) |
| 130 | + for (let i = 0; i < 1000; i++) await publisher.send(Buffer.from(`test${i + 1}`)) |
| 131 | + |
| 132 | + await client.declareConsumer( |
| 133 | + { |
| 134 | + stream: streamName, |
| 135 | + offset: Offset.first(), |
| 136 | + filter: { |
| 137 | + values: ["A", "B"], |
| 138 | + postFilterFunc: (_msg) => true, |
| 139 | + matchUnfiltered: true, |
| 140 | + }, |
| 141 | + }, |
| 142 | + (msg) => filteredMsg.push(msg.content.toString("utf-8")) |
| 143 | + ) |
| 144 | + |
| 145 | + await eventually(async () => { |
| 146 | + expect(filteredMsg.length).eql(3000) |
| 147 | + }, 10000) |
| 148 | + }).timeout(10000) |
50 | 149 | }) |
0 commit comments