Skip to content

Commit a2d8793

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

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+
def test_slow_imports(monkeypatch):
55+
# We should lazy load certain modules to avoid slowing down the startup
56+
# time when running in a serverless environment. This test will fail if
57+
# any of those modules are imported during the import of datadogpy.
58+
59+
blocklist = [
60+
'configparser',
61+
'email.mime.application',
62+
'email.mime.multipart',
63+
'importlib.metadata',
64+
'importlib_metadata',
65+
'logging.handlers',
66+
'multiprocessing',
67+
'urllib.request',
68+
]
69+
70+
class BlockListFinder:
71+
def find_spec(self, fullname, *args):
72+
for lib in blocklist:
73+
if fullname == lib:
74+
raise ImportError('module %s was imported!' % fullname)
75+
return None
76+
find_module = find_spec # Python 2
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)