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