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

Refactor: use namespace.Name and namespace.ID in tests #7065

Merged
merged 4 commits into from
Jan 11, 2025

Conversation

alexshtin
Copy link
Member

@alexshtin alexshtin commented Jan 10, 2025

What changed?

Refactor: use namespace.Name and namespace.ID instead of string in tests.

Why?

Special narrow types should be used where possible.

How did you test it?

Run tests.

Potential risks

No risks.

Documentation

No.

Is hotfix candidate?

No.

@alexshtin alexshtin requested a review from a team as a code owner January 10, 2025 07:08
Comment on lines +78 to +82
namespace namespace.Name
namespaceID namespace.ID
foreignNamespace namespace.Name
archivalNamespace namespace.Name
archivalNamespaceID namespace.ID
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a change.

Copy link
Contributor

Choose a reason for hiding this comment

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

namespaceName.
foreignNamespaceName.
etc.

otherwise - why namespaceID for namespace.ID but just namespace for namespace.Name?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. Name or ID can be inferred from type of the field (one of the reason of this PR)
  2. We all get used to just "namespace" mostly means name.

Copy link
Contributor

Choose a reason for hiding this comment

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

lets be clear and consistent, and think about future readers.
We have Namespace struct, so no, it is not clear that 'namespace` is name.
so if this represents namespace.Name - please rename this to namespaceName.

@alexshtin alexshtin force-pushed the refactor/namespace-tests branch from 4948d49 to 42f3e98 Compare January 10, 2025 20:59
@@ -321,7 +316,8 @@ func (s *ArchivalSuite) isMutableStateDeleted(namespaceID string, execution *com
}

func (s *ArchivalSuite) startAndFinishWorkflow(
id, wt, tq, namespace, namespaceID string,
id, wt, tq string,
namespace namespace.Name,
Copy link
Contributor

@ychebotarev ychebotarev Jan 10, 2025

Choose a reason for hiding this comment

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

same here. You are sending in namespace.Name structure, but using only namespace name as a string.
Just send string (as it was before).
You can rename it to namespaceName or nsName

Comment on lines +78 to +82
namespace namespace.Name
namespaceID namespace.ID
foreignNamespace namespace.Name
archivalNamespace namespace.Name
archivalNamespaceID namespace.ID
Copy link
Contributor

Choose a reason for hiding this comment

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

namespaceName.
foreignNamespaceName.
etc.

otherwise - why namespaceID for namespace.ID but just namespace for namespace.Name?

@@ -127,15 +130,23 @@ func (s *FunctionalTestBase) HttpAPIAddress() string {
return s.httpAPIAddress
}

func (s *FunctionalTestBase) Namespace() string {
func (s *FunctionalTestBase) Namespace() namespace.Name {
Copy link
Contributor

Choose a reason for hiding this comment

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

NamespaceName()

Copy link
Contributor

Choose a reason for hiding this comment

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

also return string. There is no point of returning napespace.name structure, given that it is a thin wrapper over stirng.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a point. And this is a goal of this PR. Both Namespace.ID and Namespace.Name are string, and it is easy to mess the up. Having distinct types in the code helps this. Major part of server code is using those distinct types already. This is just a follow-up for tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see what is easy to mess up in
func (s *FunctionalTestBase) NamespaceName() string {
?

or in NamespaceID() string.

we are mostly working with strings, but you can't use namespace.Name directly, you still need to call String().
So no, namespace.Name is not a string.

s.Namespace().String() is unnecessary verbose, and not adding anything.
s.NamespaceName() is much cleaner.

return s.namespaceID
}

func (s *FunctionalTestBase) ArchivalNamespace() namespace.Name {
Copy link
Contributor

Choose a reason for hiding this comment

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

ArchivalNamespaceName()

return s.archivalNamespaceID
}

func (s *FunctionalTestBase) ForeignNamespace() namespace.Name {
Copy link
Contributor

Choose a reason for hiding this comment

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

ForeignNamespaceName

s.namespace = RandomizeStr("namespace")
s.Require().NoError(s.registerNamespaceWithDefaults(s.namespace))
s.namespace = namespace.Name(RandomizeStr("namespace"))
s.namespaceID, err = s.registerNamespaceWithDefaults(s.Namespace())
Copy link
Contributor

Choose a reason for hiding this comment

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

this is the only place where you actually assign namespace.ID to something.
You may just add the code that gets namespace ID from namespace here, and avoid returning something that is mostly not used.

@@ -355,17 +369,28 @@ func (s *FunctionalTestBase) registerNamespace(
},
},
})
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

you are not using this value in anywhere directly
remove this return value.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are asserts that err is nil on caller side.


return err
descResp, err := s.client.DescribeNamespace(ctx, &workflowservice.DescribeNamespaceRequest{
Copy link
Contributor

Choose a reason for hiding this comment

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

add this code to that only place where you actually need namespace ID

@@ -134,7 +133,7 @@ func (s *ActivityApiPauseClientTestSuite) TestActivityPauseApi_WhileRunning() {

// pause activity
pauseRequest := &workflowservice.PauseActivityByIdRequest{
Namespace: s.Namespace(),
Namespace: s.Namespace().String(),
Copy link
Contributor

Choose a reason for hiding this comment

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

this is more inconvenient then before.

Copy link
Member Author

Choose a reason for hiding this comment

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

More typing, yes. More inconvenient, no.

}

func (s *FunctionalTestBase) markNamespaceAsDeleted(
namespace string,
nsName namespace.Name,
Copy link
Contributor

Choose a reason for hiding this comment

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

here and everywhere below.
What is the benefit of changing string to namespace.Name?
You are only using it as nsName.String()

Copy link
Member Author

Choose a reason for hiding this comment

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

I replied in another comment.

descResp, err := s.client.DescribeNamespace(ctx, &workflowservice.DescribeNamespaceRequest{
Namespace: nsName.String(),
})

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not there anymore.

@alexshtin alexshtin merged commit 720505d into temporalio:main Jan 11, 2025
49 checks passed
@alexshtin alexshtin deleted the refactor/namespace-tests branch January 11, 2025 01:34
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.

3 participants