Skip to content

Commit 9b7e496

Browse files
committed
sqs_client
1 parent ad9b8ed commit 9b7e496

File tree

4 files changed

+28
-62
lines changed

4 files changed

+28
-62
lines changed

lambdas/delta_backend/src/delta.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from boto3.dynamodb.conditions import Attr
99
from botocore.exceptions import ClientError
1010

11-
from common.clients import STREAM_NAME, logger
11+
from common.clients import STREAM_NAME, get_sqs_client, logger
1212
from common.log_firehose import send_log_to_firehose
1313
from converter import Converter
1414
from mappings import ActionFlag, EventName, Operation
@@ -38,24 +38,6 @@ def get_delta_table():
3838
return delta_table
3939

4040

41-
sqs_client = None
42-
43-
44-
def get_sqs_client():
45-
"""
46-
Initialize the SQS client with exception handling.
47-
"""
48-
global sqs_client
49-
if not sqs_client:
50-
try:
51-
logger.info("Initializing SQS Client")
52-
sqs_client = boto3.client("sqs", region_name)
53-
except Exception:
54-
logger.exception("Error initializing SQS Client")
55-
sqs_client = None
56-
return sqs_client
57-
58-
5941
def send_message(record, queue_url=failure_queue_url):
6042
# Create a message
6143
message_body = record

lambdas/delta_backend/tests/test_delta.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def setUp(self):
4545
self.send_log_to_firehose_patcher = patch("delta.send_log_to_firehose")
4646
self.mock_send_log_to_firehose = self.send_log_to_firehose_patcher.start()
4747

48-
self.sqs_client_patcher = patch("delta.sqs_client")
48+
self.sqs_client_patcher = patch("common.clients.global_sqs_client")
4949
self.mock_sqs_client = self.sqs_client_patcher.start()
5050

5151
self.delta_table_patcher = patch("delta.delta_table")
@@ -660,48 +660,6 @@ def test_returns_none_on_exception(self, mock_boto3_resource):
660660
self.mock_logger_error.assert_called()
661661

662662

663-
class TestGetSqsClient(unittest.TestCase):
664-
def setUp(self):
665-
# Patch logger.info and logger.error
666-
self.logger_info_patcher = patch("logging.Logger.info")
667-
self.mock_logger_info = self.logger_info_patcher.start()
668-
self.logger_error_patcher = patch("logging.Logger.error")
669-
self.mock_logger_error = self.logger_error_patcher.start()
670-
self.sqs_client_patcher = patch("delta.boto3.client")
671-
self.mock_sqs_client = self.sqs_client_patcher.start()
672-
# Reset the global sqs_client before each test
673-
delta.sqs_client = None
674-
675-
def tearDown(self):
676-
self.logger_info_patcher.stop()
677-
self.logger_error_patcher.stop()
678-
self.sqs_client_patcher.stop()
679-
680-
def test_returns_client_on_success(self):
681-
mock_client = MagicMock()
682-
self.mock_sqs_client.return_value = mock_client
683-
684-
client = delta.get_sqs_client()
685-
self.assertIs(client, mock_client)
686-
# Should cache the client
687-
self.assertIs(delta.sqs_client, mock_client)
688-
689-
def test_returns_cached_client(self):
690-
mock_client = MagicMock()
691-
delta.sqs_client = mock_client
692-
693-
client = delta.get_sqs_client()
694-
self.assertIs(client, mock_client)
695-
# Should not re-initialize
696-
self.mock_sqs_client.assert_not_called()
697-
698-
def test_returns_none_on_exception(self):
699-
self.mock_sqs_client.side_effect = Exception("fail")
700-
client = delta.get_sqs_client()
701-
self.assertIsNone(client)
702-
self.mock_logger_error.assert_called()
703-
704-
705663
class TestSendMessage(unittest.TestCase):
706664
def setUp(self):
707665
self.get_sqs_client_patcher = patch("delta.get_sqs_client")

lambdas/shared/src/common/clients.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ def get_s3_client():
2525
global_s3_client = boto3_client("s3", region_name=REGION_NAME)
2626
return global_s3_client
2727

28+
sqs_client = boto3_client("sqs", region_name=REGION_NAME)
29+
30+
# for lambdas which require a global sqs_client
31+
global_sqs_client = None
32+
33+
34+
def get_sqs_client():
35+
global global_sqs_client
36+
if global_sqs_client is None:
37+
global_sqs_client = boto3_client("sqs", region_name=REGION_NAME)
38+
return global_sqs_client
39+
2840

2941
firehose_client = boto3_client("firehose", region_name=REGION_NAME)
3042
secrets_manager_client = boto3_client("secretsmanager", region_name=REGION_NAME)

lambdas/shared/tests/test_common/test_clients.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,17 @@ def test_global_s3_client_initialization(self):
7474
call_count = self.mock_boto3_client.call_count
7575
clients.get_s3_client()
7676
self.assertEqual(self.mock_boto3_client.call_count, call_count)
77+
78+
def test_global_sqs_client(self):
79+
"""Test global_sqs_client is not initialized on import"""
80+
importlib.reload(clients)
81+
self.assertEqual(clients.global_sqs_client, None)
82+
83+
def test_global_sqs_client_initialization(self):
84+
"""Test global_sqs_client is initialized exactly once even with multiple invocations"""
85+
importlib.reload(clients)
86+
clients.get_sqs_client()
87+
self.assertNotEqual(clients.global_sqs_client, None)
88+
call_count = self.mock_boto3_client.call_count
89+
clients.get_sqs_client()
90+
self.assertEqual(self.mock_boto3_client.call_count, call_count)

0 commit comments

Comments
 (0)