Skip to content

Commit 818e949

Browse files
committed
🧪 test(bbgo): update tests for margin info updater
1 parent bbff12c commit 818e949

4 files changed

Lines changed: 56 additions & 22 deletions

File tree

pkg/bbgo/config_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,20 @@ func TestLoadConfig(t *testing.T) {
8080
"envVarPrefix": "MAX",
8181
"marginInfoUpdaterInterval": "3m0s",
8282
// json.Unmarshal decodes numbers into float64, so we need to use float64 here to match the unmarshaled value.
83-
"marginInfoUpdaterBatchSize": float64(3),
84-
"marginInfoUpdaterCooldown": "9m0s",
83+
"marginInfoUpdaterBatchSize": float64(3),
84+
"marginInfoUpdaterCooldown": "9m0s",
85+
"marginInfoUpdaterTradesBufferCount": float64(30),
86+
"marginInfoUpdaterBindSession": false,
8587
},
8688
"binance": map[string]interface{}{
8789
"exchange": "binance",
8890
"envVarPrefix": "BINANCE",
8991
"marginInfoUpdaterInterval": "5m0s",
9092
// json.Unmarshal decodes numbers into float64, so we need to use float64 here to match the unmarshaled value.
91-
"marginInfoUpdaterBatchSize": float64(5),
92-
"marginInfoUpdaterCooldown": "15m0s",
93+
"marginInfoUpdaterBatchSize": float64(5),
94+
"marginInfoUpdaterCooldown": "15m0s",
95+
"marginInfoUpdaterTradesBufferCount": float64(30),
96+
"marginInfoUpdaterBindSession": false,
9397
},
9498
},
9599
"build": map[string]interface{}{

pkg/bbgo/margin_info_updater_test.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ func TestMarginInfoUpdater_GetMaxBorrowable(t *testing.T) {
6565
svc := newMockMarginBorrowRepayService(map[string]fixedpoint.Value{
6666
"BTC": fixedpoint.NewFromFloat(1.5),
6767
})
68-
updater := NewMarginInfoUpdater(svc)
68+
config := MarginInfoUpdaterConfig{
69+
BatchSize: 1,
70+
}
71+
config.Defaults()
72+
updater := NewMarginInfoUpdater(svc, config)
6973

7074
t.Run("untracked asset is not found", func(t *testing.T) {
7175
amount, ok := updater.GetMaxBorrowable("BTC")
@@ -82,7 +86,7 @@ func TestMarginInfoUpdater_GetMaxBorrowable(t *testing.T) {
8286
})
8387

8488
t.Run("reports the queried amount after Update", func(t *testing.T) {
85-
updater.Update(context.Background(), 1)
89+
updater.Update(context.Background())
8690

8791
amount, ok := updater.GetMaxBorrowable("BTC")
8892
assert.True(t, ok)
@@ -97,8 +101,12 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
97101
"ETH": fixedpoint.NewFromFloat(2),
98102
"SOL": fixedpoint.NewFromFloat(3),
99103
}
104+
config := MarginInfoUpdaterConfig{
105+
BatchSize: len(values),
106+
}
107+
config.Defaults()
100108
svc := newMockMarginBorrowRepayService(values)
101-
updater := NewMarginInfoUpdater(svc)
109+
updater := NewMarginInfoUpdater(svc, config)
102110

103111
emitted := map[string]fixedpoint.Value{}
104112
updater.OnMaxBorrowable(func(asset string, amount fixedpoint.Value) {
@@ -110,7 +118,7 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
110118
}
111119

112120
// batch size >= number of assets => all done in one pass
113-
updater.Update(context.Background(), len(values))
121+
updater.Update(context.Background())
114122

115123
for asset, want := range values {
116124
amount, ok := updater.GetMaxBorrowable(asset)
@@ -133,7 +141,11 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
133141
}
134142

135143
svc := newMockMarginBorrowRepayService(values)
136-
updater := NewMarginInfoUpdater(svc)
144+
config := MarginInfoUpdaterConfig{
145+
BatchSize: batchSize,
146+
}
147+
config.Defaults()
148+
updater := NewMarginInfoUpdater(svc, config)
137149

138150
emitCount := map[string]int{}
139151
updater.OnMaxBorrowable(func(asset string, amount fixedpoint.Value) {
@@ -145,13 +157,13 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
145157
}
146158

147159
// A single Update only processes batchSize assets.
148-
updater.Update(context.Background(), batchSize)
160+
updater.Update(context.Background())
149161
assert.Len(t, updater.doneAssets, batchSize, "only one batch processed per Update")
150162

151163
// ceil(5/2) = 3 Update calls are needed to cover every asset. Run the two
152164
// remaining passes to complete the cycle.
153-
updater.Update(context.Background(), batchSize)
154-
updater.Update(context.Background(), batchSize)
165+
updater.Update(context.Background())
166+
updater.Update(context.Background())
155167

156168
// Every asset must have been refreshed exactly once with the correct value.
157169
for asset, want := range values {
@@ -175,18 +187,22 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
175187
"SOL": fixedpoint.NewFromFloat(3),
176188
}
177189
svc := newMockMarginBorrowRepayService(values)
178-
updater := NewMarginInfoUpdater(svc)
190+
config := MarginInfoUpdaterConfig{
191+
BatchSize: len(values),
192+
}
193+
config.Defaults()
194+
updater := NewMarginInfoUpdater(svc, config)
179195

180196
for asset := range values {
181197
updater.AddBorrowableAssets(asset)
182198
}
183199

184200
// first cycle
185-
updater.Update(context.Background(), len(values))
201+
updater.Update(context.Background())
186202
assert.Empty(t, updater.doneAssets, "cycle completes and resets")
187203

188204
// second cycle: each asset should be queried again
189-
updater.Update(context.Background(), len(values))
205+
updater.Update(context.Background())
190206
for asset := range values {
191207
assert.Equal(t, 2, svc.getQueryCount(asset), "queried again in the next cycle: %s", asset)
192208
}
@@ -199,7 +215,11 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
199215
}
200216
svc := newMockMarginBorrowRepayService(values)
201217
svc.errs["ETH"] = fmt.Errorf("boom")
202-
updater := NewMarginInfoUpdater(svc)
218+
config := MarginInfoUpdaterConfig{
219+
BatchSize: 10,
220+
}
221+
config.Defaults()
222+
updater := NewMarginInfoUpdater(svc, config)
203223

204224
emitted := map[string]fixedpoint.Value{}
205225
updater.OnMaxBorrowable(func(asset string, amount fixedpoint.Value) {
@@ -209,7 +229,7 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
209229
updater.AddBorrowableAssets("BTC", "ETH")
210230

211231
// large batch so both are attempted in one pass
212-
updater.Update(context.Background(), 10)
232+
updater.Update(context.Background())
213233

214234
// BTC succeeded; ETH errored so it is neither recorded as done nor emitted.
215235
btc, ok := updater.GetMaxBorrowable("BTC")
@@ -228,7 +248,7 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
228248
delete(svc.errs, "ETH")
229249
svc.mu.Unlock()
230250

231-
updater.Update(context.Background(), 10)
251+
updater.Update(context.Background())
232252
eth, ok := updater.GetMaxBorrowable("ETH")
233253
assert.True(t, ok)
234254
assert.Equal(t, "2", eth.String())
@@ -238,10 +258,13 @@ func TestMarginInfoUpdater_Update(t *testing.T) {
238258

239259
t.Run("no assets is a no-op", func(t *testing.T) {
240260
svc := newMockMarginBorrowRepayService(nil)
241-
updater := NewMarginInfoUpdater(svc)
261+
config := MarginInfoUpdaterConfig{
262+
BatchSize: 5,
263+
}
264+
updater := NewMarginInfoUpdater(svc, config)
242265

243266
assert.NotPanics(t, func() {
244-
updater.Update(context.Background(), 5)
267+
updater.Update(context.Background())
245268
})
246269
})
247270
}

pkg/bbgo/testdata/strategy.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ sessions:
66
marginInfoUpdaterInterval: 3m0s
77
marginInfoUpdaterBatchSize: 3
88
marginInfoUpdaterCooldown: 9m0s
9+
marginInfoUpdaterTradesBufferCount: 30
10+
marginInfoUpdaterBindSession: false
911
binance:
1012
exchange: binance
1113
envVarPrefix: BINANCE
1214
marginInfoUpdaterInterval: 5m0s
1315
marginInfoUpdaterBatchSize: 5
1416
marginInfoUpdaterCooldown: 15m0s
17+
marginInfoUpdaterTradesBufferCount: 30
18+
marginInfoUpdaterBindSession: false
1519

1620

1721
exchangeStrategies:

pkg/strategy/xmaker/borrowable_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,17 @@ func setMarginInfoUpdater(session *bbgo.ExchangeSession, updater *bbgo.MarginInf
5050
// the given per-asset amounts (assets present in the map are "found").
5151
func newBorrowableUpdater(values map[string]fixedpoint.Value) *bbgo.MarginInfoUpdater {
5252
svc := &mockMarginBorrowRepayService{maxBorrowable: values}
53-
updater := bbgo.NewMarginInfoUpdater(svc)
53+
config := bbgo.MarginInfoUpdaterConfig{
54+
BatchSize: len(values),
55+
}
56+
updater := bbgo.NewMarginInfoUpdater(svc, config)
5457

5558
assets := make([]string, 0, len(values))
5659
for asset := range values {
5760
assets = append(assets, asset)
5861
}
5962
updater.AddBorrowableAssets(assets...)
60-
updater.Update(context.Background(), len(assets))
63+
updater.Update(context.Background())
6164
return updater
6265
}
6366

0 commit comments

Comments
 (0)