-
Notifications
You must be signed in to change notification settings - Fork 14
/
ignore_test.go
49 lines (41 loc) · 1.18 KB
/
ignore_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
package metafora
import (
"testing"
"time"
)
func TestIgnore(t *testing.T) {
t.Parallel()
out := make(chan Task)
stop := make(chan struct{})
defer close(stop)
// Create ignorer
im := ignorer(out, stop)
// Ignore task for 200ms. Yes this is racy. Might need to bump deadline.
deadline1 := time.Now().Add(200 * time.Millisecond)
im.add(testTask{"1"}, deadline1)
// Ensure it's ignored
if !im.ignored("1") {
t.Fatal("test task should have been ignored but wasn't")
}
// Ignore task for 10ms to make sure tasks are returned in order (they aren't
// *guaranteed* to be in order since adds and evictions are concurrent)
deadline2 := time.Now().Add(10 * time.Millisecond)
im.add(testTask{"2"}, deadline2)
// Wait for the first eviction
eviction := <-out
if eviction.ID() != "2" {
t.Fatal("Expected 2 to be evicted before 1")
}
now := time.Now()
if now.Before(deadline2) {
t.Fatalf("First eviction happened too soon: %s < %s", now, deadline2)
}
eviction = <-out
if eviction.ID() != "1" {
t.Fatal("Expected 1 to be evicted second, found ", eviction)
}
now = time.Now()
if now.Before(deadline1) {
t.Fatalf("First eviction happened too soon: %s < %s", now, deadline1)
}
}