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

Test com.Cond #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions com/cond_test.go
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")
Comment on lines +28 to +31
Copy link
Member

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(), returning cond.done which doesn't change over the time. Unless I'm missing something, this is the exact same channel.

Copy link
Member Author

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.

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())
Copy link
Member

Choose a reason for hiding this comment

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

In its current implementation, Close() always returns nil, even when the API doc states otherwise.


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
Copy link
Member

Choose a reason for hiding this comment

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

Again, same .done channel as already checked in the select above.

Copy link
Member Author

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.


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")
}