Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extend testing to ensure correct messages are logged.
  • Loading branch information
kinyoklion committed Jun 23, 2025
commit 2a3a6bf88b2df7a9990ab131c683a4b6c62ed447
44 changes: 29 additions & 15 deletions ldclient/testing/test_ldclient_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,21 @@ def test_plugin_error_handling_get_hooks(self):
)

# The hooks cannot be accessed, but the plugin will still get registered.
with LDClient(config=config) as client:
self.assertTrue(normal_plugin.registered)
self.assertTrue(error_plugin.registered)
with patch('ldclient.impl.util.log.error') as mock_log_error:
with LDClient(config=config) as client:
self.assertTrue(normal_plugin.registered)
self.assertTrue(error_plugin.registered)

client.variation("test-flag", Context.builder("user-key").build(), "default")
client.variation("test-flag", Context.builder("user-key").build(), "default")

self.assertTrue(normal_hook.before_called)
self.assertTrue(normal_hook.after_called)
self.assertTrue(normal_hook.before_called)
self.assertTrue(normal_hook.after_called)

# Verify that the error was logged with the correct message
mock_log_error.assert_called_once()
error_call_args = mock_log_error.call_args[0]
self.assertIn("Error getting hooks from plugin Error Plugin", error_call_args[0])
self.assertIn("Get hooks error in Error Plugin", str(error_call_args))

def test_plugin_error_handling_register(self):
"""Test that errors during plugin registration are handled gracefully."""
Expand All @@ -220,17 +227,24 @@ def test_plugin_error_handling_register(self):
)

# Should not raise an exception
with LDClient(config=config) as client:
# Normal plugin should still be registered
self.assertTrue(normal_plugin.registered)

# Error plugin should not be registered
self.assertFalse(error_plugin.registered)
with patch('ldclient.impl.util.log.error') as mock_log_error:
with LDClient(config=config) as client:
# Normal plugin should still be registered
self.assertTrue(normal_plugin.registered)

# Error plugin should not be registered
self.assertFalse(error_plugin.registered)

client.variation("test-flag", Context.builder("user-key").build(), "default")
client.variation("test-flag", Context.builder("user-key").build(), "default")

self.assertTrue(normal_hook.before_called)
self.assertTrue(normal_hook.after_called)
self.assertTrue(normal_hook.before_called)
self.assertTrue(normal_hook.after_called)

# Verify that the error was logged with the correct message
mock_log_error.assert_called_once()
error_call_args = mock_log_error.call_args[0]
self.assertIn("Error registering plugin Error Plugin", error_call_args[0])
self.assertIn("Registration error in Error Plugin", str(error_call_args))

def test_plugin_with_existing_hooks(self):
"""Test that plugin hooks work alongside existing hooks and config hooks are called before plugin hooks."""
Expand Down