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

Fix flaky transform_test.go #639

Merged
merged 3 commits into from
Nov 27, 2024
Merged

Fix flaky transform_test.go #639

merged 3 commits into from
Nov 27, 2024

Conversation

msakrejda
Copy link
Contributor

Since snapshots include query references and query plan references,
and since those can be orderered nondeterministically, the test can
fail if the snapshot is built with fields in an unexpected order.

The existing test tries to account for that by enumerating all
possible combinations, but this is very difficult to maintain, and
very difficult to scale to additional fields with this pattern.

Instead, rewrite snapshots to a canonical format to ensure comparisons
are stable. We can't just sort the array fields before serializing the
snapshot for comparison, since QueryReferences and similar items are
referenced by an index into their array. To work around that, we sort
the QueryReferences (tracking their original index), and rewrite all
entries that reference them to reference their new index instead.

See discussion in #630 1.

Since snapshots include query references and query plan references,
and since those can be orderered nondeterministically, the test can
fail if the snapshot is built with fields in an unexpected order.

The existing test tries to account for that by enumerating all
possible combinations, but this is very difficult to maintain, and
very difficult to scale to additional fields with this pattern.

Instead, rewrite snapshots to a canonical format to ensure comparisons
are stable. We can't just sort the array fields before serializing the
snapshot for comparison, since QueryReferences and similar items are
referenced by an index into their array. To work around that, we sort
the QueryReferences (tracking their original index), and rewrite all
entries that reference them to reference their new index instead.

See discussion in #630 [1].

[1]: #630 (comment)
@msakrejda msakrejda requested a review from a team November 26, 2024 00:59
@@ -66,189 +68,198 @@ func TestStatements(t *testing.T) {
ServerStatistic: &pganalyze_collector.ServerStatistic{},
Replication: &pganalyze_collector.Replication{},
QueryReferences: []*pganalyze_collector.QueryReference{
&pganalyze_collector.QueryReference{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is unrelated, but element types can be inferred when the containing array type is specified, so including the type here is unnecessary.

@keiko713
Copy link
Contributor

Oh noes, I didn't realize that you were working on this and created this PR 🙃 #640
My fix is simpler as it's a bug fix but maybe this is better as it's addressing the part that the test gets complex as we add more things in the future. I'll close my PR

@msakrejda
Copy link
Contributor Author

Oh sorry! Technically I did mention in Slack I would try to work on this, but it was a couple of weeks ago now 😬 .

@keiko713
Copy link
Contributor

I haven't looked into the code of makeCanonical closely, but I just ran test locally 10 times in a row with this branch and it sadly fails consistently currently. Maybe it's missing to consider some situation?

@msakrejda
Copy link
Contributor Author

Interesting, thanks for confirming. I ran this a bunch of times locally and had no errors before submitting, but I just ran it in a loop and it did fail eventually. Looking into it.

@msakrejda
Copy link
Contributor Author

Okay, I found the issue. We need to rewrite the QueryPlanReferences' QueryIdx as well. I fixed that and ran the test in a loop for a while, and that seems to resolve the problem.

@msakrejda msakrejda merged commit 29dcb62 into main Nov 27, 2024
3 checks passed
@msakrejda msakrejda deleted the fix-flaky-transform_test branch November 27, 2024 00:59
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