-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #21071
- Loading branch information
Showing
7 changed files
with
198 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters