Skip to content

Commit

Permalink
fix(wallet)_: add tests
Browse files Browse the repository at this point in the history
fixes #21071
  • Loading branch information
friofry committed Sep 10, 2024
1 parent 66d65b3 commit 0de4099
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 46 deletions.
46 changes: 46 additions & 0 deletions services/wallet/common/mock/feed_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mock_common

import (
"time"

"github.com/ethereum/go-ethereum/event"
"github.com/status-im/status-go/services/wallet/walletevent"
)

type FeedSubscription struct {
events chan walletevent.Event
feed *event.Feed
done chan struct{}
}

func NewFeedSubscription(feed *event.Feed) *FeedSubscription {
events := make(chan walletevent.Event, 100)
done := make(chan struct{})

subscription := feed.Subscribe(events)

go func() {
<-done
subscription.Unsubscribe()
close(events)
}()

return &FeedSubscription{events: events, feed: feed, done: done}
}

func (f *FeedSubscription) WaitForEvent(timeout time.Duration) (walletevent.Event, bool) {
select {
case evt := <-f.events:
return evt, true
case <-time.After(timeout):
return walletevent.Event{}, false
}
}

func (f *FeedSubscription) GetFeed() *event.Feed {
return f.feed
}

func (f *FeedSubscription) Close() {
close(f.done)
}
13 changes: 13 additions & 0 deletions services/wallet/common/mock/online_checker_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mock_common

type MockOnlineChecker struct {
online bool
}

func (m *MockOnlineChecker) Online() bool {
return m.online
}

