forked from skip-mev/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_test.go
99 lines (79 loc) · 2.5 KB
/
metrics_test.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
package oracle_test
import (
"context"
"math/big"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/skip-mev/slinky/oracle"
metric_mocks "github.com/skip-mev/slinky/oracle/metrics/mocks"
providertypes "github.com/skip-mev/slinky/providers/types"
provider_mocks "github.com/skip-mev/slinky/providers/types/mocks"
oracletypes "github.com/skip-mev/slinky/x/oracle/types"
)
type OracleMetricsTestSuite struct {
suite.Suite
// mocked providers
mockProvider1 *provider_mocks.Provider[oracletypes.CurrencyPair, *big.Int]
mockProvider2 *provider_mocks.Provider[oracletypes.CurrencyPair, *big.Int]
// mock metrics
mockMetrics *metric_mocks.Metrics
o oracle.Oracle
}
const (
oracleTicker = 1 * time.Second
provider1 = "provider1"
provider2 = "provider2"
)
func TestOracleMetricsTestSuite(t *testing.T) {
suite.Run(t, new(OracleMetricsTestSuite))
}
func (s *OracleMetricsTestSuite) SetupTest() {
// mock providers
s.mockProvider1 = provider_mocks.NewProvider[oracletypes.CurrencyPair, *big.Int](s.T())
s.mockProvider1.On("Name").Return("provider1").Maybe()
s.mockProvider2 = provider_mocks.NewProvider[oracletypes.CurrencyPair, *big.Int](s.T())
s.mockProvider2.On("Name").Return("provider2").Maybe()
// mock metrics
s.mockMetrics = metric_mocks.NewMetrics(s.T())
var err error
s.o, err = oracle.New(
oracle.WithUpdateInterval(oracleTicker),
oracle.WithProviders(
[]providertypes.Provider[oracletypes.CurrencyPair, *big.Int]{
s.mockProvider1,
s.mockProvider2,
},
),
oracle.WithMetrics(s.mockMetrics),
)
s.Require().NoError(err)
}
// TearDownTest is run after each test in the suite.
func (s *OracleMetricsTestSuite) TearDownTest(_ *testing.T) {
checkFn := func() bool {
return !s.o.IsRunning()
}
s.Eventually(checkFn, 5*time.Second, 100*time.Millisecond)
}
// test Tick metrics are updated correctly
func (s *OracleMetricsTestSuite) TestTickMetric() {
// expect tick to be called
s.mockMetrics.On("AddTick").Return()
s.mockProvider1.On("Name").Return("provider1")
s.mockProvider1.On("Start", mock.Anything).Return(nil)
s.mockProvider1.On("GetData").Return(nil)
s.mockProvider2.On("Name").Return("provider2")
s.mockProvider2.On("Start", mock.Anything).Return(nil)
s.mockProvider2.On("GetData").Return(nil, nil)
// wait for a tick on the oracle
go func() {
s.o.Start(context.Background())
}()
// wait for a tick
time.Sleep(4 * oracleTicker)
// assert expectations
s.mockMetrics.AssertExpectations(s.T())
s.o.Stop()
}