Skip to content

Add unit tests to Go harness package (issue #21386)#38979

Open
weibangpeng wants to merge 1 commit into
apache:masterfrom
weibangpeng:go-harness-test-coverage-21386
Open

Add unit tests to Go harness package (issue #21386)#38979
weibangpeng wants to merge 1 commit into
apache:masterfrom
weibangpeng:go-harness-test-coverage-21386

Conversation

@weibangpeng

Copy link
Copy Markdown

Summary

Increases unit test coverage for the Go SDK harness package from 43% → 50.7%, addressing issue #21386.

Changes

Adds ~30 test functions across 7 files (1 new, 6 modified):

  • harness_test.goTestFail, TestControl_MetStoreToString, TestControl_GetPlanOrResponse (5 scenarios)
  • logging_test.goTestConvertSeverity (6 cases)
  • monitoring_test.goTestMonitoring_NilStore, TestGetNextShortID
  • worker_status_test.goTestIsAliveAndShutdown, TestMemoryUsage, TestGoroutineDump, TestBuildInfo, TestActiveProcessBundleStates, TestCacheStats
  • datamgr_test.go — ScopedDataManager cached-port paths, elementsChan lifecycle, terminateStreamOnError, closeInstruction
  • statemgr_test.go — ScopedStateReader construction/close/closed-path guards
  • sampler_test.go (new) — stateSampler lifecycle (new, stop, start via context cancel and done channel)

Test plan

All tests pass:

ok  github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/harness   coverage: 50.7% of statements

Adds ~30 test functions covering previously untested code paths:
- control.metStoreToString, control.getPlanOrResponse (5 scenarios)
- convertSeverity (6 cases), monitoring nil-store path
- stateSampler lifecycle (new, start via context cancel / done channel, stop)
- ScopedDataManager cached-port paths (OpenWrite, OpenElementChan, OpenTimerWrite)
- elementsChan lifecycle (InstructionEnded, Closed, PTransformDone)
- ScopedStateReader construction, close, and closed-path guards
- workerStatusHandler isAlive/shutdown, memoryUsage, goroutineDump, buildInfo, cacheStats
- shortIDCache.getNextShortID

Closes apache#21386
@github-actions github-actions Bot added the go label Jun 16, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the reliability and maintainability of the Go SDK harness package by adding a substantial number of unit tests. By covering edge cases in data and state management, as well as monitoring and logging utilities, these changes ensure that the harness behaves correctly under various conditions, ultimately leading to higher code quality and safer future development.

Highlights

  • Increased Test Coverage: Significantly improved unit test coverage for the Go SDK harness package, raising it from 43% to 50.7%.
  • New Test Suite: Introduced a new test file, sampler_test.go, to validate the lifecycle and state management of the sampler.
  • Expanded Coverage across Core Components: Added comprehensive unit tests for datamgr, statemgr, worker_status, logging, and monitoring components to ensure robust error handling and state transitions.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a comprehensive suite of unit tests for several core components of the Go SDK harness, including the data manager, control plane, logging, monitoring, state manager, sampler, and worker status handler. The feedback suggests improving the tests by directly assigning the inactive field in getPlanOrResponse tests to avoid coupling with internal details, properly asserting standard context.Canceled errors in the sampler tests, and refactoring the state channel manager initialization test to cover actual production code paths instead of mocking its own assertions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +360 to +362
if len(test.inactive.buf) > 0 {
ctrl.inactive = test.inactive
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Instead of checking the length of the unexported buf field of circleBuffer to decide whether to assign it, you can directly assign ctrl.inactive = test.inactive unconditionally. The zero-value of circleBuffer is safe to assign and this avoids coupling the test runner to the internal implementation details of circleBuffer.

			ctrl.inactive = test.inactive

Comment on lines +58 to +62
// start should return nil immediately when context is already canceled.
err := s.start(cancelCtx, samplePeriod)
if err != nil {
t.Errorf("start returned error on canceled context: %v", err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In Go, functions that block on context cancellation typically return ctx.Err() (which is context.Canceled in this case). If s.start follows this standard pattern, then err will be non-nil, causing this test to fail. Please verify if s.start returns context.Canceled on cancellation, and if so, update the assertion to allow or explicitly assert context.Canceled using errors.Is(err, context.Canceled).

Comment on lines +582 to +589
// Force-initialize ports by attempting Open; even if it fails, the map should be created.
// Restore: just set it directly since we don't want to dial.
mgr.mu.Lock()
mgr.ports = make(map[string]*StateChannel)
mgr.mu.Unlock()
if mgr.ports == nil {
t.Error("ports map should exist after initialization")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This test does not actually verify any production code behavior. It manually initializes mgr.ports inside the test itself (lines 584-586) and then asserts that mgr.ports is not nil. This only tests that Go's make function works, rather than testing that StateChannelManager initializes its ports map during actual usage (e.g., when Open is called). Consider triggering the actual initialization logic (e.g., by calling Open with an invalid port and ignoring the dial error) to ensure the production initialization path is covered.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @lostluck for label go.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant