-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathlifecycle_test.go
116 lines (97 loc) · 3.01 KB
/
lifecycle_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package oracle_test
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/skip-mev/connect/v2/oracle"
oraclefactory "github.com/skip-mev/connect/v2/providers/factories/oracle"
)
func TestStart(t *testing.T) {
t.Run("errors when init fails", func(t *testing.T) {
o, err := oracle.New(
oracleCfg,
noOpPriceAggregator{},
oracle.WithLogger(logger),
)
require.NoError(t, err)
err = o.Start(context.Background())
require.Error(t, err)
o.Stop()
})
t.Run("price providers with no market map", func(t *testing.T) {
orc, err := oracle.New(
oracleCfg,
noOpPriceAggregator{},
oracle.WithLogger(logger),
oracle.WithPriceAPIQueryHandlerFactory(oraclefactory.APIQueryHandlerFactory),
oracle.WithPriceWebSocketQueryHandlerFactory(oraclefactory.WebSocketQueryHandlerFactory),
)
require.NoError(t, err)
o := orc.(*oracle.OracleImpl)
go func() {
err := o.Start(context.Background())
if !errors.Is(err, context.Canceled) {
t.Errorf("Start() should have returned context.Canceled error")
}
}()
time.Sleep(5 * time.Second)
state := o.GetProviderState()
require.Equal(t, len(state), len(oracleCfg.Providers))
// Stop the oracle.
o.Stop()
})
t.Run("price providers with market map", func(t *testing.T) {
orc, err := oracle.New(
oracleCfg,
noOpPriceAggregator{},
oracle.WithLogger(logger),
oracle.WithPriceAPIQueryHandlerFactory(oraclefactory.APIQueryHandlerFactory),
oracle.WithPriceWebSocketQueryHandlerFactory(oraclefactory.WebSocketQueryHandlerFactory),
oracle.WithMarketMap(marketMap),
)
require.NoError(t, err)
o := orc.(*oracle.OracleImpl)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
err := o.Start(ctx)
if !errors.Is(err, context.Canceled) {
t.Errorf("Start() should have returned context.Canceled error")
}
}()
time.Sleep(5 * time.Second)
state := o.GetProviderState()
require.Equal(t, len(state), len(oracleCfg.Providers))
// Stop the oracle.
o.Stop()
})
t.Run("price providers with market map provider but price providers have no ids to fetch", func(t *testing.T) {
orc, err := oracle.New(
oracleCfgWithMapper,
noOpPriceAggregator{},
oracle.WithLogger(logger),
oracle.WithPriceAPIQueryHandlerFactory(oraclefactory.APIQueryHandlerFactory),
oracle.WithPriceWebSocketQueryHandlerFactory(oraclefactory.WebSocketQueryHandlerFactory),
oracle.WithMarketMapperFactory(oraclefactory.MarketMapProviderFactory),
)
require.NoError(t, err)
o := orc.(*oracle.OracleImpl)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
err := o.Start(ctx)
if !errors.Is(err, context.Canceled) {
t.Errorf("Start() should have returned context.Canceled error")
}
}()
time.Sleep(5 * time.Second)
state := o.GetProviderState()
require.Equal(t, len(state), len(oracleCfgWithMapper.Providers)-1)
mapper := o.GetMarketMapProvider()
require.NotNil(t, mapper)
// Stop the oracle.
o.Stop()
})
}