Fixed Bean and Encryption Issues Causing Order-Dependent Flakiness in Multiple Tests #1466
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1465
Motivation
The following (victim) tests exhibit order-dependent behavior and fail when
EncryptorConfigTest.testEncryptionWithoutEncryptorImplementation(the polluter) runs before them, unless intermediate tests clean the shared static state:When
EncryptorConfigTest.testEncryptionWithoutEncryptorImplementationruns first it removes the encryption bean from the application context, leavesEncryptionProvider.encryptorin a polluted state, and may leaveSlangEntitiesSpringConfig.applicationContextpointing to the wrong context. The victim tests attempt to createSensitiveValueinstances which require encryption but they fail because they either use a cached encryptor with the wrong context, the encryption bean cannot be resolved because the application context is misconfigured, or "Application context bean is missing" errors are triggered.Additionally,
DeserializeTest.testDeserializeInputfails unlessSensitiveValueTest.testEncryptedStringSensitiveValuePreEncrypted(or another test that properly initializes the state) is run beforehand. This is because the test requires theSlangEntitiesSpringConfig.applicationContextto be set to a context containing a DummyEncryptor bean and theEncryptionProvider.encryptorcache to be cleared or properly initialized.Order dependent flaky tests can lead to unreliable results from CI and can erode developer trust in the test suite.
Modifications
A new abstract base class
SpringEncryptionTestBasewas introduced to ensure static state is properly initialized before each test method. It clears theEncryptionProvider.encryptorcache using reflection to reset the staticAtomicReferenceand injects the test'sApplicationContextintoSlangEntitiesSpringConfig.applicationContextto ensure the correct Spring context is used. Each of the affected test classes (ValueFactoryTest, SensitiveValueTest, and DeserializeTest) now extend this new abstract base class.Additionally, a
DummyEncryptorbean configuration was added toDeserializeTestto ensure it has the required encryption implementation in its Spring context.Affected Tests
This change affects the existing tests:
Tools Used
iDFlakies was utilized to identify the order dependent polluter(s), victim(s), and cleaner(s) in the test suite(s). The tool functions by repeatedly running a project's JUnit tests under many different deterministic test orders to detect flaky tests which pass and fail inconsistently, and then re-executes the failing orders to classify each flaky test as either order-dependent or non-order-dependent. iFixFlakies was executed on the output to further diagnose the source of the problems and additionally generate test helper patches from existing cleaners that were found. With the combination of these tools, I was able to more easily diagnose the source(s) of the problem(s) and create effective solution(s) for them.