CI / Lint (push) Failing after 14m46s
CI / Test (push) Failing after 4m23s
CI / Build (darwin/amd64) (push) Skipped
CI / Build (linux/amd64) (push) Skipped
CI / Build (windows/amd64) (push) Skipped
CI / Build (darwin/arm64) (push) Skipped
CI / Build (linux/arm64) (push) Skipped
CI / Build (windows/arm64) (push) Skipped
CI / Release (push) Skipped
107 lines
2.1 KiB
Go
107 lines
2.1 KiB
Go
package dedup
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestShouldIgnoreOwnOrigin(t *testing.T) {
|
|
f := NewFilter("machine-a")
|
|
|
|
p := Payload{
|
|
Text: "hello",
|
|
Ts: time.Now().UnixNano(),
|
|
Origin: "machine-a", // same as our origin
|
|
}
|
|
|
|
if !f.ShouldIgnore(p) {
|
|
t.Error("should ignore payload from own origin")
|
|
}
|
|
}
|
|
|
|
func TestShouldIgnoreStaleTimestamp(t *testing.T) {
|
|
f := NewFilter("machine-a")
|
|
f.MarkWritten("previous text")
|
|
|
|
// Wait a tiny bit so the timestamp is definitely in the past.
|
|
time.Sleep(time.Millisecond)
|
|
|
|
p := Payload{
|
|
Text: "hello",
|
|
Ts: f.LastWriteTs() - 1, // older than our last write
|
|
Origin: "machine-b",
|
|
}
|
|
|
|
if !f.ShouldIgnore(p) {
|
|
t.Error("should ignore payload with stale timestamp")
|
|
}
|
|
}
|
|
|
|
func TestShouldIgnoreDuplicateHash(t *testing.T) {
|
|
f := NewFilter("machine-a")
|
|
f.MarkWritten("hello world")
|
|
|
|
p := Payload{
|
|
Text: "hello world",
|
|
Ts: time.Now().UnixNano(),
|
|
Origin: "machine-b",
|
|
}
|
|
|
|
if !f.ShouldIgnore(p) {
|
|
t.Error("should ignore payload with recently written hash")
|
|
}
|
|
}
|
|
|
|
func TestShouldAcceptNewContent(t *testing.T) {
|
|
f := NewFilter("machine-a")
|
|
f.MarkWritten("old content")
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
p := Payload{
|
|
Text: "new content",
|
|
Ts: time.Now().UnixNano(),
|
|
Origin: "machine-b",
|
|
}
|
|
|
|
if f.ShouldIgnore(p) {
|
|
t.Error("should accept new content from other origin")
|
|
}
|
|
}
|
|
|
|
func TestCacheEviction(t *testing.T) {
|
|
f := NewFilter("machine-a")
|
|
|
|
// Fill the cache with 20 different texts.
|
|
for i := 0; i < defaultCacheSize; i++ {
|
|
f.MarkWritten("text")
|
|
}
|
|
|
|
// The latest write is "text" — it should be in cache.
|
|
p := Payload{
|
|
Text: "text",
|
|
Ts: time.Now().UnixNano(),
|
|
Origin: "machine-b",
|
|
}
|
|
if !f.ShouldIgnore(p) {
|
|
t.Error("'text' should still be in cache after 20 writes")
|
|
}
|
|
}
|
|
|
|
func TestMarkWrittenUpdatesTimestamp(t *testing.T) {
|
|
f := NewFilter("machine-a")
|
|
|
|
before := f.LastWriteTs()
|
|
if before != 0 {
|
|
t.Errorf("initial LastWriteTs = %d, want 0", before)
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
f.MarkWritten("some text")
|
|
|
|
after := f.LastWriteTs()
|
|
if after <= before {
|
|
t.Errorf("LastWriteTs not updated: %d <= %d", after, before)
|
|
}
|
|
}
|