func (m *MockOnlineChecker) SetOnline(status bool) {
m.online = status
}
2 changes: 1 addition & 1 deletion services/wallet/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (pm *Manager) setIsConnected(value bool) {
if value {
message = "up"
}
if pm.onlineChecker.Online() {
if pm.onlineChecker == nil || pm.onlineChecker.Online() {
pm.feed.Send(walletevent.Event{
Type: EventMarketStatusChanged,
Accounts: []common.Address{},
Expand Down
84 changes: 84 additions & 0 deletions services/wallet/market/market_feed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package market

import (
"testing"
"time"

"github.com/stretchr/testify/suite"

"github.com/ethereum/go-ethereum/event"
mock_common "github.com/status-im/status-go/services/wallet/common/mock"
mock_market "github.com/status-im/status-go/services/wallet/market/mock"
"github.com/status-im/status-go/services/wallet/thirdparty"
)

type MarketTestSuite struct {
suite.Suite
manager *Manager
feedSub *mock_common.FeedSubscription
symbols []string
currencies []string
onlineChecker *mock_common.MockOnlineChecker
}

func (s *MarketTestSuite) SetupTest() {
priceProviderWithError := &mock_market.MockPriceProviderWithError{}

feed := new(event.Feed)
s.feedSub = mock_common.NewFeedSubscription(feed)

s.symbols = []string{"BTC", "ETH"}
s.currencies = []string{"USD", "EUR"}
s.onlineChecker = &mock_common.MockOnlineChecker{}
s.manager = NewManager([]thirdparty.MarketDataProvider{priceProviderWithError}, feed, s.onlineChecker)
}

func (s *MarketTestSuite) TearDownTest() {
s.feedSub.Close()
}

func (s *MarketTestSuite) TestEventSentWhenOnlineCheckerIsNil() {
// GIVEN
priceProviderWithError := &mock_market.MockPriceProviderWithError{}
manager := NewManager([]thirdparty.MarketDataProvider{priceProviderWithError}, s.feedSub.GetFeed(), nil)

// WHEN
_, err := manager.FetchPrices(s.symbols, s.currencies)
s.Require().Error(err, "expected error from FetchPrices due to MockPriceProviderWithError")
event, ok := s.feedSub.WaitForEvent(5 * time.Second)
s.Require().True(ok, "expected an event, but none was received")

// THEN
s.Require().Equal(event.Type, EventMarketStatusChanged)
}

func (s *MarketTestSuite) TestEventSentWhenOnlineCheckerIsOnline() {
// GIVEN
s.onlineChecker.SetOnline(true)

// WHEN
_, err := s.manager.FetchPrices(s.symbols, s.currencies)
s.Require().Error(err, "expected error from FetchPrices due to MockPriceProviderWithError")
event, ok := s.feedSub.WaitForEvent(5 * time.Second)
s.Require().True(ok, "expected an event, but none was received")

// THEN
s.Require().Equal(event.Type, EventMarketStatusChanged)
}

func (s *MarketTestSuite) TestNoEventSentWhenOnlineCheckerIsOffline() {
// GIVEN
s.onlineChecker.SetOnline(false)

// WHEN
_, err := s.manager.FetchPrices(s.symbols, s.currencies)
s.Require().Error(err, "expected error from FetchPrices due to MockPriceProviderWithError")
_, ok := s.feedSub.WaitForEvent(time.Millisecond * 500)

//THEN
s.Require().False(ok, "expected no event, but one was received")
}

func TestMarketTestSuite(t *testing.T) {
suite.Run(t, new(MarketTestSuite))
}
51 changes: 7 additions & 44 deletions services/wallet/market/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,13 @@ import (

"github.com/stretchr/testify/require"

mock_market "github.com/status-im/status-go/services/wallet/market/mock"
"github.com/status-im/status-go/services/wallet/thirdparty"
mock_thirdparty "github.com/status-im/status-go/services/wallet/thirdparty/mock"
)

type MockPriceProvider struct {
mock_thirdparty.MockMarketDataProvider
mockPrices map[string]map[string]float64
}

func NewMockPriceProvider(ctrl *gomock.Controller) *MockPriceProvider {
return &MockPriceProvider{
MockMarketDataProvider: *mock_thirdparty.NewMockMarketDataProvider(ctrl),
}
}

func (mpp *MockPriceProvider) setMockPrices(prices map[string]map[string]float64) {
mpp.mockPrices = prices
}

func (mpp *MockPriceProvider) ID() string {
return "MockPriceProvider"
}

func (mpp *MockPriceProvider) FetchPrices(symbols []string, currencies []string) (map[string]map[string]float64, error) {
res := make(map[string]map[string]float64)
for _, symbol := range symbols {
res[symbol] = make(map[string]float64)
for _, currency := range currencies {
res[symbol][currency] = mpp.mockPrices[symbol][currency]
}
}
return res, nil
}

type MockPriceProviderWithError struct {
MockPriceProvider
}

func (mpp *MockPriceProviderWithError) FetchPrices(symbols []string, currencies []string) (map[string]map[string]float64, error) {
return nil, errors.New("error")
}

func setupTestPrice(t *testing.T, providers []thirdparty.MarketDataProvider) *Manager {
return NewManager(providers, &event.Feed{})
return NewManager(providers, &event.Feed{}, nil)
}

var mockPrices = map[string]map[string]float64{
Expand All @@ -80,8 +43,8 @@ var mockPrices = map[string]map[string]float64{
func TestPrice(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
priceProvider := NewMockPriceProvider(ctrl)
priceProvider.setMockPrices(mockPrices)
priceProvider := mock_market.NewMockPriceProvider(ctrl)
priceProvider.SetMockPrices(mockPrices)

manager := setupTestPrice(t, []thirdparty.MarketDataProvider{priceProvider, priceProvider})

Expand Down Expand Up @@ -125,9 +88,9 @@ func TestPrice(t *testing.T) {
func TestFetchPriceErrorFirstProvider(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
priceProvider := NewMockPriceProvider(ctrl)
priceProvider.setMockPrices(mockPrices)
priceProviderWithError := &MockPriceProviderWithError{}
priceProvider := mock_market.NewMockPriceProvider(ctrl)
priceProvider.SetMockPrices(mockPrices)
priceProviderWithError := &mock_market.MockPriceProviderWithError{}
symbols := []string{"BTC", "ETH"}
currencies := []string{"USD", "EUR"}

Expand Down
46 changes: 46 additions & 0 deletions services/wallet/market/mock/mock_price_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mock_market

import (
"errors"

"github.com/golang/mock/gomock"
mock_thirdparty "github.com/status-im/status-go/services/wallet/thirdparty/mock"
)

type MockPriceProvider struct {
mock_thirdparty.MockMarketDataProvider
mockPrices map[string]map[string]float64
}

func NewMockPriceProvider(ctrl *gomock.Controller) *MockPriceProvider {
return &MockPriceProvider{
MockMarketDataProvider: *mock_thirdparty.NewMockMarketDataProvider(ctrl),
}
}

func (mpp *MockPriceProvider) SetMockPrices(prices map[string]map[string]float64) {
mpp.mockPrices = prices
}

func (mpp *MockPriceProvider) ID() string {
return "MockPriceProvider"
}

func (mpp *MockPriceProvider) FetchPrices(symbols []string, currencies []string) (map[string]map[string]float64, error) {
res := make(map[string]map[string]float64)
for _, symbol := range symbols {
res[symbol] = make(map[string]float64)
for _, currency := range currencies {
res[symbol][currency] = mpp.mockPrices[symbol][currency]
}
}
return res, nil
}

type MockPriceProviderWithError struct {
MockPriceProvider
}

func (mpp *MockPriceProviderWithError) FetchPrices(symbols []string, currencies []string) (map[string]map[string]float64, error) {
return nil, errors.New("error")
}
2 changes: 1 addition & 1 deletion services/wallet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewService(
return
}

if !onlineChecker.Online() {
if onlineChecker != nil && !onlineChecker.Online() {
return
}
feed.Send(walletevent.Event{
Expand Down

0 comments on commit 0de4099

Please sign in to comment.