Files
akvorado/inlet/flow/root_test.go
Vincent Bernat 756e4a8fbd */kafka: switch to franz-go
The concurrency of this library is easier to handle than Sarama.
Notably, it is more compatible with the new model of "almost share
nothing" we use for the inlet and the outlet. The lock for workers in
outlet is removed. We can now use sync.Pool to allocate slice of bytes
in inlet.

It may also be more performant.

In the future, we may want to commit only when pushing data to
ClickHouse. However, this does not seem easy when there is a rebalance.
In case of rebalance, we need to do something when a partition is
revoked to avoid duplicating data. For example, we could flush the
current batch to ClickHouse. Have a look at the
`example/mark_offsets/main.go` file in franz-go repository for a
possible approach. In the meantime, we rely on autocommit.

Another contender could be https://github.com/segmentio/kafka-go. Also
see https://github.com/twmb/franz-go/pull/1064.
2025-07-27 21:44:28 +02:00

108 lines
2.2 KiB
Go

// SPDX-FileCopyrightText: 2022 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
package flow
import (
"bytes"
"fmt"
"path"
"runtime"
"sync"
"testing"
"time"
"akvorado/common/daemon"
"akvorado/common/helpers"
"akvorado/common/httpserver"
kafkaCommon "akvorado/common/kafka"
"akvorado/common/pb"
"akvorado/common/reporter"
"akvorado/inlet/flow/input/file"
"akvorado/inlet/kafka"
"github.com/twmb/franz-go/pkg/kgo"
)
func TestFlow(t *testing.T) {
_, src, _, _ := runtime.Caller(0)
base := path.Join(path.Dir(src), "input", "file", "testdata")
paths := []string{
path.Join(base, "file1.txt"),
path.Join(base, "file2.txt"),
}
inputs := []InputConfiguration{
{
Config: &file.Configuration{
Paths: paths,
MaxFlows: 100,
},
},
}
r := reporter.NewMock(t)
config := DefaultConfiguration()
config.Inputs = inputs
producer, cluster := kafka.NewMock(t, r, kafka.DefaultConfiguration())
defer cluster.Close()
// Use the new helper to intercept messages
var mu sync.Mutex
helloCount := 0
byeCount := 0
totalCount := 0
done := make(chan bool)
kafkaCommon.InterceptMessages(t, cluster, func(record *kgo.Record) {
mu.Lock()
defer mu.Unlock()
// Check topic
expectedTopic := fmt.Sprintf("flows-v%d", pb.Version)
if record.Topic != expectedTopic {
t.Errorf("Expected topic %s, got %s", expectedTopic, record.Topic)
return
}
// Count messages based on content
if bytes.Contains(record.Value, []byte("hello world!")) {
helloCount++
} else if bytes.Contains(record.Value, []byte("bye bye")) {
byeCount++
}
totalCount++
if totalCount >= 100 {
close(done)
}
})
c, err := New(r, config, Dependencies{
Daemon: daemon.NewMock(t),
HTTP: httpserver.NewMock(t, r),
Kafka: producer,
})
if err != nil {
t.Fatalf("New() error:\n%+v", err)
}
helpers.StartStop(t, c)
// Wait for flows
select {
case <-done:
// Check that we got the expected number of each message type
mu.Lock()
if helloCount != 50 {
t.Errorf("Expected 50 'hello world!' messages, got %d", helloCount)
}
if byeCount != 50 {
t.Errorf("Expected 50 'bye bye' messages, got %d", byeCount)
}
mu.Unlock()
case <-time.After(time.Second):
t.Fatalf("flows not received")
}
}