Fix intermittent test failures through shared Ballerina environment#1892
Conversation
…llerina environment All ~890 tests run in a single JVM. Each test was calling ProjectLoader.load() with a fresh default environment, forcing repeated resolution of ballerina/http and its transitive deps from the distribution repo on every compilation. Under CI load this caused sporadic I/O failures that silently aborted OAS generation. Fix: create one shared Environment (backed by ballerina.home) in TestUtils as a static field and pass it to every OASContractGenerator via setEnvironmentBuilder(). OASContractGenerator now calls ProjectLoader.load(path, environmentBuilder) when a builder is provided, so the package repository is populated once and reused across the entire test suite.
📝 WalkthroughWalkthrough
ChangesProjectEnvironmentBuilder injection and test wiring
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/TestUtils.java`:
- Around line 49-52: The method in TestUtils.java currently returns null when
ballerinaHome is null or blank, which causes OASContractGenerator to fall back
to default loading and reintroduces test flakiness. Instead of returning null,
either throw an explicit exception to fail fast when ballerinaHome is not
properly configured, or construct and return a non-null shared builder to ensure
the environment is properly set up. This ensures test infrastructure
misconfiguration is caught immediately rather than silently allowing fallback
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d5459b96-a65f-4e76-a428-4849abe8f4c9
📒 Files selected for processing (2)
openapi-cli/src/main/java/io/ballerina/openapi/cmd/OASContractGenerator.javaopenapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/TestUtils.java
Returning null silently fell back to the non-shared ProjectLoader default, reintroducing the flakiness the fix was meant to prevent. Use ProjectEnvironmentBuilder.getDefaultBuilder() directly — it reads ballerina.home internally and throws IllegalStateException if the property is not set, so misconfiguration surfaces immediately at test class initialisation.
|



Purpose
Fixes: ballerina-platform/ballerina-library#8825
This pull request improves test stability in the openapi-tools test suite by addressing intermittent failures caused by resource contention during test execution.
Problem
The test suite (approximately 890 tests in a single JVM) was experiencing sporadic failures where OpenAPI specification generation would silently fail. Root cause analysis revealed that each test was independently resolving and loading the
ballerina/httppackage from the distribution repository, creating I/O contention and intermittent failures under continuous integration load.Solution
Implemented an environment sharing strategy across the test suite:
OASContractGenerator: Added a
setEnvironmentBuilder()method that allows tests to inject a sharedProjectEnvironmentBuilder. When provided, the generator uses the shared environment during project loading; otherwise it falls back to the default behavior.TestUtils: Creates a single shared
ProjectEnvironmentBuilderinstance (initialized from theballerina.homesystem property) at class load time and configures allOASContractGeneratorinstances used in tests to utilize this shared environment.Result
The package repository is now populated once and reused throughout the entire test suite execution, eliminating redundant dependency resolution and the associated I/O contention. This delivers more stable and reliable test execution with fewer intermittent failures.