Skip to content

Commit

Permalink
fix antispam && test (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryRomanov authored Mar 1, 2024
1 parent 8f3cbe4 commit 766777f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pipeline/antispam/antispammer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Antispammer struct {
}

type source struct {
counter atomic.Int32
counter *atomic.Int32
name string
}

Expand Down Expand Up @@ -103,7 +103,7 @@ func (a *Antispammer) IsSpam(id uint64, name string, isNewSource bool, event []b
src = newSrc
} else {
src = source{
counter: atomic.Int32{},
counter: &atomic.Int32{},
name: name,
}
a.sources[id] = src
Expand Down
44 changes: 44 additions & 0 deletions pipeline/antispam/antispammer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package antispam

import (
"testing"
"time"

"github.com/ozontech/file.d/logger"
"github.com/ozontech/file.d/metric"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)

func TestAntispam(t *testing.T) {
r := require.New(t)

threshold := 5
unbanIterations := 2
antispamer := NewAntispammer(Options{
MaintenanceInterval: time.Second * 1,
Threshold: threshold,
UnbanIterations: unbanIterations,
Logger: logger.Instance.Named("antispam").Desugar(),
MetricsController: metric.NewCtl("test", prometheus.NewRegistry()),
})

checkSpam := func() bool {
return antispamer.IsSpam(1, "test", false, []byte(`{}`))
}

for i := 0; i < threshold-1; i++ {
result := checkSpam()
r.False(result)
}

result := checkSpam()
r.True(result)

for i := 0; i <= unbanIterations; i++ {
antispamer.Maintenance()
}

result = checkSpam()
r.False(result)
}

0 comments on commit 766777f

Please sign in to comment.