| 1 |
package eventrules |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMetrics(t *testing.T) { |
| 16 |
var ( |
| 17 |
i int |
| 18 |
ctx = context.Background() |
| 19 |
p = &scheduler.Event{} |
| 20 |
a = errors.New("a") |
| 21 |
h = func(f func() error, _ ...string) error { |
| 22 |
i++ |
| 23 |
return f() |
| 24 |
} |
| 25 |
) |
| 26 |
for ti, tc := range []struct { |
| 27 |
ctx context.Context |
| 28 |
e *scheduler.Event |
| 29 |
err error |
| 30 |
}{ |
| 31 |
{ctx, p, a}, |
| 32 |
{ctx, p, nil}, |
| 33 |
{ctx, nil, a}, |
| 34 |
} { |
| 35 |
for ri, r := range []Rule{ |
| 36 |
Metrics(h, nil), |
| 37 |
Metrics(h, func(_ context.Context, _ *scheduler.Event) []string { return nil }), |
| 38 |
} { |
| 39 |
c, e, err := r.Eval(tc.ctx, tc.e, tc.err, ChainIdentity) |
| 40 |
if !reflect.DeepEqual(c, tc.ctx) { |
| 41 |
t.Errorf("test case %d: expected context %q instead of %q", ti, tc.ctx, c) |
| 42 |
} |
| 43 |
if !reflect.DeepEqual(e, tc.e) { |
| 44 |
t.Errorf("test case %d: expected event %q instead of %q", ti, tc.e, e) |
| 45 |
} |
| 46 |
if !reflect.DeepEqual(err, tc.err) { |
| 47 |
t.Errorf("test case %d: expected error %q instead of %q", ti, tc.err, err) |
| 48 |
} |
| 49 |
if y := (ti * 2) + ri + 1; y != i { |
| 50 |
t.Errorf("test case %d: expected count %q instead of %q", ti, y, i) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
func() { |
| 55 |
defer func() { |
| 56 |
if x := recover(); x != nil { |
| 57 |
t.Log("intercepted expected panic", x) |
| 58 |
} |
| 59 |
}() |
| 60 |
_ = Metrics(nil, nil) |
| 61 |
t.Fatalf("expected a panic because nil harness is not allowed") |
| 62 |
}() |
| 63 |
} |