-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathtesting.go
50 lines (40 loc) · 1.07 KB
/
testing.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package metric
import (
"context"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
// testableObserverVec allows us to assert which observations are being made
// with which labels.
type TestableObserverVec struct {
Observations []*testableObserver
prometheus.ObserverVec
}
func (v *TestableObserverVec) With(l prometheus.Labels) prometheus.Observer {
ret := &testableObserver{Labels: l}
v.Observations = append(v.Observations, ret)
return ret
}
type testableObserver struct {
Labels prometheus.Labels
Observation float64
}
func (o *testableObserver) Observe(f float64) {
o.Observation = f
}
type TestInvoker struct {
T *testing.T
Called bool
RetErr error
}
func (i *TestInvoker) Invoke(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
i.Called = true
require.NotNil(i.T, ctx)
require.NotEmpty(i.T, method)
require.NotNil(i.T, req)
return i.RetErr
}