aboutsummaryrefslogtreecommitdiff
path: root/dedupe/dedupe_test.go
blob: 5891429c73a7571e8fa4858ae63b5668edfdc675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package dedup

import (
	"testing"
	"testing/quick"
	"time"
)

const expiresAfter = time.Millisecond

func TestPacketDeduplicatorSeen(t *testing.T) {
	dedup := NewDeduplicator(expiresAfter)

	sender := uint32(1)
	packetID := uint32(1)

	// Test Seen with new packetID
	if dedup.Seen(sender, packetID) {
		t.Error("Expected the packet to not have been seen")
	}

	// Test Seen with same packetID again
	if !dedup.Seen(sender, packetID) {
		t.Error("Expected the packet to have been seen")
	}

	// Wait for expiration
	time.Sleep(expiresAfter + time.Millisecond)

	// Test Seen with same packetID after expiration
	if dedup.Seen(sender, packetID) {
		t.Error("Expected the packet to not have been seen after expiration")
	}
}

func TestDuplicatorProp(t *testing.T) {
	if err := quick.Check(func(s, p uint32) bool {
		dedup := NewDeduplicator(expiresAfter)
		// Test Seen with new packetID
		if dedup.Seen(s, p) {
			t.Error("Expected the packet to not have been seen")
			return false
		}

		// Test Seen with same packetID again
		if !dedup.Seen(s, p) {
			t.Error("Expected the packet to have been seen")
			return false
		}
		return true
	}, nil); err != nil {
		t.Error(err)
	}
}

func FuzzDup(f *testing.F) {
	f.Fuzz(func(t *testing.T, s uint32, p uint32) {
		dedup := NewDeduplicator(10*time.Second)
		// Test Seen with new packetID
		if dedup.Seen(s, p) {
			t.Errorf("Expected the packet %v/%v to not have been seen the first time", s, p)
		}

		// Test Seen with same packetID again
		if !dedup.Seen(s, p) {
			t.Errorf("Expected the packet %v/%v to have been seen", s, p)
		}

	})
}

func TestPacketDeduplicatorSeenData(t *testing.T) {
	dedup := NewDeduplicator(expiresAfter)

	data := []byte("test data")

	// Test SeenData with new data
	if dedup.SeenData(data) {
		t.Error("Expected the data to not have been seen")
	}

	// Test SeenData with same data again
	if !dedup.SeenData(data) {
		t.Error("Expected the data to have been seen")
	}

	// Wait for expiration
	time.Sleep(expiresAfter + time.Millisecond)

	// Test SeenData with same data after expiration
	if dedup.SeenData(data) {
		t.Error("Expected the data to not have been seen after expiration")
	}
}