-
Notifications
You must be signed in to change notification settings - Fork 24
/
lossy_logging_test.go
75 lines (60 loc) · 2.24 KB
/
lossy_logging_test.go
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
package feeds
import (
"testing"
"time"
"github.com/ossf/package-feeds/pkg/events"
)
func TestProcessPackagesNoOverlap(t *testing.T) {
t.Parallel()
feedName := "foo-feed"
mockSink := &events.MockSink{}
allowLossyFeedEventsFilter := events.NewFilter([]string{events.LossyFeedEventType}, nil, nil)
eventHandler := events.NewHandler(mockSink, *allowLossyFeedEventsFilter)
lossyFeedAlerter := NewLossyFeedAlerter(eventHandler)
baseTime := time.Date(2021, 4, 20, 14, 30, 0, 0, time.UTC)
pkgs1 := []*Package{
NewPackage(baseTime.Add(-time.Minute*2), "foopkg", "1.0", feedName),
NewPackage(baseTime.Add(-time.Minute*3), "barpkg", "1.0", feedName),
}
// Populate previous packages
lossyFeedAlerter.ProcessPackages(feedName, pkgs1)
pkgs2 := []*Package{
NewPackage(baseTime, "bazpkg", "1.0", feedName),
NewPackage(baseTime.Add(-time.Minute*1), "quxpkg", "2.0", feedName),
}
// Trigger no overlap
lossyFeedAlerter.ProcessPackages(feedName, pkgs2)
evs := mockSink.GetEvents()
if len(evs) != 1 {
t.Fatalf("ProcessPackages failed to detect a lack of overlap")
}
if evs[0].GetType() != events.LossyFeedEventType {
t.Errorf("ProcessPackages did not produce a lossy feed event")
}
}
func TestProcessPackagesWithOverlap(t *testing.T) {
t.Parallel()
feedName := "foo-feed"
mockSink := &events.MockSink{}
allowLossyFeedEventsFilter := events.NewFilter([]string{events.LossyFeedEventType}, nil, nil)
eventHandler := events.NewHandler(mockSink, *allowLossyFeedEventsFilter)
lossyFeedAlerter := NewLossyFeedAlerter(eventHandler)
baseTime := time.Date(2021, 4, 20, 14, 30, 0, 0, time.UTC)
pkgs1 := []*Package{
NewPackage(baseTime.Add(-time.Minute*2), "foopkg", "1.0", feedName),
NewPackage(baseTime.Add(-time.Minute*3), "barpkg", "1.0", feedName),
}
// Populate previous packages
lossyFeedAlerter.ProcessPackages(feedName, pkgs1)
pkgs2 := []*Package{
NewPackage(baseTime, "bazpkg", "1.0", feedName),
NewPackage(baseTime.Add(-time.Minute*1), "quxpkg", "2.0", feedName),
NewPackage(baseTime.Add(-time.Minute*2), "foopkg", "1.0", feedName),
}
// Trigger overlap
lossyFeedAlerter.ProcessPackages(feedName, pkgs2)
evs := mockSink.GetEvents()
if len(evs) != 0 {
t.Fatalf("ProcessPackages failed to identify an overlap when one existed")
}
}