Skip to content

Commit

Permalink
engine: fix potential difference between exchange config name and def…
Browse files Browse the repository at this point in the history
…ault name (thrasher-corp#1041)

* engine: make sure upper and lower case names standardize from config to default and return error if sync item never matches

* nits: Add tests for @gloriousCode

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
  • Loading branch information
shazbert and Ryan O'Hara-Reid authored Sep 21, 2022
1 parent 0184b8c commit 0b95786
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
3 changes: 3 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,9 @@ func (bot *Engine) LoadExchange(name string, wg *sync.WaitGroup) error {
}
}

// NOTE: This will standardize name to default and apply it to the config.
exchCfg.Name = exch.GetName()

exchCfg.Enabled = true
err = exch.Setup(exchCfg)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions engine/sync_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
errNoSyncItemsEnabled = errors.New("no sync items enabled")
errUnknownSyncItem = errors.New("unknown sync item")
errSyncPairNotFound = errors.New("exchange currency pair syncer not found")
errCouldNotSyncNewData = errors.New("could not sync new data")
)

// setupSyncManager starts a new CurrencyPairSyncer
Expand Down Expand Up @@ -421,7 +422,7 @@ func (m *syncManager) Update(exchangeName string, p currency.Pair, a asset.Item,
createdCounter)
m.initSyncWG.Done()
}

return nil
case SyncItemOrderbook:
origHadData := m.currencyPairs[x].Orderbook.HaveData
m.currencyPairs[x].Orderbook.LastUpdated = time.Now()
Expand All @@ -439,7 +440,7 @@ func (m *syncManager) Update(exchangeName string, p currency.Pair, a asset.Item,
createdCounter)
m.initSyncWG.Done()
}

return nil
case SyncItemTrade:
origHadData := m.currencyPairs[x].Trade.HaveData
m.currencyPairs[x].Trade.LastUpdated = time.Now()
Expand All @@ -457,10 +458,11 @@ func (m *syncManager) Update(exchangeName string, p currency.Pair, a asset.Item,
createdCounter)
m.initSyncWG.Done()
}
return nil
}
}
}
return nil
return fmt.Errorf("%w for %s %s %s", errCouldNotSyncNewData, exchangeName, p, a)
}

func (m *syncManager) worker() {
Expand Down
74 changes: 74 additions & 0 deletions engine/sync_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,77 @@ func TestWaitForInitialSync(t *testing.T) {
t.Fatalf("received %v, but expected: %v", err, nil)
}
}

func TestSyncManagerUpdate(t *testing.T) {
t.Parallel()
var m *syncManager
err := m.Update("", currency.EMPTYPAIR, 1, 47, nil)
if !errors.Is(err, ErrNilSubsystem) {
t.Fatalf("received %v, but expected: %v", err, ErrNilSubsystem)
}

m = &syncManager{}
err = m.Update("", currency.EMPTYPAIR, 1, 47, nil)
if !errors.Is(err, ErrSubSystemNotStarted) {
t.Fatalf("received %v, but expected: %v", err, ErrSubSystemNotStarted)
}

m.started = 1
// not started initial sync
err = m.Update("", currency.EMPTYPAIR, 1, 47, nil)
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}

m.initSyncStarted = 1
// orderbook not enabled
err = m.Update("", currency.EMPTYPAIR, 1, 1, nil)
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}

m.config.SynchronizeOrderbook = true
// ticker not enabled
err = m.Update("", currency.EMPTYPAIR, 1, 0, nil)
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}

m.config.SynchronizeTicker = true
// trades not enabled
err = m.Update("", currency.EMPTYPAIR, 1, 2, nil)
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}

m.config.SynchronizeTrades = true
err = m.Update("", currency.EMPTYPAIR, 1, 1336, nil)
if !errors.Is(err, errUnknownSyncItem) {
t.Fatalf("received %v, but expected: %v", err, errUnknownSyncItem)
}

err = m.Update("", currency.EMPTYPAIR, 1, 1, nil)
if !errors.Is(err, errCouldNotSyncNewData) {
t.Fatalf("received %v, but expected: %v", err, errCouldNotSyncNewData)
}

m.currencyPairs = append(m.currencyPairs, currencyPairSyncAgent{AssetType: 1})
m.initSyncWG.Add(3)
// orderbook match
err = m.Update("", currency.EMPTYPAIR, 1, 1, errors.New("test"))
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}

// ticker match
err = m.Update("", currency.EMPTYPAIR, 1, 0, errors.New("test"))
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}

// trades match
err = m.Update("", currency.EMPTYPAIR, 1, 2, errors.New("test"))
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}
}

0 comments on commit 0b95786

Please sign in to comment.