11import { randomUUID } from 'node:crypto'
22import { KafkaJS } from '@confluentinc/kafka-javascript'
33import { waitAndRetry } from '@lokalise/universal-ts-utils/node'
4+ import {
5+ Consumer ,
6+ type Message ,
7+ ProduceAcks ,
8+ Producer ,
9+ stringDeserializers ,
10+ } from '@platformatic/kafka'
11+ import { stringSerializers } from '@platformatic/kafka'
412import { type TestContext , registerDependencies } from '../test/testContext.ts'
513
614describe ( 'Test' , ( ) => {
@@ -14,59 +22,112 @@ describe('Test', () => {
1422 await testContext . dispose ( )
1523 } )
1624
17- it ( 'should send and receive a message' , async ( ) => {
18- // Given
19- const clientId = randomUUID ( )
20- const groupId = randomUUID ( )
21- // Use a fresh, unique topic per run to avoid stale state
22- const topic = `admin-test-topic-${ Date . now ( ) } `
23- const messageValue = 'My test message'
24-
25- const kafka = new KafkaJS . Kafka ( {
26- 'client.id' : clientId ,
27- 'bootstrap.servers' : testContext . cradle . kafkaConfig . brokers . join ( ',' ) ,
28- } )
25+ describe ( 'confluentic' , ( ) => {
26+ it ( 'should send and receive a message' , async ( ) => {
27+ // Given
28+ const clientId = randomUUID ( )
29+ const groupId = randomUUID ( )
30+ // Use a fresh, unique topic per run to avoid stale state
31+ const topic = `test-topic-${ Date . now ( ) } `
32+ const messageValue = 'My test message'
2933
30- // Topics can be created from producers, but as we will first connect a consumer, we need to create the topic first
31- const admin = kafka . admin ( )
32- await admin . connect ( )
33- await admin . createTopics ( {
34- topics : [ { topic } ] ,
35- } )
34+ const kafka = new KafkaJS . Kafka ( {
35+ 'client.id' : clientId ,
36+ 'bootstrap.servers' : testContext . cradle . kafkaConfig . brokers . join ( ',' ) ,
37+ } )
3638
37- const messages : string [ ] = [ ]
39+ // Topics can be created from producers, but as we will first connect a consumer, we need to create the topic first
40+ const admin = kafka . admin ( )
41+ await admin . connect ( )
42+ await admin . createTopics ( {
43+ topics : [ { topic } ] ,
44+ } )
3845
39- const consumer = kafka . consumer ( { 'group.id' : groupId } )
40- await consumer . connect ( )
41- await consumer . subscribe ( { topic } )
46+ const messages : string [ ] = [ ]
4247
43- await consumer . run ( {
44- eachMessage : ( { message } ) => {
45- const messageString = message . value ?. toString ( )
46- if ( messageString ) messages . push ( messageString )
47- return Promise . resolve ( )
48- } ,
49- } )
50- // Wait for the consumer to be assigned partitions
51- await waitAndRetry ( ( ) => consumer . assignment ( ) . length > 0 , 100 , 10 )
52-
53- // When
54- const producer = kafka . producer ( )
55- await producer . connect ( )
56- await producer . send ( {
57- topic,
58- messages : [ { value : messageValue } ] ,
48+ const consumer = kafka . consumer ( { 'group.id' : groupId } )
49+ await consumer . connect ( )
50+ await consumer . subscribe ( { topic } )
51+
52+ await consumer . run ( {
53+ eachMessage : ( { message } ) => {
54+ const messageString = message . value ?. toString ( )
55+ if ( messageString ) messages . push ( messageString )
56+ return Promise . resolve ( )
57+ } ,
58+ } )
59+ // Wait for the consumer to be assigned partitions
60+ await waitAndRetry ( ( ) => consumer . assignment ( ) . length > 0 , 100 , 10 )
61+
62+ // When
63+ const producer = kafka . producer ( )
64+ await producer . connect ( )
65+ await producer . send ( {
66+ topic,
67+ messages : [ { value : messageValue } ] ,
68+ } )
69+
70+ // Then
71+ await waitAndRetry ( ( ) => messages . length > 0 )
72+
73+ // Cleaning up before checks to avoid stale state
74+ await consumer . disconnect ( )
75+ await producer . disconnect ( )
76+ await admin . disconnect ( )
77+
78+ expect ( messages ) . toHaveLength ( 1 )
79+ expect ( messages [ 0 ] ) . toEqual ( messageValue )
5980 } )
81+ } )
82+
83+ describe ( 'platformatic' , ( ) => {
84+ it ( 'should send and receive a message' , async ( ) => {
85+ // Given
86+ const clientId = randomUUID ( )
87+ // Use a fresh, unique topic per run to avoid stale state
88+ const topic = `test-topic-${ Date . now ( ) } `
89+ const messageValue = 'My test message'
90+
91+ const receivedMessages : Message < string , string , string , string > [ ] = [ ]
92+
93+ // Create producer
94+ const producer = new Producer ( {
95+ clientId,
96+ bootstrapBrokers : testContext . cradle . kafkaConfig . brokers ,
97+ serializers : stringSerializers ,
98+ autocreateTopics : true ,
99+ } )
60100
61- // Then
62- await waitAndRetry ( ( ) => messages . length > 0 )
101+ // Create consumer
102+ const consumer = new Consumer ( {
103+ clientId,
104+ groupId : randomUUID ( ) ,
105+ bootstrapBrokers : testContext . cradle . kafkaConfig . brokers ,
106+ deserializers : stringDeserializers ,
107+ autocreateTopics : true ,
108+ } )
63109
64- // Cleaning up before checks to avoid stale state
65- await consumer . disconnect ( )
66- await producer . disconnect ( )
67- await admin . disconnect ( )
110+ const stream = await consumer . consume ( { topics : [ topic ] } )
111+ stream . on ( 'data' , ( message ) => {
112+ receivedMessages . push ( message )
113+ stream . close ( )
114+ } )
68115
69- expect ( messages ) . toHaveLength ( 1 )
70- expect ( messages [ 0 ] ) . toEqual ( messageValue )
116+ // When
117+ await producer . send ( {
118+ messages : [ { topic, value : messageValue } ] ,
119+ acks : ProduceAcks . NO_RESPONSE ,
120+ } )
121+
122+ // Then
123+ await waitAndRetry ( ( ) => receivedMessages . length > 0 )
124+
125+ // Cleanup
126+ producer . close ( )
127+ consumer . close ( )
128+
129+ expect ( receivedMessages ) . toHaveLength ( 1 )
130+ expect ( receivedMessages [ 0 ] ?. value ?. toString ( ) ) . toBe ( messageValue )
131+ } )
71132 } )
72133} )
0 commit comments