mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
outlet/kafka: make lag test more robust
The consumer may not have started when testing initial lag. Just try a bit more.
This commit is contained in:
@@ -367,7 +367,7 @@ func TestKafkaLagMetric(t *testing.T) {
|
|||||||
defer cluster.Close()
|
defer cluster.Close()
|
||||||
|
|
||||||
// Watch for autocommits to avoid relying on time
|
// Watch for autocommits to avoid relying on time
|
||||||
clusterCommitNotification := make(chan interface{})
|
clusterCommitNotification := make(chan any)
|
||||||
cluster.Control(func(request kmsg.Request) (kmsg.Response, error, bool) {
|
cluster.Control(func(request kmsg.Request) (kmsg.Response, error, bool) {
|
||||||
switch k := kmsg.Key(request.Key()); k {
|
switch k := kmsg.Key(request.Key()); k {
|
||||||
case kmsg.OffsetCommit:
|
case kmsg.OffsetCommit:
|
||||||
@@ -404,7 +404,7 @@ func TestKafkaLagMetric(t *testing.T) {
|
|||||||
helpers.StartStop(t, c)
|
helpers.StartStop(t, c)
|
||||||
|
|
||||||
// Start a worker with a callback that blocks on a channel after receiving a message
|
// Start a worker with a callback that blocks on a channel after receiving a message
|
||||||
workerBlockReceive := make(chan interface{})
|
workerBlockReceive := make(chan any)
|
||||||
defer close(workerBlockReceive)
|
defer close(workerBlockReceive)
|
||||||
c.StartWorkers(func(_ int, _ chan<- ScaleRequest) (ReceiveFunc, ShutdownFunc) {
|
c.StartWorkers(func(_ int, _ chan<- ScaleRequest) (ReceiveFunc, ShutdownFunc) {
|
||||||
return func(context.Context, []byte) error {
|
return func(context.Context, []byte) error {
|
||||||
@@ -414,14 +414,24 @@ func TestKafkaLagMetric(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// No messages yet, no lag
|
// No messages yet, no lag
|
||||||
time.Sleep(10 * time.Millisecond)
|
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
|
||||||
gotMetrics := r.GetMetrics("akvorado_outlet_kafka_", "consumergroup", "workers")
|
defer cancel()
|
||||||
expected := map[string]string{
|
for {
|
||||||
"consumergroup_lag_messages": "0",
|
gotMetrics := r.GetMetrics("akvorado_outlet_kafka_", "consumergroup", "workers")
|
||||||
"workers": "1",
|
expected := map[string]string{
|
||||||
}
|
"consumergroup_lag_messages": "0",
|
||||||
if diff := helpers.Diff(gotMetrics, expected); diff != "" {
|
"workers": "1",
|
||||||
t.Fatalf("Metrics (-got, +want):\n%s", diff)
|
}
|
||||||
|
if diff := helpers.Diff(gotMetrics, expected); diff != "" {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Fatalf("Metrics (-got, +want):\n%s", diff)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a single message, allow it to be processed
|
// Send a single message, allow it to be processed
|
||||||
@@ -442,8 +452,8 @@ func TestKafkaLagMetric(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The message was processed, there's no lag
|
// The message was processed, there's no lag
|
||||||
gotMetrics = r.GetMetrics("akvorado_outlet_kafka_", "consumergroup", "received_messages_total")
|
gotMetrics := r.GetMetrics("akvorado_outlet_kafka_", "consumergroup", "received_messages_total")
|
||||||
expected = map[string]string{
|
expected := map[string]string{
|
||||||
"consumergroup_lag_messages": "0",
|
"consumergroup_lag_messages": "0",
|
||||||
`received_messages_total{worker="0"}`: "1",
|
`received_messages_total{worker="0"}`: "1",
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user