2021-05-04 06:40:57 +02:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2020-03-27 01:31:39 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
from lbry.testcase import AsyncioTestCase
|
2021-05-04 06:40:57 +02:00
|
|
|
from lbry.extras.daemon.exchange_rate_manager import ExchangeRate, ExchangeRateManager, FEEDS, MarketFeed
|
2020-03-27 01:31:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestExchangeRateManager(AsyncioTestCase):
|
|
|
|
async def test_exchange_rate_manager(self):
|
|
|
|
manager = ExchangeRateManager(FEEDS)
|
|
|
|
manager.start()
|
|
|
|
self.addCleanup(manager.stop)
|
|
|
|
for feed in manager.market_feeds:
|
|
|
|
self.assertFalse(feed.is_online)
|
|
|
|
self.assertIsNone(feed.rate)
|
|
|
|
await manager.wait()
|
2021-05-04 06:40:57 +02:00
|
|
|
failures = set()
|
2020-03-27 01:31:39 +01:00
|
|
|
for feed in manager.market_feeds:
|
2021-05-04 06:40:57 +02:00
|
|
|
if feed.is_online:
|
|
|
|
self.assertIsInstance(feed.rate, ExchangeRate)
|
|
|
|
else:
|
|
|
|
failures.add(feed.name)
|
|
|
|
self.assertFalse(feed.has_rate)
|
|
|
|
self.assertLessEqual(len(failures), 1, f"feed failures: {failures}. Please check exchange rate feeds!")
|
2021-02-10 19:29:05 +01:00
|
|
|
lbc = manager.convert_currency('USD', 'LBC', Decimal('1.0'))
|
|
|
|
self.assertGreaterEqual(lbc, 2.0)
|
2021-06-02 16:14:17 +02:00
|
|
|
self.assertLessEqual(lbc, 15.0)
|
2021-02-10 19:29:05 +01:00
|
|
|
lbc = manager.convert_currency('BTC', 'LBC', Decimal('0.01'))
|
|
|
|
self.assertGreaterEqual(lbc, 1_000)
|
|
|
|
self.assertLessEqual(lbc, 4_000)
|
2021-05-04 06:40:57 +02:00
|
|
|
|
|
|
|
async def test_it_handles_feed_being_offline(self):
|
|
|
|
class FakeFeed(MarketFeed):
|
|
|
|
name = "fake"
|
|
|
|
url = "http://impossi.bru"
|
|
|
|
manager = ExchangeRateManager((FakeFeed,))
|
|
|
|
manager.start()
|
|
|
|
self.addCleanup(manager.stop)
|
|
|
|
for feed in manager.market_feeds:
|
|
|
|
self.assertFalse(feed.is_online)
|
|
|
|
self.assertIsNone(feed.rate)
|
|
|
|
await asyncio.wait_for(manager.wait(), 2)
|
|
|
|
for feed in manager.market_feeds:
|
|
|
|
self.assertFalse(feed.is_online)
|
|
|
|
self.assertFalse(feed.has_rate)
|