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

Fixes 2896: refactor pulp client in dao #438

Merged
merged 4 commits into from
Nov 16, 2023

Conversation

rverdile
Copy link
Contributor

Summary

The method I introduced here to set the domain scoped pulp client is bug-prone.

This PR demonstrates a new way of setting the pulp client by adding two chained methods. Now, we can initialize the pulp client with context.Background() during dependency injection. Then, replace the context later, during the request, so that the pulp client is using the domain.

For example, the pulp client method GetContentPath() is called with pulpClient.WithContext(ctx).WithDomain(domainName).GetContentPath()

I've commented out most of the failing tests for now, as I was just trying out this approach.

@rverdile rverdile force-pushed the pc-3 branch 3 times, most recently from 8701643 to e553803 Compare October 24, 2023 19:06
@rverdile rverdile changed the title use method chaining to set ctx Fixes 2896: refactor pulp client in dao Oct 24, 2023
Comment on lines 92 to 94
mockPulpClient.On("WithContext", context.Background()).Return(mockPulpClient).Twice()
mockPulpClient.On("GetContentPath").Return(testContentPath, nil).Twice()
mockPulpClient.On("WithDomain", "").Return(mockPulpClient)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this PR is a step in the right direction, although it feels like it adds a lot more boiler plate (like this) to the tests which is not ideal.

Copy link
Member

Choose a reason for hiding this comment

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

here's an idea (if you think it makes sense) . We could add some methods to the mock that do this for us. I imagine something like:

mockPulpClient.WithContext().WithDomain().
     On("GetContentPath").Return(testContentPath, nil).Twice()

and under the hood, this would mock WithContext & WithDomain MinTimes(1) so we'd lose the count on those particular mock calls, but I'm not sure we actually care for that to be precise. I would imagine these methods would have to live in some new file like "pulp_client_mock_helpers.go" or something?

Copy link
Member

Choose a reason for hiding this comment

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

actually i realized MinTimes() is in go mock and not testify. It looks like if you just don't specify Times() it doesn't care the number

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this idea. I'll make that change.

@rverdile rverdile marked this pull request as ready for review October 24, 2023 19:42
@jlsherrill
Copy link
Member

@jlsherrill
Copy link
Member

this needs a rebase now too

@rverdile rverdile force-pushed the pc-3 branch 3 times, most recently from d699fab to 08bc4e6 Compare October 30, 2023 19:27
return nil
func (r *repositoryConfigDaoImpl) WithContext(ctx context.Context) RepositoryConfigDao {
r.ctx = ctx
return r
Copy link
Member

Choose a reason for hiding this comment

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

Should this return a copy of the DoaImpl instead of the same DaoImpl?

I'm imagining two requests coming in at the same time, being run at the same time in the same process. They are sharing a DaoImpl, so if they each call .WithContext, they could stomp over each other?

Its very possible i'm not understanding something corretly thougH :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're right. I'll see if I can get that to return a copy such that mocking still works in the tests.

@Andrewgdewar
Copy link
Member

/retest

@rverdile
Copy link
Contributor Author

this is updated now

@jlsherrill
Copy link
Member

/retest

1 similar comment
@jlsherrill
Copy link
Member

/retest

Copy link
Member

@jlsherrill jlsherrill left a comment

Choose a reason for hiding this comment

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

ACK pending tests passing

@rverdile rverdile merged commit 9c565b3 into content-services:main Nov 16, 2023
7 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.

3 participants