Skip to content

Commit

Permalink
fix race condition in test
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Oct 9, 2024
1 parent e1f8132 commit 5515758
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions workers/dnstapserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnscollector/telemetry"
"github.com/dmachard/go-dnstap-protobuf"
"github.com/dmachard/go-framestream"
"github.com/dmachard/go-logger"
Expand Down Expand Up @@ -632,8 +633,16 @@ func Test_DnstapProcessor_TelemetryCounters(t *testing.T) {
// run the consumer with a fake logger
fl := GetWorkerForTest(pkgconfig.DefaultBufferSize)

cfg := pkgconfig.GetDefaultConfig()
cfg.Global.Telemetry.Enabled = true
cfg.Global.Worker.InternalMonitor = 1

config := pkgconfig.Config{}
metrics := telemetry.NewPrometheusCollector(&config)

// init the dnstap consumer
consumer := NewDNSTapProcessor(0, "peertest", pkgconfig.GetDefaultConfig(), logger, "test", 512)
consumer := NewDNSTapProcessor(0, "peertest", cfg, logger, "test", 512)
consumer.SetMetrics(metrics)
consumer.AddDefaultRoute(fl)
consumer.AddDroppedRoute(fl)

Expand All @@ -654,12 +663,14 @@ func Test_DnstapProcessor_TelemetryCounters(t *testing.T) {

// add packet to consumer and read output
consumer.GetDataChannel() <- data

<-fl.GetInputChannel()
r := <-metrics.Record

if consumer.totalIngress != 1 {
t.Errorf("invalid total ingress oucnter: got %d expect 1", consumer.totalIngress)
if r.TotalIngress != 1 {
t.Errorf("invalid total ingress oucnter: got %d expect 1", r.TotalIngress)
}
if consumer.totalEgress != 1 {
t.Errorf("invalid total egress counter: got %d expect 1", consumer.totalEgress)
if r.TotalEgress != 1 {
t.Errorf("invalid total egress counter: got %d expect 1", r.TotalEgress)
}
}
2 changes: 1 addition & 1 deletion workers/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (w *GenericWorker) Monitor() {

// // send to telemetry?
if w.config.Global.Telemetry.Enabled && w.metrics != nil {
if w.totalIngress > 0 || w.totalForwarded > 0 || w.totalDropped > 0 {
if w.totalIngress > 0 || w.totalEgress > 0 || w.totalForwarded > 0 || w.totalDropped > 0 {
w.metrics.Record <- telemetry.WorkerStats{
Name: w.GetName(),
TotalIngress: w.totalIngress,
Expand Down

0 comments on commit 5515758

Please sign in to comment.