diff --git a/.circleci/config_continue.yml b/.circleci/config_continue.yml index 004004ce1..25ae526f7 100644 --- a/.circleci/config_continue.yml +++ b/.circleci/config_continue.yml @@ -79,6 +79,22 @@ jobs: - run: make with-django2x - run: (cd .circleci/ && ./websiteDjango2x.sh) - slack/status + test-website-flask-nest-asyncio: + docker: + - image: rishabhpoddar/supertokens_python_driver_testing + resource_class: large + environment: + SUPERTOKENS_NEST_ASYNCIO: "1" + steps: + - checkout + - run: update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk-15.0.1/bin/java" 2 + - run: update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk-15.0.1/bin/javac" 2 + - run: git config --global url."https://github.com/".insteadOf ssh://git@github.com/ + - run: echo "127.0.0.1 localhost.org" >> /etc/hosts + - run: make with-flask + - run: python -m pip install nest-asyncio + - run: (cd .circleci/ && ./websiteFlask.sh) + - slack/status test-authreact-fastapi: docker: - image: rishabhpoddar/supertokens_python_driver_testing diff --git a/supertokens_python/async_to_sync_wrapper.py b/supertokens_python/async_to_sync_wrapper.py index 4fd22b49e..8e019336d 100644 --- a/supertokens_python/async_to_sync_wrapper.py +++ b/supertokens_python/async_to_sync_wrapper.py @@ -19,13 +19,8 @@ _T = TypeVar("_T") -def is_nest_asyncio_enabled(): - try: - import nest_asyncio as _ # type: ignore - - return getenv("SUPERTOKENS_NEST_ASYNCIO", "") == "1" - except Exception: - return False +def nest_asyncio_enabled(): + return getenv("SUPERTOKENS_NEST_ASYNCIO", "") == "1" def create_or_get_event_loop() -> asyncio.AbstractEventLoop: @@ -35,7 +30,7 @@ def create_or_get_event_loop() -> asyncio.AbstractEventLoop: if "There is no current event loop in thread" in str(ex): loop = asyncio.new_event_loop() - if is_nest_asyncio_enabled(): + if nest_asyncio_enabled(): import nest_asyncio # type: ignore nest_asyncio.apply(loop) # type: ignore diff --git a/tests/test_config.py b/tests/test_config.py index 2df57fe59..4d0a90ef7 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -11,6 +11,8 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import asyncio + from pytest import mark from unittest.mock import MagicMock from supertokens_python import InputAppInfo, SupertokensConfig, init @@ -800,3 +802,26 @@ async def test_cookie_samesite_with_ec2_public_url(): assert SessionRecipe.get_instance().config.cookie_domain is None assert SessionRecipe.get_instance().config.cookie_same_site == "lax" assert SessionRecipe.get_instance().config.cookie_secure is False + + +def test_nest_asyncio_import(): + from supertokens_python.async_to_sync_wrapper import nest_asyncio_enabled, sync + from os import getenv + + circleci = getenv("CIRCLECI", "false") == "true" + + if not circleci: + return + + # Has to be circleci + if nest_asyncio_enabled(): + # nest-asyncio should be installed + sync(asyncio.sleep(0.1)) + else: + # nest-asyncio shouldn't be installed and sync() should throw error + try: + sync(asyncio.sleep(0.1)) + assert False, "Shouldn't come here" + except ModuleNotFoundError: + # should be missing + assert True