| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package eventtest |
| 11 |
|
| 12 |
import ( |
| 13 |
"context" |
| 14 |
"os" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/google/go-cmp/cmp" |
| 19 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 20 |
"golang.org/x/exp/event" |
| 21 |
"golang.org/x/exp/event/adapter/logfmt" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
func NewContext(ctx context.Context, tb testing.TB) context.Context { |
| 26 |
h := &testHandler{tb: tb} |
| 27 |
return event.WithExporter(ctx, event.NewExporter(h, nil)) |
| 28 |
} |
| 29 |
|
| 30 |
type testHandler struct { |
| 31 |
tb testing.TB |
| 32 |
printer logfmt.Printer |
| 33 |
} |
| 34 |
|
| 35 |
func (h *testHandler) Event(ctx context.Context, ev *event.Event) context.Context { |
| 36 |
|
| 37 |
|
| 38 |
h.printer.Event(os.Stdout, ev) |
| 39 |
return ctx |
| 40 |
} |
| 41 |
|
| 42 |
var InitialTime = func() time.Time { |
| 43 |
t, _ := time.Parse(logfmt.TimeFormat, "2020/03/05 14:27:48") |
| 44 |
return t |
| 45 |
}() |
| 46 |
|
| 47 |
func ExporterOptions() *event.ExporterOptions { |
| 48 |
nextTime := InitialTime |
| 49 |
return &event.ExporterOptions{ |
| 50 |
Now: func() time.Time { |
| 51 |
thisTime := nextTime |
| 52 |
nextTime = nextTime.Add(time.Second) |
| 53 |
return thisTime |
| 54 |
}, |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func CmpOptions() []cmp.Option { |
| 59 |
return []cmp.Option{ |
| 60 |
cmpopts.SortSlices(func(x, y event.Label) bool { |
| 61 |
return x.Name < y.Name |
| 62 |
}), |
| 63 |
cmpopts.IgnoreFields(event.Event{}, "At", "ctx", "target", "labels"), |
| 64 |
} |
| 65 |
} |