52 lines
904 B
Go
52 lines
904 B
Go
package test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/twmb/franz-go/pkg/kgo"
|
|
"mailnotifier/internal/models"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_Kafka(t *testing.T) {
|
|
brokers := []string{"localhost:9092"}
|
|
topic := "test-topic"
|
|
|
|
client, err := kgo.NewClient(
|
|
kgo.SeedBrokers(brokers...),
|
|
kgo.DefaultProduceTopic(topic),
|
|
)
|
|
if err != nil {
|
|
fmt.Println("Failed to send message to Kafka topic:", err)
|
|
}
|
|
|
|
defer func() {
|
|
client.Close()
|
|
}()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
message := models.Message{
|
|
AccountID: "",
|
|
Email: "pashamullin2001@gmail.com",
|
|
SendAt: time.Now(),
|
|
}
|
|
|
|
msg, err := json.Marshal(message)
|
|
if err != nil {
|
|
fmt.Println("errr")
|
|
continue
|
|
}
|
|
|
|
resp := client.ProduceSync(context.Background(), &kgo.Record{
|
|
Value: msg,
|
|
})
|
|
|
|
if err := resp.FirstErr(); err != nil {
|
|
fmt.Println("Failed to send message to Kafka topic:", err)
|
|
}
|
|
}
|
|
|
|
}
|