Skip to content

Commit 1c3dd7f

Browse files
committed
Test to ensure slow imports do not reappear.
1 parent 9779c89 commit 1c3dd7f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/unit/util/test_compat.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# This product includes software developed at Datadog (https://www.datadoghq.com/).
44
# Copyright 2015-Present Datadog, Inc
55
import logging
6+
import pytest
7+
import sys
68
import unittest
79

810
from mock import patch
@@ -48,3 +50,35 @@ def test_function():
4850
mock_debug.assert_called_once()
4951
else:
5052
mock_debug.assert_not_called()
53+
54+
@pytest.mark.subprocess()
55+
def test_slow_imports(monkeypatch):
56+
# We should lazy load certain modules to avoid slowing down the startup
57+
# time when running in a serverless environment. This test will fail if
58+
# any of those modules are imported during the import of datadogpy.
59+
60+
blocklist = [
61+
'configparser',
62+
'email.mime.application',
63+
'email.mime.multipart',
64+
'importlib.metadata',
65+
'importlib_metadata',
66+
'logging.handlers',
67+
'multiprocessing',
68+
'urllib.request',
69+
]
70+
71+
class BlockListFinder:
72+
def find_spec(self, fullname, *args):
73+
for lib in blocklist:
74+
if fullname == lib:
75+
raise ImportError('module %s was imported!' % fullname)
76+
return None
77+
78+
monkeypatch.setattr('sys.meta_path', [BlockListFinder()] + sys.meta_path)
79+
80+
for mod in sys.modules.copy():
81+
if mod in blocklist or mod.startswith('datadog'):
82+
del sys.modules[mod]
83+
84+
import datadog

0 commit comments

Comments
 (0)