@@ -33,6 +33,7 @@ def set_fail(self, e, batch):
3333 def setUp (self ):
3434 self .failed = False
3535 self .client = Client (FAKE_TEST_API_KEY , on_error = self .set_fail )
36+ Client ._enforce_singleton = False # Disable singleton for tests
3637
3738 def test_requires_api_key (self ):
3839 self .assertRaises (AssertionError , Client )
@@ -159,6 +160,7 @@ def test_basic_capture_exception_with_correct_host_generation(self):
159160
160161 with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
161162 client = Client (FAKE_TEST_API_KEY , on_error = self .set_fail , host = "https://aloha.com" )
163+ print (client .host )
162164 exception = Exception ("test exception" )
163165 client .capture_exception (exception , "distinct_id" )
164166
@@ -187,6 +189,7 @@ def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(s
187189
188190 with mock .patch .object (Client , "capture" , return_value = None ) as patch_capture :
189191 client = Client (FAKE_TEST_API_KEY , on_error = self .set_fail , host = "https://app.posthog.com" )
192+ print (client .host )
190193 exception = Exception ("test exception" )
191194 client .capture_exception (exception , "distinct_id" )
192195
@@ -1073,3 +1076,58 @@ def test_default_properties_get_added_properly(self, patch_decide):
10731076 group_properties = {},
10741077 disable_geoip = False ,
10751078 )
1079+
1080+ def test_singleton_behavior (self ):
1081+ # Reset singleton state
1082+ Client ._instance = None
1083+ Client ._enforce_singleton = True
1084+
1085+ # Create first instance
1086+ client1 = Client (FAKE_TEST_API_KEY , host = "https://host1.com" )
1087+
1088+ # Create second instance with different params
1089+ client2 = Client (FAKE_TEST_API_KEY , host = "https://host2.com" )
1090+
1091+ # Both should reference the same instance
1092+ self .assertIs (client1 , client2 )
1093+
1094+ # Host should be from first initialization
1095+ self .assertEqual (client1 .host , "https://host1.com" )
1096+ self .assertEqual (client2 .host , "https://host1.com" )
1097+
1098+ def test_singleton_disabled_for_testing (self ):
1099+ # Reset singleton state
1100+ Client ._instance = None
1101+ Client ._enforce_singleton = False
1102+
1103+ # Create instances with different params
1104+ client1 = Client (FAKE_TEST_API_KEY , host = "https://host1.com" )
1105+ client2 = Client (FAKE_TEST_API_KEY , host = "https://host2.com" )
1106+
1107+ # Should be different instances
1108+ self .assertIsNot (client1 , client2 )
1109+
1110+ # Each should maintain their own host
1111+ self .assertEqual (client1 .host , "https://host1.com" )
1112+ self .assertEqual (client2 .host , "https://host2.com" )
1113+
1114+ def test_singleton_warning_on_multiple_initialization (self ):
1115+ # Reset singleton state
1116+ Client ._instance = None
1117+ Client ._enforce_singleton = True
1118+
1119+ # Create first instance
1120+ client1 = Client (FAKE_TEST_API_KEY )
1121+
1122+ # Second initialization should log warning
1123+ with self .assertLogs ("posthog" , level = "WARNING" ) as logs :
1124+ client2 = Client (FAKE_TEST_API_KEY )
1125+ self .assertEqual (
1126+ logs .output [0 ],
1127+ "WARNING:posthog:Warning: Attempting to create multiple PostHog client instances. "
1128+ "PostHog client should be used as a singleton. "
1129+ "The existing instance will be reused instead of creating a new one. "
1130+ "Consider using PostHog.get_instance() to access the client."
1131+ )
1132+
1133+
0 commit comments