Merge pull request #428 from fntlnz/signals-context-duplicated-check

chore(kit/signals): removed duplicated check for signals and add an example
pull/10616/head
Lorenzo Fontana 2018-07-18 17:50:31 +02:00 committed by GitHub
commit 8681940d7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -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

View File

@ -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