Skip to content

Commit

Permalink
fix: cond options
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Mar 19, 2024
1 parent 676a5d5 commit 6250b41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lang/channel/cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ type Cond interface {
}

func NewCond(opts ...CondOption) Cond {
return new(cond)
cd := new(cond)
for _, opt := range opts {
opt(cd)
}
return cd
}

type condSignal = chan struct{}
Expand Down
12 changes: 12 additions & 0 deletions lang/channel/cond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func TestCond(t *testing.T) {
}
}

func TestCondTimeout(t *testing.T) {
cd := NewCond(WithCondTimeout(time.Millisecond * 200))
go func() {
time.Sleep(time.Millisecond * 500)
cd.Broadcast()
}()
begin := time.Now()
cd.Wait(context.Background())
cost := time.Since(begin)
t.Logf("cost=%dms", cost.Milliseconds())
}

func BenchmarkChanCond(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
Expand Down

0 comments on commit 6250b41

Please sign in to comment.