ProcessTicker: added a condtion to check for existing tickers

In line with https://github.com/thrasher-/gocryptotrader/pull/197/

Thanks to @shazbert and @andreygrehov
This commit is contained in:
Adrian Gallagher
2018-10-31 11:46:49 +11:00
parent 640a7e6a83
commit 1f2516dbd0
2 changed files with 63 additions and 14 deletions

View File

@@ -157,10 +157,6 @@ func ProcessTicker(exchangeName string, p pair.CurrencyPair, tickerNew Price, ti
tickerNew.CurrencyPair = p.Pair().String()
tickerNew.LastUpdated = time.Now()
if len(Tickers) == 0 {
CreateNewTicker(exchangeName, p, tickerNew, tickerType)
return
}
ticker, err := GetTickerByExchange(exchangeName)
if err != nil {
@@ -169,16 +165,12 @@ func ProcessTicker(exchangeName string, p pair.CurrencyPair, tickerNew Price, ti
}
if FirstCurrencyExists(exchangeName, p.FirstCurrency) {
if !SecondCurrencyExists(exchangeName, p) {
m.Lock()
a := ticker.Price[p.FirstCurrency]
b := make(map[string]Price)
b[tickerType] = tickerNew
a[p.SecondCurrency] = b
ticker.Price[p.FirstCurrency] = a
m.Unlock()
return
}
m.Lock()
a := make(map[string]Price)
a[tickerType] = tickerNew
ticker.Price[p.FirstCurrency][p.SecondCurrency] = a
m.Unlock()
return
}
m.Lock()

View File

@@ -1,8 +1,12 @@
package ticker
import (
"math/rand"
"reflect"
"strconv"
"sync"
"testing"
"time"
"github.com/thrasher-/gocryptotrader/currency/pair"
)
@@ -275,4 +279,57 @@ func TestProcessTicker(t *testing.T) { //non-appending function to tickers
if err != nil {
t.Fatal("Test failed. TestProcessTicker failed to return an existing ticker")
}
type quick struct {
Name string
P pair.CurrencyPair
TP Price
}
var testArray []quick
_ = rand.NewSource(time.Now().Unix())
var wg sync.WaitGroup
var sm sync.Mutex
for i := 0; i < 500; i++ {
wg.Add(1)
go func() {
newName := "Exchange" + strconv.FormatInt(rand.Int63(), 10)
newPairs := pair.NewCurrencyPair("BTC"+strconv.FormatInt(rand.Int63(), 10),
"USD"+strconv.FormatInt(rand.Int63(), 10))
tp := Price{
Pair: newPairs,
CurrencyPair: newPairs.Pair().String(),
Last: rand.Float64(),
}
ProcessTicker(newName, newPairs, tp, Spot)
sm.Lock()
testArray = append(testArray, quick{Name: newName, P: newPairs, TP: tp})
sm.Unlock()
wg.Done()
}()
}
wg.Wait()
for _, test := range testArray {
wg.Add(1)
go func(test quick) {
result, err := GetTicker(test.Name, test.P, Spot)
if err != nil {
t.Fatal("Test failed. TestProcessTicker failed to retrieve new ticker")
}
if result.Last != test.TP.Last {
t.Error("Test failed. TestProcessTicker failed bad values")
}
wg.Done()
}(test)
}
wg.Wait()
}