Skip to content

Fix intermittent test failures through shared Ballerina environment#1892

Merged
TharmiganK merged 2 commits into
masterfrom
test-fix
Jun 17, 2026
Merged

Fix intermittent test failures through shared Ballerina environment#1892
TharmiganK merged 2 commits into
masterfrom
test-fix

Conversation

@TharmiganK

@TharmiganK TharmiganK commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

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/http package 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 shared ProjectEnvironmentBuilder. When provided, the generator uses the shared environment during project loading; otherwise it falls back to the default behavior.

  • TestUtils: Creates a single shared ProjectEnvironmentBuilder instance (initialized from the ballerina.home system property) at class load time and configures all OASContractGenerator instances 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.

…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.
@TharmiganK TharmiganK marked this pull request as ready for review June 17, 2026 10:11
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

OASContractGenerator gains a private ProjectEnvironmentBuilder field and a public setter; generateOAS3DefinitionsAllService conditionally calls ProjectLoader.load(servicePath, environmentBuilder) when the builder is set. TestUtils builds a class-level shared ProjectEnvironmentBuilder from the ballerina.home system property and injects it into OASContractGenerator instances in both comparison helper methods.

Changes

ProjectEnvironmentBuilder injection and test wiring

Layer / File(s) Summary
OASContractGenerator field, setter, and conditional load
openapi-cli/src/main/java/io/ballerina/openapi/cmd/OASContractGenerator.java
Adds ProjectEnvironmentBuilder import, a private field, a public setEnvironmentBuilder setter, and a conditional branch in generateOAS3DefinitionsAllService that calls the overloaded ProjectLoader.load(servicePath, environmentBuilder) when the builder is non-null.
TestUtils shared environment builder and wiring
openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/TestUtils.java
Imports environment builder types, introduces a static SHARED_ENV_BUILDER initialized once from ballerina.home (null when absent), and calls setEnvironmentBuilder on OASContractGenerator in both compareGeneratedFiles and compareWithGeneratedFile overloads.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 Hippity-hop through the build-time maze,
A builder was injected in stylish ways.
ballerina.home points the path just right,
Conditional loads now load with delight.
The rabbit approves — things compile tonight! 🌙

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: fixing intermittent test failures by implementing a shared Ballerina environment strategy across the test suite.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description is comprehensive and well-structured, covering Purpose (with issue link), Problem analysis, Solution implementation details, and expected Result.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test-fix

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@TharmiganK TharmiganK changed the title Fix intermittent 'Yaml was not generated' test failures by sharing Balerina environment Fix intermittent test failures through shared Ballerina environment Jun 17, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between dc1b20a and 53aa3e6.

📒 Files selected for processing (2)
  • openapi-cli/src/main/java/io/ballerina/openapi/cmd/OASContractGenerator.java
  • openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/TestUtils.java

Comment thread openapi-cli/src/test/java/io/ballerina/openapi/generators/openapi/TestUtils.java Outdated
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.
@sonarqubecloud

Copy link
Copy Markdown

@daneshk daneshk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TharmiganK TharmiganK merged commit 24da621 into master Jun 17, 2026
5 checks passed
@TharmiganK TharmiganK deleted the test-fix branch June 17, 2026 11:27
@TharmiganK TharmiganK mentioned this pull request Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] openapi-tools :openapi-cli:test failures: Yaml was not generated

2 participants