Skip to content

Increase sherdlock selector test coverage. #1505

Merged
adecaro merged 6 commits into
hyperledger-labs:mainfrom
Rozerxshashank:fix/issue-1488-coverage-selector
Apr 29, 2026
Merged

Increase sherdlock selector test coverage. #1505
adecaro merged 6 commits into
hyperledger-labs:mainfrom
Rozerxshashank:fix/issue-1488-coverage-selector

Conversation

@Rozerxshashank
Copy link
Copy Markdown
Contributor

@Rozerxshashank Rozerxshashank commented Apr 8, 2026

Description

This PR addresses Issue #1488 by significantly increasing the unit test coverage for the token/services/selector/sherdlock package. Before these changes, the package lacked fundamental unit tests for its core logic, particularly around retry mechanisms and fetcher strategies.

I've introduced a comprehensive test suite that now covers 91.1% of the statements in the package, ensuring the robustness of token selection logic, cache management, and service lifecycle.

Proposed Changes

1. Testability Refactoring

  • service.go: Refactored SelectorService and the internal loader to interact with a new internal TMS (Token Management Service) interface.
  • tms.go: Added a PublicParameters() convenience method to the core ManagementService struct to facilitate interface implementation and SDK-wide testability. This decoupling allows for effective mocking of core services during unit testing.

2. New Unit Test Suite (Component-Specific)

Instead of a generic unit_test.go, tests are now organized by component:

  • Fetcher Strategies (fetcher_unit_test.go): Added tests for LazyFetcher, CachedFetcher, and MixedFetcher to verify correct database interactions and cache refresh cycles.
  • Selector Logic (selector_test.go): Implemented tests for core Selector logic, covering edge cases like unit selection, locked tokens, and selector closure.
  • Retry & Backoff: Comprehensive testing for StubbornSelector and the retry utility, verifying exponential backoff, max retry exhaustion, and respectful handling of context cancellation.
  • Service Lifecycle (service_test.go): Targeted tests for SelectorService manager tracking, loader logic, and shutdown flows.

3. Maintenance & Cleanup

  • Standardized package code using go fmt and golangci-lint (passed with 0 issues).
  • Resolved naming conflicts and type mismatches in mock implementations.
  • Documented justifications for timing tolerances in retry_test.go to ensure stability in CI/CD environments.

Testing & Coverage Evidence

The package was verified using the following metrics:

  • Total Statement Coverage: 91.1%
  • Unit Test Result: PASS (All new and re-organized tests passing)
Component Coverage
Sherdlock Total 91.1%
Service Manager 94.8%
Selector Logic 91.3%
Fetcher Strategies 81.0%

Related Issues

Fixes #1488

@adecaro adecaro force-pushed the fix/issue-1488-coverage-selector branch from fac2330 to e8ad5c4 Compare April 9, 2026 05:49
@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 9, 2026

Hi @Rozerxshashank , you are the master of the test coverage. Fantastic, thanks for the effort 🙏

@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 9, 2026

@Rozerxshashank , are you on discord?

@adecaro adecaro self-requested a review April 9, 2026 05:50
@adecaro adecaro self-assigned this Apr 9, 2026
@adecaro adecaro added this to the Q2/26 milestone Apr 9, 2026
@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 9, 2026

@Rozerxshashank , same thing here. Please, use counterfeiter. Thanks 🙏

@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

Rozerxshashank commented Apr 9, 2026

@Rozerxshashank , are you on discord?

Yes @adecaro, I am on discord my username - shashank4956

@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

@Rozerxshashank , same thing here. Please, use counterfeiter. Thanks 🙏

Sure Thanks for telling. I will use counterfeiter from now on.

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch from fe1ea4d to 725c1aa Compare April 9, 2026 08:17
@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

Hey @adecaro, I've updated the sherdlock package unit tests to utilize counterfeiter-generated fakes as requested. 🙌

@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 9, 2026

@Rozerxshashank , please, generate all the mocks under the folder token/services/selector/sherdlock/mocks to be aligned with the rest of the codebase.

Thanks 🙏

PS: Please, check the linter as well. To fix fmt related issues, you can run make fmt.

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch 3 times, most recently from e77f741 to dfe4ea0 Compare April 9, 2026 10:43
@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

Rozerxshashank commented Apr 9, 2026

Hi @adecaro, I have updated the PR to move all mocks to the mocks/ subfolder and centralized the interfaces in interfaces.go to support the new structure. I also fixed the nlreturn lint issues and ran code formatting. 🙌😊

Once this gets approved, I'll focus on the remaining test coverage!

Also, I’m very interested in applying for the LFX Mentorship for this project. Could you let me know if these types of contributions are a good start for my application, or if there is a specific area I should focus on to be a strong candidate? Thanks for your guidance! 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch 2 times, most recently from 10b3410 to 5b54181 Compare April 9, 2026 10:53
@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 10, 2026

Hi @Rozerxshashank , please, can you rebase the PR. There are conflicts that need to be resolved.
Thanks 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch from 7674cd7 to 4ab965f Compare April 10, 2026 08:57
@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

@adecaro, Done with rebasing the PR.🙌

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch 8 times, most recently from 6bcf8e6 to 87e6bd4 Compare April 11, 2026 11:42
@adecaro adecaro force-pushed the fix/issue-1488-coverage-selector branch from 87e6bd4 to d1d992d Compare April 12, 2026 14:18
cfg := postgres2.DefaultConfig(postgres2.WithDBName(t.Name()))
terminate, _, err := postgres2.StartPostgres(t.Context(), cfg, nil)
require.NoError(t, err)
if err != nil {
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.

We should not skip here.

}

func key(tms *token.ManagementService) string {
func key(tms token.ManagementService) string {
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.

why this? Why passing by value here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My bad! Totally missed that during the refactoring. I've switched it back to a pointer to avoid the extra copying.😅

Comment thread test_output.txt Outdated
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.

we don't need this 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My bad, that was a leftover from some local debugging! I've removed it in the latest push. 😅

@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 12, 2026

Hi @Rozerxshashank , left a few more comments. Thanks 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch from d1d992d to 9c55b1e Compare April 12, 2026 15:41
@adecaro adecaro force-pushed the fix/issue-1488-coverage-selector branch from 9c55b1e to 425da59 Compare April 12, 2026 18:32
@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

@adecaro , I really appreciate you jumping in and fixing that HTLC scanner bug yourself. It was a big help. Thanks for the support! 🙏

@adecaro adecaro force-pushed the fix/issue-1488-coverage-selector branch from 425da59 to a926cf3 Compare April 13, 2026 05:46
@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch 2 times, most recently from 9b6967e to 2f1ea52 Compare April 13, 2026 07:23
// Use generous tolerance for scheduling jitter
for i := 1; i < len(intervals); i++ {
assert.InDelta(t, 10*time.Millisecond, intervals[i], float64(10*time.Millisecond),
assert.InDelta(t, 10*time.Millisecond, intervals[i], float64(50*time.Millisecond),
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.

why do we need this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This increase is to prevent flaky test failures in CI environments where scheduling jitter often causes delays to exceed the previous 10ms tolerance. I've added a code comment to document this justification.

@adecaro
Copy link
Copy Markdown
Contributor

adecaro commented Apr 17, 2026

@Rozerxshashank , we are in the good direction.
Please, some more effort to increase the test coverage of service.go and we are done.
I left also a comment on the code.
Also, please, no generic names like unit_test.go. If the file is testing code in a give file, have <given file>_test.go.

Thanks much 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch from 2f1ea52 to 52975e0 Compare April 17, 2026 08:48
@Rozerxshashank
Copy link
Copy Markdown
Contributor Author

Rozerxshashank commented Apr 17, 2026

@adecaro, I've addressed your feedback: I've reorganized the tests into component-specific files (eliminating unit_test.go) and increased the service.go statement coverage to 91.1%. I also resolved the stability issues in retry_test.go.

Thanks 🙏

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch 2 times, most recently from e78a855 to defa2c7 Compare April 17, 2026 10:13
@adecaro adecaro force-pushed the fix/issue-1488-coverage-selector branch from defa2c7 to 7ee9c17 Compare April 17, 2026 12:54
@adecaro adecaro self-requested a review April 17, 2026 12:54
Copy link
Copy Markdown
Contributor

@adecaro adecaro left a comment

Choose a reason for hiding this comment

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

LGTM

@Rozerxshashank Rozerxshashank force-pushed the fix/issue-1488-coverage-selector branch from 7ee9c17 to 279c11a Compare April 17, 2026 15:27
Signed-off-by: Shashank <yshashank959@gmail.com>
…nterfeiter

Signed-off-by: Shashank <yshashank959@gmail.com>
Signed-off-by: Shashank <yshashank959@gmail.com>
Signed-off-by: Shashank <yshashank959@gmail.com>
Signed-off-by: Shashank <yshashank959@gmail.com>
Signed-off-by: Shashank <yshashank959@gmail.com>
@adecaro adecaro force-pushed the fix/issue-1488-coverage-selector branch from 279c11a to f7fd80a Compare April 29, 2026 08:40
@adecaro adecaro merged commit 0cf5e6d into hyperledger-labs:main Apr 29, 2026
95 checks passed
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.

unit-test: token/services/selector

2 participants