diff --git a/exchanges/ticker/ticker.go b/exchanges/ticker/ticker.go index b523985f..6f424803 100644 --- a/exchanges/ticker/ticker.go +++ b/exchanges/ticker/ticker.go @@ -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() diff --git a/exchanges/ticker/ticker_test.go b/exchanges/ticker/ticker_test.go index 7f5d29ed..b850161b 100644 --- a/exchanges/ticker/ticker_test.go +++ b/exchanges/ticker/ticker_test.go @@ -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() + }