|
| 1 | +## Developer Plugin |
| 2 | + |
| 3 | +> [!WARNING] The plugin is NOT intended to be used in production environments. It's designed for the purpose of testing. |
| 4 | +
|
| 5 | +The Developer Plugin allows developers to simulate failures by providing a mechanism to raise exceptions when connection operations occur. By simulating these failures, developers can verify that their application handles these scenarios correctly. |
| 6 | + |
| 7 | +Since some exceptions raised by the drivers rarely happen, testing for those might be difficult and require a lot of effort in building a testing environment. Exceptions associated with network outages are a good example of those exceptions. It may require substantial efforts to design and build a testing environment where such timeout exceptions could be produced with 100% accuracy and 100% guarantee. If a test suite can't produce and verify such cases with 100% accuracy it significantly decreases the value of such tests and makes the tests unstable and flaky. The Developer Plugin simplifies testing of such scenarios as shown below. |
| 8 | + |
| 9 | +The `dev` plugin code should be added to the connection plugins parameter in order to be able to intercept Python calls and raise a test exception when conditions are met. |
| 10 | + |
| 11 | +### Simulate an exception while opening a new connection |
| 12 | + |
| 13 | +The plugin introduces a new class `ExceptionSimulatorManager` that will handle how a given exception will be passed to the connection to be tested. |
| 14 | + |
| 15 | +In order to raise a test exception while opening a new connection, first create an instance of the exception to be tested, then use `raise_exception_on_next_connect` in `ExceptionSimulatorManager` so it will be triggered at next connection attempt. |
| 16 | + |
| 17 | +Once the exception is raised, it will be cleared and will not be raised again. This means that the next opened connection will not raise the exception again. |
| 18 | + |
| 19 | +```python |
| 20 | +import psycopg |
| 21 | +from aws_advanced_python_wrapper import AwsWrapperConnection |
| 22 | +from aws_advanced_python_wrapper.pep249 import Error |
| 23 | + |
| 24 | +params = { |
| 25 | + "plugins": "dev", |
| 26 | + "dialect": "aurora-pg" |
| 27 | +} |
| 28 | +exception: Error = Error("test") |
| 29 | +ExceptionSimulatorManager.raise_exception_on_next_connect(exception) |
| 30 | + |
| 31 | +AwsWrapperConnection.connect(psycopg.Connection.connect, **params) # this throws the exception |
| 32 | +AwsWrapperConnection.connect(psycopg.Connection.connect, **params) # goes as usual with no exception |
| 33 | +``` |
| 34 | + |
| 35 | +### Simulate an exception with already opened connection |
| 36 | + |
| 37 | +It is possible to also simulate an exception thrown in a connection after the connection has been opened. |
| 38 | + |
| 39 | +Similar to the previous case, the exception is cleared up once it's raised and subsequent Python calls should behave normally. |
| 40 | + |
| 41 | +```python |
| 42 | +import psycopg |
| 43 | +from aws_advanced_python_wrapper import AwsWrapperConnection |
| 44 | + |
| 45 | +params = { |
| 46 | + "plugins": "dev", |
| 47 | + "dialect": "aurora-pg" |
| 48 | +} |
| 49 | +exception: RuntimeError = RuntimeError("test") |
| 50 | +ExceptionSimulatorManager.raise_exception_on_next_method("Connection.cursor", exception) |
| 51 | +conn = AwsWrapperConnection.connect(psycopg.Connection.connect, **params) |
| 52 | +conn.cursor() # this throws the exception |
| 53 | +conn.cursor() # goes as usual with no exception |
| 54 | +``` |
| 55 | + |
| 56 | +It's possible to use callback functions to check Python call parameters and decide whether to return an exception or not. Check `ExceptionSimulatorManager.set_connect_callback` and `ExceptionSimulatorManager.set_method_callback` for more details. |
0 commit comments