Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for pinot_visibility_metric_clients.go #5767

Merged
merged 3 commits into from
Mar 9, 2024

Conversation

bowenxia
Copy link
Contributor

@bowenxia bowenxia commented Mar 8, 2024

What changed?
Added test for pinot_visibility_metric_clients.go

Why?
for a better code coverage: (100%)

How did you test it?
unit test

Potential risks

Release notes

Documentation Changes

Copy link

codecov bot commented Mar 8, 2024

Codecov Report

Merging #5767 (bba5e51) into master (a2e8540) will increase coverage by 0.19%.
The diff coverage is n/a.

Additional details and impacted files

see 8 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a2e8540...bba5e51. Read the comment docs.

@bowenxia bowenxia enabled auto-merge (squash) March 8, 2024 22:38
@coveralls
Copy link

Pull Request Test Coverage Report for Build 018e2026-edf4-4412-9bff-53e48e77108b

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 60 unchanged lines in 11 files lost coverage.
  • Overall coverage increased (+0.2%) to 64.077%

Files with Coverage Reduction New Missed Lines %
common/peerprovider/ringpopprovider/config.go 2 81.58%
service/history/task/transfer_active_task_executor.go 2 72.38%
service/matching/taskListManager.go 2 79.7%
service/history/task/fetcher.go 2 85.57%
service/history/task/transfer_standby_task_executor.go 2 87.42%
service/history/execution/mutable_state_util.go 2 78.48%
common/persistence/statsComputer.go 3 94.64%
common/task/fifo_task_scheduler.go 4 83.51%
service/history/task/cross_cluster_task_processor.go 8 80.79%
service/history/execution/mutable_state_task_refresher.go 14 66.77%
Totals Coverage Status
Change from base Build 018e1f4b-3683-47f3-842a-f3e687b3f996: 0.2%
Covered Lines: 93407
Relevant Lines: 145774

💛 - Coveralls

mockMetricClient.On("Scope", mock.Anything, mock.Anything).Return(mockScope).Once()
mockScope.On("StartTimer", mock.Anything, mock.Anything).Return(testStopwatch).Once()

if test.expectedError != nil {
Copy link
Member

Choose a reason for hiding this comment

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

this is probably ok, but in general if we see that we're having to branch out at all in the table-test Run method that's a sign that we should probably use a different pattern.

A nice way to do this (I think), is to create an affordance - or a function which takes the mock and sets the expectations per Table-test row. Eg:

          tests := map[string]struct {
		request       *p.VisibilityDeleteWorkflowExecutionRequest
                 producerMockAffordance function(*MockPublisher)
		expectedError error
	}{
		"Case1: error case": {
			request:       errorRequest,
                         producerMockAffordance: func(m MockPublisher){
                             m.Expect().Publish(...).Return(errors.New("an error"))
                         }
			expectedError: fmt.Errorf("error"),
		}
	}


for name, td := range tests
      t.Run(name, func(t testing.T) {
          ... create mocks
         td.producerAffordance(mockProducer)

         // assertions
      }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Will make another PR to do this.

@bowenxia bowenxia merged commit 7fc6f24 into master Mar 9, 2024
21 checks passed
@bowenxia bowenxia deleted the xbowen_pinot_visibility_metric_clients_test branch March 9, 2024 07:40
Comment on lines +87 to +107
// create mock clients
ctrl := gomock.NewController(t)
mockPinotClient := pnt.NewMockGenericClient(ctrl)
mockProducer := &mocks.KafkaProducer{}
mockMetricClient := &metricsClientMocks.Client{}
mockScope := &metricsClientMocks.Scope{}

// create metricClient
logger := testlogger.New(t)
mgr := NewPinotVisibilityStore(mockPinotClient, &service.Config{
ValidSearchAttributes: dynamicconfig.GetMapPropertyFn(definition.GetDefaultIndexedKeys()),
ESIndexMaxResultWindow: dynamicconfig.GetIntPropertyFn(3),
}, mockProducer, testlogger.New(t))
visibilityStore := mgr.(*pinotVisibilityStore)
pinotVisibilityManager := p.NewVisibilityManagerImpl(visibilityStore, logger)
visibilityMgr := NewPinotVisibilityMetricsClient(pinotVisibilityManager, mockMetricClient, logger)
metricsClient := visibilityMgr.(*pinotVisibilityMetricsClient)

// mock behaviors
mockMetricClient.On("Scope", mock.Anything, mock.Anything).Return(mockScope).Once()
mockScope.On("StartTimer", mock.Anything, mock.Anything).Return(testStopwatch).Once()
Copy link
Member

Choose a reason for hiding this comment

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

This initialisation part looks pretty much the same across all 17 tests which is a big overhead.
Is it possible to extract it into some function?
This looks like a great candidate to Suite. I know we're trying to avoid it, but we can maybe borrow the idea of TestSuite' SetupTest().

Copy link
Member

Choose a reason for hiding this comment

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

Moving the absolutely-fixed parts to helper functions make sense but I'd be cautious of doing that because you may end up passing bunch of flags/parameters to make such setup helper functions handle all cases. Some level of redundancy in test code is OK and readability/self-containedness/explicitness/repeatability should be optimized IMO.

Suites work fine until they don't :) When you want to add a new test cases that initializes common components slightly differently you end up doing hacky things (example) or add the redundant code at that point anyway.

Copy link
Member

Choose a reason for hiding this comment

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

I would second Taylan's point that it's hard to generalize assertions in a single place, but I think you're right that this can and perhaps should be less verbose, and, to your wider point, that we need to be careful to ensure the tests remain high-quality as we sprint for coverage.

I didn't see it too concerning, but in general I'd err towards avoiding if/else blocks with branching mock assertions such as line 109/116 and subsequently. If the assertions are different between table test invocations, then probably best to set them per table-row (with a func which is invoked to setup the mocks per row). That would make it easier to follow I think.

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.

5 participants