-
Notifications
You must be signed in to change notification settings - Fork 19
/
functions.spec.ts
176 lines (155 loc) · 4.9 KB
/
functions.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
SchemaRegistry,
AvroEachMessagePayload,
toAvroEachMessage,
toProducerRecord,
} from '../src';
import { Kafka, logLevel, Admin, Producer, Consumer } from 'kafkajs';
import { retry } from 'ts-retry-promise';
import * as uuid from 'uuid';
import { schema } from 'avsc';
interface MessageType {
stringField: string;
intField?: number | null;
}
type KeyType = number;
const schema: schema.RecordType = {
type: 'record',
name: 'TestMessage',
fields: [
{ type: 'string', name: 'stringField' },
{ type: ['null', 'int'], name: 'intField' },
],
};
const keySchema: schema.PrimitiveType = 'int';
const topic = `dev_avroKafkajs_${uuid.v4()}`;
describe('Functions', () => {
let producer: Producer;
let consumer: Consumer;
let admin: Admin;
let schemaRegistry: SchemaRegistry;
beforeEach(async () => {
const kafka = new Kafka({ brokers: ['localhost:29092'], logLevel: logLevel.NOTHING });
admin = kafka.admin();
consumer = kafka.consumer({ groupId: 'test-1' });
producer = kafka.producer();
schemaRegistry = new SchemaRegistry({ uri: 'http://localhost:8081' });
await Promise.all([consumer.connect(), producer.connect(), admin.connect()]);
});
afterEach(() => Promise.all([consumer.disconnect(), producer.disconnect(), admin.disconnect()]));
it('Should process using helper functions', async () => {
jest.setTimeout(10000);
const consumed: AvroEachMessagePayload<MessageType>[] = [];
await admin.createTopics({ topics: [{ topic, numPartitions: 2 }] });
await consumer.subscribe({ topic });
await consumer.run({
partitionsConsumedConcurrently: 2,
eachMessage: toAvroEachMessage<MessageType>(schemaRegistry, async (payload) => {
consumed.push(payload);
}),
});
await producer.send(
await toProducerRecord<MessageType>(schemaRegistry, {
topic,
schema,
messages: [
{ value: { intField: 10, stringField: 'test1' }, partition: 0, key: 'test-1' },
{ value: { intField: null, stringField: 'test2' }, partition: 1, key: 'test-2' },
],
}),
);
await retry(
async () => {
expect(consumed).toHaveLength(2);
expect(consumed).toContainEqual(
expect.objectContaining({
partition: 0,
message: expect.objectContaining({
key: Buffer.from('test-1'),
value: { intField: 10, stringField: 'test1' },
schema,
}),
}),
);
expect(consumed).toContainEqual(
expect.objectContaining({
partition: 1,
message: expect.objectContaining({
key: Buffer.from('test-2'),
value: { intField: null, stringField: 'test2' },
schema,
}),
}),
);
},
{ delay: 1000, timeout: 4000 },
);
});
it('Should process using raw SchemaRegistry with encoded keys', async () => {
jest.setTimeout(10000);
const consumed: Array<{ value: MessageType; key: KeyType; partition: number }> = [];
await admin.createTopics({ topics: [{ topic, numPartitions: 2 }] });
await consumer.subscribe({ topic });
await consumer.run({
partitionsConsumedConcurrently: 2,
eachMessage: async (payload) => {
if (payload.message.value) {
const value = await schemaRegistry.decode<MessageType>(payload.message.value);
const key = await schemaRegistry.decode<KeyType>(payload.message.key);
consumed.push({ value, key, partition: payload.partition });
}
},
});
const value1 = await schemaRegistry.encode<MessageType>({
topic,
schemaType: 'value',
schema,
value: { intField: 10, stringField: 'test1' },
});
const key1 = await schemaRegistry.encode<KeyType>({
topic,
schemaType: 'key',
schema: keySchema,
value: 101,
});
const value2 = await schemaRegistry.encode<MessageType>({
topic,
schemaType: 'value',
schema,
value: { intField: null, stringField: 'test2' },
});
const key2 = await schemaRegistry.encode<KeyType>({
topic,
schemaType: 'key',
schema: keySchema,
value: 102,
});
await producer.send({
topic,
messages: [
{ value: value1, partition: 0, key: key1 },
{ value: value2, partition: 1, key: key2 },
],
});
await retry(
async () => {
expect(consumed).toHaveLength(2);
expect(consumed).toContainEqual(
expect.objectContaining({
partition: 0,
value: { intField: 10, stringField: 'test1' },
key: 101,
}),
);
expect(consumed).toContainEqual(
expect.objectContaining({
partition: 1,
value: { intField: null, stringField: 'test2' },
key: 102,
}),
);
},
{ delay: 1000, timeout: 4000 },
);
});
});