Skip to content

Commit

Permalink
Use WindowsSelectorEventLoopPolicy on tests to avoid aiohttp closed l…
Browse files Browse the repository at this point in the history
…oop warning
  • Loading branch information
Guillaume De Saint Martin committed Oct 26, 2020
1 parent 62405da commit b44673d
Show file tree
Hide file tree
Showing 22 changed files with 42 additions and 9 deletions.
18 changes: 16 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import sys
import asyncio
from os import path

import aiohttp
import pytest
import requests

from octobot_commons.asyncio_tools import ErrorContainer
import octobot_commons.asyncio_tools as asyncio_tools
from octobot_commons.tests.test_config import load_test_config
from octobot_tentacles_manager.api.installer import install_all_tentacles
from octobot_tentacles_manager.constants import TENTACLES_PATH
Expand All @@ -31,9 +32,11 @@

@pytest.yield_fixture
def event_loop():
# re-configure async loop each time this fixture is called
_configure_async_test_loop()
loop = asyncio.new_event_loop()
# use ErrorContainer to catch otherwise hidden exceptions occurring in async scheduled tasks
error_container = ErrorContainer()
error_container = asyncio_tools.ErrorContainer()
loop.set_exception_handler(error_container.exception_handler)
yield loop
# will fail if exceptions have been silently raised
Expand Down Expand Up @@ -66,3 +69,14 @@ def _tentacles_local_path():
async with aiohttp.ClientSession() as session:
yield await install_all_tentacles(_tentacles_local_path(), aiohttp_session=session)
_cleanup()


def _configure_async_test_loop():
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
# use WindowsSelectorEventLoopPolicy to avoid aiohttp connexion close warnings
# https://github.com/encode/httpx/issues/914#issuecomment-622586610
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


# set default values for async loop
_configure_async_test_loop()
1 change: 1 addition & 0 deletions tests/api/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# License along with this library.
import pytest

from tests import event_loop
from octobot_trading.api.channels import subscribe_to_ohlcv_channel, subscribe_to_trades_channel, \
subscribe_to_order_channel

Expand Down
1 change: 1 addition & 0 deletions tests/api/test_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
get_exchange_configurations_from_exchange_name, get_exchange_manager_from_exchange_name_and_id
from octobot_trading.exchanges.exchanges import Exchanges
from tests.exchanges import exchange_manager
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
Expand Down
1 change: 1 addition & 0 deletions tests/exchange_data/funding/test_funding_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from octobot_trading.exchange_data.funding.funding_manager import FundingManager
from tests.util.random_numbers import random_timestamp, random_funding_rate
from tests import event_loop

pytestmark = pytest.mark.asyncio

Expand Down
1 change: 1 addition & 0 deletions tests/exchange_data/kline/test_kline_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from octobot_trading.exchange_data.kline.kline_manager import KlineManager
from tests.util.random_numbers import random_kline, random_price, random_timestamp, random_quantity
from tests import event_loop

pytestmark = pytest.mark.asyncio

Expand Down
1 change: 1 addition & 0 deletions tests/exchange_data/ohlcv/test_candles_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from octobot_trading.exchange_data.kline.kline_manager import KlineManager
from octobot_trading.exchange_data.exchange_symbol_data import ExchangeSymbolData
from octobot_trading.exchanges.exchange_manager import ExchangeManager
from tests import event_loop


# All test coroutines will be treated as marked.
Expand Down
1 change: 1 addition & 0 deletions tests/exchange_data/order_book/test_order_book_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from octobot_trading.enums import TradeOrderSide
from tests.util.random_numbers import random_price_list, random_price, random_quantity, random_order_book_side
from tests.util.random_numbers import random_timestamp
from tests import event_loop

pytestmark = pytest.mark.asyncio

Expand Down
1 change: 1 addition & 0 deletions tests/exchange_data/ticker/test_ticker_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from octobot_trading.exchange_data.ticker.ticker_manager import TickerManager
from octobot_trading.enums import ExchangeConstantsMiniTickerColumns, ExchangeConstantsTickersColumns
from tests.util.random_numbers import random_price, random_quantity, random_timestamp
from tests import event_loop

pytestmark = pytest.mark.asyncio

Expand Down
1 change: 1 addition & 0 deletions tests/exchanges/data/test_exchange_config_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# License along with this library.
import pytest

from tests import event_loop
from octobot_commons.tests.test_config import load_test_config
from octobot_commons.constants import CONFIG_CRYPTO_CURRENCIES
from octobot_trading.exchanges.exchange_manager import ExchangeManager
Expand Down
5 changes: 2 additions & 3 deletions tests/exchanges/test_exchange_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

from octobot_commons.tests.test_config import load_test_config
from octobot_trading.exchanges.exchange_manager import ExchangeManager

from octobot_trading.api.exchange import cancel_ccxt_throttle_task

