-
Notifications
You must be signed in to change notification settings - Fork 1
/
e2e.go
137 lines (117 loc) · 3.57 KB
/
e2e.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
134
135
136
137
package e2e
import (
"math/rand"
"net"
"net/http"
"strconv"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/xmtp/xmtpd/pkg/context"
"github.com/xmtp/xmtpd/pkg/zap"
"golang.org/x/sync/errgroup"
_ "net/http/pprof"
)
type E2E struct {
ctx context.Context
log *zap.Logger
metrics *Metrics
rand *rand.Rand
randMu sync.Mutex
opts *Options
}
type Options struct {
APIURLs []string `long:"api-url" env:"XMTP_API_URLS" description:"XMTP node API URLs" default:"http://localhost"`
ClientsPerURL int `long:"clients-per-url" description:"Number of clients for each API URL" default:"1"`
MessagePerClient int `long:"messages-per-client" description:"Number of messages to publish for each client" default:"3"`
Continuous bool `long:"continuous" description:"Run continuously"`
ExitOnError bool `long:"exit-on-error" description:"Exit on error if running continuously"`
RunDelay time.Duration `long:"delay" description:"Delay between runs" default:"5s"`
AdminPort uint `long:"admin-port" description:"Admin HTTP server listen port" default:"7777"`
QueryConvergenceDelay time.Duration `long:"query-convergence-delay" description:"Delay between query convergence checks" default:"10ms"`
GitCommit string
}
type testRunFunc func(name string) error
type Test struct {
Name string
Run testRunFunc
}
func (e *E2E) Tests() []*Test {
return []*Test{
e.newTest("convergence", e.testConvergence),
}
}
func New(ctx context.Context, opts *Options) (*E2E, error) {
e := &E2E{
ctx: ctx,
log: ctx.Logger().Named("e2e"),
metrics: newMetrics(),
rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
opts: opts,
}
e.log.Info("running", zap.String("git-commit", opts.GitCommit), zap.Strings("nodes", opts.APIURLs))
if e.opts.Continuous {
go func() {
// Initialize HTTP server for profiler and metrics.
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {})
addr := net.JoinHostPort("0.0.0.0", strconv.Itoa(int(opts.AdminPort)))
go func() {
e.log.Info("serving admin http", zap.String("address", addr))
err := http.ListenAndServe(addr, nil)
if err != nil {
e.log.Error("serving e2e admin", zap.Error(err))
}
}()
}()
}
for {
g, _ := errgroup.WithContext(e.ctx)
for _, test := range e.Tests() {
test := test
g.Go(func() error {
return e.runTest(test)
})
}
err := g.Wait()
if err != nil && (!e.opts.Continuous || e.opts.ExitOnError) {
return nil, err
}
if !e.opts.Continuous {
break
}
time.Sleep(e.opts.RunDelay)
}
return e, nil
}
func (e *E2E) runTest(test *Test) error {
started := time.Now().UTC()
log := e.log.With(zap.String("test", test.Name))
err := test.Run(test.Name)
duration := time.Since(started)
log = log.With(zap.Duration("duration", duration))
if err != nil {
log.Error("test failed", zap.Error(err))
e.metrics.recordRun(e.ctx, test.Name, "failed", duration)
return err
}
log.Info("test passed")
e.metrics.recordRun(e.ctx, test.Name, "passed", duration)
return nil
}
func (e *E2E) newTest(name string, runFn testRunFunc) *Test {
return &Test{
Name: name,
Run: runFn,
}
}
func (e *E2E) randomStringLower(n int) string {
e.randMu.Lock()
defer e.randMu.Unlock()
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[e.rand.Intn(len(letterRunes))]
}
return string(b)
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")