-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiviner_test.go
91 lines (84 loc) · 2.17 KB
/
diviner_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2019 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package diviner
import (
"testing"
"time"
)
func TestReplicates(t *testing.T) {
var r Replicates
r.Set(2)
if !r.Contains(2) {
t.Error("not contains 3")
}
if r.Completed(3) {
t.Error("complete 3")
}
r.Set(0)
r.Set(1)
if !r.Completed(3) {
t.Error("not complete 3")
}
r.Clear(0)
if r.Completed(3) {
t.Error("complete 3")
}
if got, want := r.Count(), 2; got != want {
t.Errorf("got %v, want %v", got, want)
}
seen := make(map[int]bool)
for num, r := r.Next(); num != -1; num, r = r.Next() {
if seen[num] {
t.Errorf("seen twice: %d", num)
}
seen[num] = true
}
if got, want := len(seen), 2; got != want {
t.Fatalf("got %v, want %v", got, want)
}
if !seen[1] || !seen[2] {
t.Errorf("not seen 1, 2: %v", seen)
}
}
func TestReplicatedTrial(t *testing.T) {
rep := replicatedTrial(
Run{Replicate: 0, State: Success, Metrics: []Metrics{{"x": 1.0}}},
Run{Replicate: 0, State: Pending, Metrics: []Metrics{{"x": 0.5}}},
Run{Replicate: 1, State: Success, Metrics: []Metrics{{"x": 2.0}}},
)
if got, want := rep.Metrics, (Metrics{"x": 1.5}); !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
if got, want := rep.Replicates.Count(), 2; got != want {
t.Errorf("got %v, want %v", got, want)
}
if rep.Pending {
t.Error("rep was pending")
}
var (
now = time.Now()
then = now.Add(-time.Minute)
)
rep = replicatedTrial(
Run{Replicate: 0, State: Pending, Updated: then, Metrics: []Metrics{{"x": 1.0}}},
Run{Replicate: 0, State: Pending, Updated: now, Metrics: []Metrics{{"x": 0.5}}},
Run{Replicate: 1, State: Success, Metrics: []Metrics{{"x": 2.0}}},
)
if got, want := rep.Metrics, (Metrics{"x": 1.25}); !got.Equal(want) {
t.Errorf("got %v, want %v", got, want)
}
if got, want := rep.Replicates.Count(), 2; got != want {
t.Errorf("got %v, want %v", got, want)
}
if !rep.Pending {
t.Error("rep was not pending")
}
}
func replicatedTrial(runs ...Run) Trial {
trials := make([]Trial, len(runs))
for i, run := range runs {
trials[i] = run.Trial()
}
return ReplicatedTrial(trials)
}