forked from bytedance/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package channel | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
var ( | ||
_ Signal = (*sigal)(nil) | ||
) | ||
|
||
type Signal interface { | ||
Signal() | ||
Wait(ctx context.Context) bool | ||
} | ||
|
||
type SignalOption func(c *sigal) | ||
|
||
func WithSinalTimeout(timeout time.Duration) SignalOption { | ||
return func(s *sigal) { | ||
s.timeout = timeout | ||
} | ||
} | ||
|
||
func NewSignal(opts ...SignalOption) Signal { | ||
sg := new(sigal) | ||
for _, opt := range opts { | ||
opt(sg) | ||
} | ||
sg.trigger = make(chan struct{}) | ||
return sg | ||
} | ||
|
||
type sigal struct { | ||
trigger chan struct{} | ||
timeout time.Duration | ||
} | ||
|
||
func (s *sigal) Signal() { | ||
select { | ||
case <-s.trigger: | ||
default: | ||
close(s.trigger) | ||
} | ||
} | ||
|
||
func (s *sigal) Wait(ctx context.Context) bool { | ||
if s.timeout > 0 { | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, s.timeout) | ||
defer cancel() | ||
} | ||
if ctx == nil || ctx.Done() == nil { | ||
<-s.trigger | ||
return true | ||
} | ||
select { | ||
case <-s.trigger: | ||
return true | ||
case <-ctx.Done(): | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package channel | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSignal(t *testing.T) { | ||
sg := NewSignal() | ||
var finished int32 | ||
emptyCtx := context.Background() | ||
cancelCtx, cancelFunc := context.WithCancel(emptyCtx) | ||
for i := 0; i < 10; i++ { | ||
go func(i int) { | ||
if i%2 == 0 { | ||
sg.Wait(emptyCtx) | ||
} else { | ||
sg.Wait(cancelCtx) | ||
} | ||
atomic.AddInt32(&finished, 1) | ||
}(i) | ||
} | ||
time.Sleep(time.Millisecond * 100) | ||
cancelFunc() | ||
for atomic.LoadInt32(&finished) != int32(5) { | ||
runtime.Gosched() | ||
} | ||
sg.Signal() | ||
for atomic.LoadInt32(&finished) != int32(10) { | ||
runtime.Gosched() | ||
} | ||
} | ||
|
||
func TestSignalTimeout(t *testing.T) { | ||
sg := NewSignal(WithSinalTimeout(time.Millisecond * 200)) | ||
go func() { | ||
time.Sleep(time.Millisecond * 500) | ||
sg.Signal() | ||
}() | ||
begin := time.Now() | ||
sg.Wait(context.Background()) | ||
cost := time.Since(begin) | ||
t.Logf("cost=%dms", cost.Milliseconds()) | ||
} |