File: eventtest.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20231006.7918f67-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 6,492 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 27
file content (65 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download | duplicates (4)
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
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package eventtest supports logging events to a test.
// You can use NewContext to create a context that knows how to deliver
// telemetry events back to the test.
// You must use this context or a derived one anywhere you want telemetry to be
// correctly routed back to the test it was constructed with.
package eventtest

import (
	"context"
	"os"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"golang.org/x/exp/event"
	"golang.org/x/exp/event/adapter/logfmt"
)

// NewContext returns a context you should use for the active test.
func NewContext(ctx context.Context, tb testing.TB) context.Context {
	h := &testHandler{tb: tb}
	return event.WithExporter(ctx, event.NewExporter(h, nil))
}

type testHandler struct {
	tb      testing.TB
	printer logfmt.Printer
}

func (h *testHandler) Event(ctx context.Context, ev *event.Event) context.Context {
	//TODO: choose between stdout and stderr based on the event
	//TODO: decide if we should be calling h.tb.Fail()
	h.printer.Event(os.Stdout, ev)
	return ctx
}

var InitialTime = func() time.Time {
	t, _ := time.Parse(logfmt.TimeFormat, "2020/03/05 14:27:48")
	return t
}()

func ExporterOptions() *event.ExporterOptions {
	nextTime := InitialTime
	return &event.ExporterOptions{
		Now: func() time.Time {
			thisTime := nextTime
			nextTime = nextTime.Add(time.Second)
			return thisTime
		},
	}
}

func CmpOptions() []cmp.Option {
	return []cmp.Option{
		cmpopts.SortSlices(func(x, y event.Label) bool {
			return x.Name < y.Name
		}),
		cmpopts.IgnoreFields(event.Event{}, "At", "ctx", "target", "labels"),
	}
}