Skip to content

Comments

Re-enable the default rule count test case#200

Merged
PerthCharern merged 1 commit intomasterfrom
PerthCharern/DebugRuleTests
Jan 31, 2018
Merged

Re-enable the default rule count test case#200
PerthCharern merged 1 commit intomasterfrom
PerthCharern/DebugRuleTests

Conversation

@PerthCharern
Copy link
Contributor

@PerthCharern PerthCharern commented Jan 31, 2018

Re-enable the default rule count test case (disabled in #176)

The problem was this:

ValidateCustomExtension test calls ValidationRuleSet.DefaultRuleSet and makes changes to it directly. Since the DefaultRuleSet is cached, the underlying private _defaultRuleSet is actually modified.

The reason we did not see the issue locally might be that the order of local test run and the order on AppVeyor are different. We see the issue only if the "Count" test is run after the custom extension test.

The problem was this:

ValidateCustomExtension test calls ValidationRuleSet.DefaultRuleSet and makes changes to it directly. Since the DefaultRuleSet is cached, this means the private _defaultRuleSet is actually modified.

The reason we are not seeing this repro locally might be that the order of test running locally and the order on AppVeyor are different. We see the issue only if the "Count" test is run after the custom extension test.

// We create a new instance of ValidationRuleSet per call as a safeguard
// against unintentional modification of the private _defaultRuleSet.
return new ValidationRuleSet(_defaultRuleSet);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

   return new ValidationRuleSet(_defaultRuleSet); [](start = 4, length = 54)

We could alternatively prescribe that the caller never modifies the DefaultRuleSet and always uses it like this if they need to use default rules + more rules:

new ValidationRuleSet(ValidationRuleSet.DefaultRuleSet)

But then that's kinda risky and people will not always follow the best practice.

Creating the new instance here safeguards against that scenario as well.

Copy link
Member

Choose a reason for hiding this comment

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

Creating the new collection on each call seems like the best approach. The rules don't need to get re-instantiated so the cost should be minimal. The other option would be to return a ReadOnlyCollection.

BTW, thanks for finding this. I was looking in the completely wrong direction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem.

A ReadOnlyCollection is something I wanted to do, but it will require more changes since we return the default rule set now as a class object, not as a list/dictionary of rules. I'll go with the current implementation for now, and we can revisit if this turns out not working well.

@PerthCharern PerthCharern merged commit b897a1d into master Jan 31, 2018
@PerthCharern PerthCharern deleted the PerthCharern/DebugRuleTests branch January 31, 2018 20:44
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.

2 participants