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

Make sure each call to NewRand is initialized with a unique seed #1787

Merged
merged 1 commit into from
Nov 30, 2023

Conversation

dio
Copy link
Contributor

@dio dio commented Nov 30, 2023

Fixes #1352.

There are several potential locations to fix this, but I think this proposed change helps to remedy other tests that rely on random values.

I tested on the main branch to reproduce the flakiness as follows:

for i in {1..10} ; do go test -count 1 ./internal/db/... ; done

I consistently get an error similar to the following (but mostly not at the first run):

2023-11-30 23:28:49.938 WIB [11553] ERROR:  duplicate key value violates unique constraint "projects_name_idx"
2023-11-30 23:28:49.938 WIB [11553] DETAIL:  Key (name)=(dZGJGaiAlN) already exists.

Hence resulting STATEMENT: -- name: CreateOrganization :one and STATEMENT: -- name: CreateProject :one to fail, similar to #1352.

2023-11-30 23:28:49.938 WIB [11553] STATEMENT:  -- name: CreateOrganization :one
        INSERT INTO projects (
            name,
            is_organization,
            metadata 
        ) VALUES (
            $1, TRUE, $2::jsonb
        ) RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at

2023-11-30 23:28:49.940 WIB [11557] ERROR:  invalid input syntax for type json
2023-11-30 23:28:49.940 WIB [11557] DETAIL:  The input string ended unexpectedly.
2023-11-30 23:28:49.940 WIB [11557] CONTEXT:  JSON data, line 1: 
        unnamed portal parameter $3 = ''
2023-11-30 23:28:49.940 WIB [11557] STATEMENT:  -- name: CreateProject :one
        INSERT INTO projects (
            name,
            parent_id,
            metadata
        ) VALUES (
            $1, $2, $3::jsonb
        ) RETURNING id, name, is_organization, metadata, parent_id, created_at, updated_at

With this change, run the above test (with or without -race) is successful:

for i in {1..100} ; do go test -count 1 ./internal/db/... ; done
for i in {1..100} ; do go test -count 1 -race ./internal/db/... ; done

@eleftherias eleftherias self-requested a review November 30, 2023 17:27
Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @dio! This is a nice improvement.

@eleftherias eleftherias merged commit f6da731 into mindersec:main Nov 30, 2023
13 checks passed
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.

tests: flaky database unit tests
2 participants