Merge pull request #428 from fntlnz/signals-context-duplicated-check
chore(kit/signals): removed duplicated check for signals and add an examplepull/10616/head
commit
8681940d7a
|
@ -18,12 +18,8 @@ func WithSignals(ctx context.Context, sigs ...os.Signal) context.Context {
|
|||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case sig := <-sigCh:
|
||||
for _, s := range sigs {
|
||||
if s == sig {
|
||||
return
|
||||
}
|
||||
}
|
||||
case <-sigCh:
|
||||
return
|
||||
}
|
||||
}()
|
||||
return ctx
|
||||
|
|
|
@ -25,6 +25,25 @@ func ExampleWithSignals() {
|
|||
// finished
|
||||
}
|
||||
|
||||
func ExampleWithUnregisteredSignals() {
|
||||
dctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond*100)
|
||||
defer cancel()
|
||||
|
||||
ctx := WithSignals(dctx, syscall.SIGUSR1)
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond) // after some time SIGUSR2 is sent
|
||||
// mimicking a signal from the outside, WithSignals will not handle it
|
||||
syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("finished")
|
||||
}
|
||||
// Output:
|
||||
// finished
|
||||
}
|
||||
|
||||
func TestWithSignals(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
Loading…
Reference in New Issue