-
Notifications
You must be signed in to change notification settings - Fork 5
/
publish_test.go
133 lines (115 loc) · 2.99 KB
/
publish_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package scientist
import (
"fmt"
"testing"
)
func TestPublish(t *testing.T) {
e := New("publish")
e.Use(func() (interface{}, error) {
return 1, nil
})
e.Try(func() (interface{}, error) {
return 2, nil
})
published := false
reported := false
e.Publish(func(r Result) error {
published = true
if r.Experiment.Name != "publish" {
t.Errorf("Bad experiment name: %q", r.Experiment.Name)
}
return nil
})
e.ReportErrors(func(errors ...ResultError) {
reported = true
t.Errorf("result errors reported :(")
})
v, err := e.Run()
if v != 1 {
t.Errorf("Unexpected control value: %d", v)
}
if err != nil {
t.Errorf("Unexpected control error: %v", err)
}
if !published {
t.Errorf("results never published")
}
if reported {
t.Errorf("result errors reported :(")
}
}
func TestPublishWithErrors(t *testing.T) {
e := New("publish")
e.Use(func() (interface{}, error) {
return 1, nil
})
e.Try(func() (interface{}, error) {
return 2, nil
})
e.BeforeRun(func() error {
return fmt.Errorf("(before)")
})
e.Compare(func(control, candidate interface{}) (bool, error) {
return true, fmt.Errorf("(compare) candidate: %d", candidate)
})
// ignore callback 0, no error
e.Ignore(func(control, candidate interface{}) (bool, error) {
return false, nil
})
// ignore callback 1, returns an error
e.Ignore(func(control, candidate interface{}) (bool, error) {
return true, fmt.Errorf("(ignore) candidate: %d", candidate)
})
published := false
reported := make(map[string]int)
e.Publish(func(r Result) error {
published = true
return fmt.Errorf("(publish) result: %s", r.Experiment.Name)
})
e.ReportErrors(func(errors ...ResultError) {
for _, err := range errors {
reported[err.Operation] = reported[err.Operation] + 1
if err.Experiment != e.Name {
t.Errorf("Bad experiment name for %q operation: %q", err.Operation, err.Experiment)
}
switch err.Operation {
case "compare":
if actual := err.Error(); actual != "(compare) candidate: 2" {
t.Errorf("Bad error message for compare operation: %q", actual)
}
case "ignore":
if actual := err.Error(); actual != "(ignore) candidate: 2" {
t.Errorf("Bad error message for ignore operation: %q", actual)
}
case "publish":
if actual := err.Error(); actual != "(publish) result: publish" {
t.Errorf("Bad error message for publish operation: %q", actual)
}
case "before_run":
if actual := err.Error(); actual != "(before)" {
t.Errorf("Bad error message for before_run operation: %q", actual)
}
default:
t.Errorf("Bad operation: %q", err.Operation)
}
}
})
v, err := e.Run()
if v != 1 {
t.Errorf("Unexpected control value: %d", v)
}
if err != nil {
t.Errorf("Unexpected control error: %v", err)
}
if !published {
t.Errorf("results never published")
}
if len(reported) != 4 {
t.Errorf("all result errors not reported: %v", reported)
}
for key, times := range reported {
if times != 1 {
t.Errorf("%q errors reported %d times", key, times)
}
}
}