From 6250b410874e3f7de045c663ff3986b5b1ed3892 Mon Sep 17 00:00:00 2001 From: wangzhuowei Date: Tue, 19 Mar 2024 19:34:13 +0800 Subject: [PATCH] fix: cond options --- lang/channel/cond.go | 6 +++++- lang/channel/cond_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lang/channel/cond.go b/lang/channel/cond.go index 8888271c..d9c0c196 100644 --- a/lang/channel/cond.go +++ b/lang/channel/cond.go @@ -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{} diff --git a/lang/channel/cond_test.go b/lang/channel/cond_test.go index fb950b20..f750da41 100644 --- a/lang/channel/cond_test.go +++ b/lang/channel/cond_test.go @@ -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()