[Improvement]: Change internal implementation of ballerina function mocking to use function pointers #42281
Labels
Area/TestFramework
IceBox
Reason/Other
None of the other reasons.
Team/DevTools
Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs )
Type/Improvement
Description
As of now function mocking in ballerina tests are done by dynamically loading the class of the mocked function and modifying the bytecode of the mocked function body using ASM. It is possible to do this more efficiently using function pointers.
Describe your problem(s)
Due to the usage of reflection to dynamically load the class for mocked function modification, the current implementation has a drawback when it comes to running tests through a GraalVM native image. Since GraalVM is an ahead of time compilation, we have to generate multiple JARs for each module with function mocking. This is a time consuming operation and the image generation time could multiply based on the number of modules with mocked functions.
Describe your solution(s)
It is possible to facilitate function mocking using function pointers instead of dynamic bytecode modification. With this implementation, GraalVM tests can be done using just one JAR. See the following example,
Before desugar phase
Imagine the following function is being mocked,
The test suit will be as follows,
After desugar phase
Mocking can be done by modifying the
desugar phase
in the ballerina compiler to desugar the mocked function(foo
) to the following,We can modify the
testablePkg
as well inside thedesugar phase
,In the above code snippet we change the behavior of the
foo
function to mocked behavior in thefp = mock_foo;
line. And restore it back to the original behavior in thefp = original_foo;
line. This combination can be used for more complex mocking instances as well.Related area
-> Test Framework
Related issue(s) (optional)
No response
Suggested label(s) (optional)
No response
Suggested assignee(s) (optional)
No response
The text was updated successfully, but these errors were encountered: