Skip to content

Commit

Permalink
Engine: Fix false test passes for nil OrderManager
Browse files Browse the repository at this point in the history
TestWebsocketRoutineManagerSetup tests that passing a nil value returns errNilOrderManager;
However that's not actually what would happen when order manager is configured off.

The arguments to setupWebsocketRoutineManager are interface types.
When a nil pointer of interface type is passed, it does NOT equal nil.
nil is a primitive type.
A nil pointer of interface type has the value (nil;type).
See [the Go lang
spec](https://golang.org/ref/spec#Comparison_operators):
```
Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil.
```

So that means that whilst this test was passing, because it was sending
in a real nil value and comparing it to a real nil, that's not what
would happen at runtime. At runtime the bot.OrderManager would be a nil
pointer to a concrete type *OrderManager, and so not comparible to nil.

This commit just fixes that oversight, and explains the often
misunderstood mechanics of comparing interface types to nil.

In practical terms this means that the tests assert that the WSRM would
not run without a OM, but in fact it actually would. And panic later.

This commit SHOULD introduce a FAILing test. Sorry if you're bisecting.
  • Loading branch information
gbjk committed Sep 7, 2023
1 parent 51f7330 commit 441c014
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion engine/websocketroutine_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestWebsocketRoutineManagerSetup(t *testing.T) {
t.Errorf("error '%v', expected '%v'", err, errNilExchangeManager)
}

_, err = setupWebsocketRoutineManager(NewExchangeManager(), nil, nil, nil, false)
_, err = setupWebsocketRoutineManager(NewExchangeManager(), (*OrderManager)(nil), nil, nil, false)
if !errors.Is(err, errNilOrderManager) {
t.Errorf("error '%v', expected '%v'", err, errNilOrderManager)
}
Expand Down

0 comments on commit 441c014

Please sign in to comment.