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

[recorder] Improved getUniqueName #6345

Merged
merged 6 commits into from
Dec 19, 2019
Merged

Conversation

sadasant
Copy link
Contributor

@sadasant sadasant commented Nov 27, 2019

This is a simplistic solution. The idea is that, when the tests run, they will store these names in an in-memory array and throw if the name happens to be repeated. It's cheap enough.

I see a random number generator in the name, I wonder if we should remove that.

Fixes #5698

@sadasant sadasant self-assigned this Nov 27, 2019
@jeremymeng
Copy link
Member

nit: In practice the array is likely very small so this doesn’t matter. But usually for checking membership a set type of data structure is used?

@sadasant
Copy link
Contributor Author

sadasant commented Dec 2, 2019

@jeremymeng

nit: In practice the array is likely very small so this doesn’t matter. But usually for checking membership a set type of data structure is used?

I changed it to be const namesAlreadyTaken: { [key: string]: boolean } = {}! I think this is better too (if this is what you meant).

Math.floor(Math.random() * 10000).toString(),
5,
"00000"
)}`;
if (namesAlreadyTaken[name]) {
throw new Error(`Test name: ${name} is duplicated.`);
Copy link
Member

Choose a reason for hiding this comment

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

The error might be a bit misleading.

image

Also, it's better to have this namesAlreadyTaken at the getUniqueName function which is exposed from the Recorder object.

getUniqueName: function(prefix: string, label?: string): string {

And throw an error saying.. Label <label> is already taken for the corresponding prefix, please provide a different prefix <prefix> OR give the same prefix with a new label.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! I see. I pushed something that seems to tackle this issue better. Let me know if this works.

Copy link
Member

Choose a reason for hiding this comment

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

No, the new code misses the case where two different prefixes are trying to get the label. According to the recorder logic, it is not allowed(meaning playback would fail).

The older namesAlreadyTaken was fine, but put that here and use it for the label(labels cannot be repeated) -

getUniqueName: function(prefix: string, label?: string): string {

Is that clear?

Copy link
Member

Choose a reason for hiding this comment

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

Pushed a commit to solve the issue without the need for a new array/set.

@HarshaNalluru
Copy link
Member

Updated getUniqueName checks and throws an error if a label is already used.

image

@HarshaNalluru HarshaNalluru added Client This issue points to a problem in the data-plane of the library. test-utils-recorder Label for the issues related to the common recorder labels Dec 18, 2019
Copy link
Member

@xirzec xirzec left a comment

Choose a reason for hiding this comment

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

Small data structure suggestion

getUniqueName: function(prefix: string, label?: string): string {
let name: string;
if (!label) {
label = prefix;
}
if (isRecordMode()) {
name = getUniqueName(prefix);
recorder.uniqueTestInfo["uniqueName"][label] = name;
if (recorder.uniqueTestInfo["uniqueName"][label]) {
Copy link
Member

Choose a reason for hiding this comment

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

If we wanted to avoid any potential problem with weird labels e.g. toString() or __proto__, we could use a Set here instead of an object.

Copy link
Member

Choose a reason for hiding this comment

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

weird labels? example?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, leaving it as it is for now since it is of low priority and will take up a lot of efforts to revamp the recordings. Might revisit later.

Copy link
Member

Choose a reason for hiding this comment

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

labels like "toString()". Currently I believe we only use this for getting unique names for Azure resources and label is provided by users if they want to use a pre-existing prefix so we probably don't need to care for edge cases.

Copy link
Member

Choose a reason for hiding this comment

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

Yup, discussed offline with @xirzec !

Copy link
Contributor

@chradek chradek left a comment

Choose a reason for hiding this comment

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

Looks like a nice improvement!

@HarshaNalluru HarshaNalluru merged commit e094e24 into Azure:master Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. test-utils-recorder Label for the issues related to the common recorder
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Common Recorder] Throw helpful error when getUniqueName() is used with the same label more than once
5 participants