# All test coroutines will be treated as marked.
from octobot_trading.exchanges.types.spot_exchange import SpotExchange
from octobot_trading.exchanges.implementations.spot_exchange_simulator import SpotExchangeSimulator
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio


Expand Down
1 change: 1 addition & 0 deletions tests/exchanges/test_exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from octobot_trading.exchanges.exchange_manager import ExchangeManager
from octobot_trading.exchanges.exchanges import Exchanges
from octobot_trading.api.exchange import cancel_ccxt_throttle_task
from tests import event_loop

pytestmark = pytest.mark.asyncio

Expand Down
3 changes: 3 additions & 0 deletions tests/exchanges/traders/test_trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import time
from mock import AsyncMock, patch

from tests import event_loop
from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_commons.constants import CONFIG_ENABLED_OPTION, PORTFOLIO_AVAILABLE, PORTFOLIO_TOTAL
from octobot_commons.tests.test_config import load_test_config
Expand Down Expand Up @@ -676,6 +677,8 @@ async def test_sell_all_currencies(self):
assert sell_USDT_order.symbol == "BTC/USDT"
assert sell_USDT_order.order_type == TraderOrderType.BUY_MARKET
assert round(sell_USDT_order.origin_quantity, 8) == round(1000 / sell_USDT_order.origin_price, 8)
# let market orders get filled before stopping exchange
await wait_asyncio_next_cycle()

await self.stop(exchange_manager)

Expand Down
1 change: 1 addition & 0 deletions tests/modes/test_abstract_mode_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from octobot_trading.exchanges.exchange_manager import ExchangeManager
from octobot_trading.modes import AbstractTradingMode
from octobot_trading.exchanges.traders.trader_simulator import TraderSimulator
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
Expand Down
1 change: 1 addition & 0 deletions tests/personal_data/orders/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from octobot_trading.enums import TradeOrderSide, OrderStatus, TraderOrderType
from octobot_trading.personal_data.orders import Order

from tests import event_loop
from tests.exchanges import exchange_manager
from tests.exchanges.traders import trader_simulator
from tests.exchanges.traders import trader
Expand Down
1 change: 1 addition & 0 deletions tests/personal_data/orders/test_order_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from octobot_trading.enums import ExchangeConstantsMarketStatusColumns as Ecmsc
from octobot_trading.personal_data.orders import check_and_adapt_order_details_if_necessary, split_orders, \
adapt_quantity, trunc_with_n_decimal_digits, add_dusts_to_quantity_if_necessary, adapt_price
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
Expand Down
1 change: 1 addition & 0 deletions tests/personal_data/orders/test_order_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import pytest
from octobot_commons.tests.test_config import load_test_config

from tests import event_loop
from octobot_trading.personal_data.orders import Order, parse_order_type
from octobot_trading.enums import TradeOrderSide, TradeOrderType, TraderOrderType
from octobot_trading.exchanges.exchange_manager import ExchangeManager
Expand Down
1 change: 1 addition & 0 deletions tests/personal_data/portfolios/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from tests.test_utils.order_util import fill_market_order, fill_limit_or_stop_order

from tests.exchanges import backtesting_trader, backtesting_config, backtesting_exchange_manager, fake_backtesting
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
Expand Down
1 change: 1 addition & 0 deletions tests/personal_data/portfolios/test_portfolio_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from octobot_trading.personal_data.orders import BuyMarketOrder, BuyLimitOrder

from tests.exchanges import backtesting_trader, backtesting_config, backtesting_exchange_manager, fake_backtesting
from tests import event_loop

# All test coroutines will be treated as marked.
from tests.util.random_numbers import random_price, random_quantity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
# License along with this library.
import pytest

from tests.personal_data.portfolios import update_portfolio_balance
from tests.util.random_numbers import random_quantity

from tests.exchanges import backtesting_trader, backtesting_config, backtesting_exchange_manager, fake_backtesting
from tests.personal_data.portfolios import update_portfolio_balance
from tests.exchanges import backtesting_trader, backtesting_config, backtesting_exchange_manager, fake_backtesting
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tests.util.random_numbers import random_quantity, random_price

from tests.exchanges import backtesting_trader, backtesting_config, backtesting_exchange_manager, fake_backtesting
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
Expand Down
3 changes: 2 additions & 1 deletion tests/personal_data/trades/__test_trades_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

from octobot_trading.exchanges import ExchangeManager
from config import *
from tests.test_utils.config import load_test_config
from trading.trader.trader_simulator import TraderSimulator
from trading.trader.trade import Trade
from trading.trader.order import SellLimitOrder
from tests.test_utils.config import load_test_config
from tests import event_loop

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio
Expand Down
1 change: 1 addition & 0 deletions tests/personal_data/trades/test_trade_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import pytest

from tests import event_loop
from octobot_commons.tests.test_config import load_test_config
from octobot_trading.enums import TraderOrderType, OrderStatus
from octobot_trading.exchanges.exchange_manager import ExchangeManager
Expand Down

0 comments on commit b44673d

Please sign in to comment.