Download this file
| 1 | package hostfiles |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestQueueArmsOnTheFirstTouchAndLaterTouchesDoNotPushItBack(t *testing.T) { |
| 10 | q := newQueue(time.Second) |
| 11 | t0 := time.Unix(1000, 0) |
| 12 | q.Touch("a", t0) |
| 13 | q.Touch("a", t0.Add(900*time.Millisecond)) // still being written |
| 14 | if got := q.Due(t0.Add(999 * time.Millisecond)); len(got) != 0 { |
| 15 | t.Fatalf("due early: %v", got) |
| 16 | } |
| 17 | if got := q.Due(t0.Add(time.Second)); !reflect.DeepEqual(got, []string{"a"}) { |
| 18 | t.Fatalf("due = %v, want [a]", got) |
| 19 | } |
| 20 | if got := q.Due(t0.Add(2 * time.Second)); len(got) != 0 { |
| 21 | t.Fatalf("due twice: %v", got) |
| 22 | } |
| 23 | q.Touch("a", t0.Add(3*time.Second)) // a new modification arms a new deadline |
| 24 | if got := q.Due(t0.Add(4 * time.Second)); !reflect.DeepEqual(got, []string{"a"}) { |
| 25 | t.Fatalf("second deadline: %v", got) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestQueueReturnsDuePathsSorted(t *testing.T) { |
| 30 | q := newQueue(time.Second) |
| 31 | t0 := time.Unix(1000, 0) |
| 32 | for _, p := range []string{"c", "a", "b"} { |
| 33 | q.Touch(p, t0) |
| 34 | } |
| 35 | if got := q.Due(t0.Add(time.Second)); !reflect.DeepEqual(got, []string{"a", "b", "c"}) { |
| 36 | t.Fatalf("due = %v", got) |
| 37 | } |
| 38 | } |
| 39 | |