Skip to content

Commit d66d7b1

Browse files
authored
engine: Harden GetOrderbookAmountByNominal/Impact tests against shared-state flakiness (thrasher-corp#2191)
* engine: harden orderbook nominal/impact tests against nil panics and shared state * engine: use counter-based unique test exchange names
1 parent 6f0d5d7 commit d66d7b1

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

engine/rpcserver_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ const (
6161
fakeExchangeName = "fake"
6262
)
6363

64-
var errExpectedTestError = errors.New("expected test error")
64+
var (
65+
errExpectedTestError = errors.New("expected test error")
66+
testExchangeCounter common.Counter
67+
)
68+
69+
func newUniqueFakeExchangeName() string {
70+
return fmt.Sprintf("%s-%d", fakeExchangeName, testExchangeCounter.IncrementAndGet())
71+
}
6572

6673
// fExchange is a fake exchange with function overrides
6774
// we're not testing an actual exchange's implemented functions
@@ -3141,7 +3148,8 @@ func TestGetOrderbookAmountByNominal(t *testing.T) {
31413148

31423149
exch.SetDefaults()
31433150
b := exch.GetBase()
3144-
b.Name = fakeExchangeName
3151+
uniqueFakeExchangeName := newUniqueFakeExchangeName()
3152+
b.Name = uniqueFakeExchangeName
31453153
b.Enabled = true
31463154

31473155
cp := currency.NewPairWithDelimiter("btc", "meme", "-")
@@ -3167,7 +3175,7 @@ func TestGetOrderbookAmountByNominal(t *testing.T) {
31673175
_, err = s.GetOrderbookAmountByNominal(t.Context(), req)
31683176
require.ErrorIs(t, err, common.ErrExchangeNameNotSet)
31693177

3170-
req.Exchange = "fake"
3178+
req.Exchange = uniqueFakeExchangeName
31713179
_, err = s.GetOrderbookAmountByNominal(t.Context(), req)
31723180
require.ErrorIs(t, err, asset.ErrNotSupported)
31733181

@@ -3181,9 +3189,7 @@ func TestGetOrderbookAmountByNominal(t *testing.T) {
31813189
Quote: currency.MEME.String(),
31823190
}
31833191
_, err = s.GetOrderbookAmountByNominal(t.Context(), req)
3184-
if !strings.Contains(err.Error(), "cannot find orderbook") {
3185-
t.Fatalf("received: '%+v' but expected: '%v'", err, "cannot find orderbook")
3186-
}
3192+
require.ErrorIs(t, err, orderbook.ErrOrderbookNotFound)
31873193

31883194
depth, err := orderbook.DeployDepth(req.Exchange, currency.NewPair(currency.BTC, currency.MEME), asset.Spot)
31893195
require.NoError(t, err, "orderbook.DeployDepth must not error")
@@ -3227,7 +3233,8 @@ func TestGetOrderbookAmountByImpact(t *testing.T) {
32273233

32283234
exch.SetDefaults()
32293235
b := exch.GetBase()
3230-
b.Name = fakeExchangeName
3236+
uniqueFakeExchangeName := newUniqueFakeExchangeName()
3237+
b.Name = uniqueFakeExchangeName
32313238
b.Enabled = true
32323239

32333240
cp := currency.NewPairWithDelimiter("btc", "mad", "-")
@@ -3253,7 +3260,7 @@ func TestGetOrderbookAmountByImpact(t *testing.T) {
32533260
_, err = s.GetOrderbookAmountByImpact(t.Context(), req)
32543261
require.ErrorIs(t, err, common.ErrExchangeNameNotSet)
32553262

3256-
req.Exchange = "fake"
3263+
req.Exchange = uniqueFakeExchangeName
32573264
_, err = s.GetOrderbookAmountByImpact(t.Context(), req)
32583265
require.ErrorIs(t, err, asset.ErrNotSupported)
32593266

@@ -3267,9 +3274,7 @@ func TestGetOrderbookAmountByImpact(t *testing.T) {
32673274
Quote: currency.MAD.String(),
32683275
}
32693276
_, err = s.GetOrderbookAmountByImpact(t.Context(), req)
3270-
if !strings.Contains(err.Error(), "cannot find orderbook") {
3271-
t.Fatalf("received: '%+v' but expected: '%v'", err, "cannot find orderbook")
3272-
}
3277+
require.ErrorIs(t, err, orderbook.ErrOrderbookNotFound)
32733278

32743279
depth, err := orderbook.DeployDepth(req.Exchange, currency.NewPair(currency.BTC, currency.MAD), asset.Spot)
32753280
require.NoError(t, err, "orderbook.DeployDepth must not error")

0 commit comments

Comments
 (0)