-
Notifications
You must be signed in to change notification settings - Fork 0
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
Test com.Cond #46
base: main
Are you sure you want to change the base?
Test com.Cond #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCond_Broadcast(t *testing.T) { | ||
cond := NewCond(context.Background()) | ||
defer func() { _ = cond.Close() }() | ||
|
||
done := cond.Done() | ||
wait := cond.Wait() | ||
|
||
select { | ||
case <-done: | ||
require.Fail(t, "cond should not be closed, yet") | ||
case <-wait: | ||
require.Fail(t, "cond should not be ready, yet") | ||
case <-time.After(time.Second / 10): | ||
} | ||
|
||
cond.Broadcast() | ||
|
||
select { | ||
case <-done: | ||
require.Fail(t, "cond should still not be closed") | ||
case <-cond.Done(): | ||
require.Fail(t, "cond should not be closed for round 2, yet") | ||
case <-cond.Wait(): | ||
require.Fail(t, "cond should not be ready for round 2") | ||
case <-time.After(time.Second / 10): | ||
} | ||
|
||
select { | ||
case _, ok := <-wait: | ||
if ok { | ||
require.Fail(t, "cond ready channel should be closed") | ||
} | ||
case <-time.After(time.Second / 10): | ||
require.Fail(t, "cond should be ready") | ||
} | ||
} | ||
|
||
func TestCond_Close(t *testing.T) { | ||
cond := NewCond(context.Background()) | ||
done := cond.Done() | ||
wait := cond.Wait() | ||
|
||
require.NoError(t, cond.Close()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In its current implementation, |
||
|
||
select { | ||
case _, ok := <-done: | ||
if ok { | ||
require.Fail(t, "existing cond-closed channel should be closed") | ||
} | ||
case <-time.After(time.Second / 10): | ||
require.Fail(t, "cond should be closed") | ||
} | ||
|
||
select { | ||
case _, ok := <-cond.Done(): | ||
if ok { | ||
require.Fail(t, "new cond-closed channel should be closed") | ||
} | ||
case <-time.After(time.Second / 10): | ||
require.Fail(t, "cond should be still closed") | ||
} | ||
Comment on lines
+63
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you digged out is an implementation detail which tests don't rely on. |
||
|
||
select { | ||
case <-wait: | ||
require.Fail(t, "cond should not be ready") | ||
case <-time.After(time.Second / 10): | ||
} | ||
|
||
require.Panics(t, func() { cond.Wait() }, "cond should panic on Wait after Close") | ||
require.Panics(t, func() { cond.Broadcast() }, "cond should panic on Broadcast after Close") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a difference between those two cases? Both cases check the value of
cond.Done()
, returningcond.done
which doesn't change over the time. Unless I'm missing something, this is the exact same channel.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you digged out is an implementation detail which tests don't rely on.