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

Concurrency primitives need concurrent tests #6274

Merged
merged 4 commits into from
Sep 9, 2024
Merged

Conversation

Groxx
Copy link
Contributor

@Groxx Groxx commented Sep 9, 2024

Spotted while looking into other racy code.
Honestly I think it's shocking that this was ever approved.
The good news though is that it does seem to work.

I also took the opportunity to remove the wildly unnecessary testify/suite, and add some comparative benchmarks:

BenchmarkLock-8                          2719692               450.3 ns/op           148 B/op          3 allocs/op
BenchmarkStdlibLock-8                   89316176                13.40 ns/op            0 B/op          0 allocs/op
BenchmarkParallelLock-8                  2119658               562.3 ns/op           148 B/op          3 allocs/op
BenchmarkParallelStdlibLock-8           10159339               111.6 ns/op             0 B/op          0 allocs/op

At the moment I'm not sure if this is an acceptable cost or not tbh (though it feels like "no").

Honestly I think it's shocking that this was ever approved.
The good news though is that it does seem to work.

I also took the opportunity to remove the wildly unnecessary test suite use.
lock.Unlock()
}

func BenchmarkLock(b *testing.B) {
l := NewMutex()
ctx := context.Background()
for n := 0; n < b.N; n++ {
l.Lock(ctx) //nolint:errcheck
l.Unlock() //nolint:staticcheck
_ = l.Lock(ctx)
Copy link
Contributor

@taylanisikdemir taylanisikdemir Sep 9, 2024

Choose a reason for hiding this comment

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

The lockInternal could be optimized by keeping an internal state in an atomic variable. When there's noone holding the lock (state=0) it can be a lightweight operation bypassing the goroutine + channel wait.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea, I've got a different PR for making changes. this is just setting groundwork for "verify that this thing actually works"

@Groxx Groxx enabled auto-merge (squash) September 9, 2024 19:35
Copy link

codecov bot commented Sep 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.11%. Comparing base (0a6ad93) to head (1b61c5f).
Report is 1 commits behind head on master.

Additional details and impacted files

see 8 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0a6ad93...1b61c5f. Read the comment docs.

@Groxx Groxx merged commit cc0b5ad into uber:master Sep 9, 2024
19 of 20 checks passed
@Groxx Groxx deleted the locktests branch September 9, 2024 21:00
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