From 6c97b05cce8cf18d3c6d72aa1b307fb4e475cc4d Mon Sep 17 00:00:00 2001 From: Jayson Wang Date: Tue, 1 Mar 2022 13:58:38 +0800 Subject: [PATCH] update reference badge --- .github/workflows/go.yml | 2 +- .gitignore | 27 +++++++++++ README.md | 13 ++++-- signal_test.go | 97 ++++++++++++++++++++++++---------------- 4 files changed, 96 insertions(+), 43 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index bc6f9d5..12f296a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -28,7 +28,7 @@ jobs: strategy: matrix: os: [ ubuntu-latest, macos-latest ] - go: [ 1.13, 1.14, 1.15, 1.16, 1.17 ] + go: [ 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19 ] include: - os: ubuntu-latest go-cache: ~/go/pkg/mod diff --git a/.gitignore b/.gitignore index a6623bb..50f840a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,30 @@ +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + # Binaries for programs and plugins *.exe *.exe~ diff --git a/README.md b/README.md index 2bf2bb4..901faef 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # go-signal -[![GoDoc](https://godoc.org/github.com/wjiec/go-signal?status.svg)](https://godoc.org/github.com/wjiec/go-signal) +[![Go Reference](https://pkg.go.dev/badge/github.com/wjiec/go-signal.svg)](https://pkg.go.dev/github.com/wjiec/go-signal) [![Go Report Card](https://goreportcard.com/badge/github.com/wjiec/go-signal)](https://goreportcard.com/report/github.com/wjiec/go-signal) Package signal provides simple, semantic manipulation of the operating system's signal processing. @@ -18,19 +18,24 @@ Listens to the user's signal to exit the program and performs cleanup ```go func main() { f, _ := os.Open("path/to/your/config") + s, _ := http.NewServer(f) + signal.Once(syscall.SIGTERM).Notify(context.TODO(), func(sig os.Signal) { + _ = s.Shutdown() _ = f.Close() }) + + s.Start() } ``` Listening for `SIGUSR1` signals from users and performing services reload ```go -var srv Reloadable - func main() { + ngx, _ := nginx.New(cfg) + signal.When(syscall.SIGUSR1).Notify(context.TODO(), func(sig os.Signal) { - _ = srv.Reload() + _ = ngx.Reload() }) } ``` diff --git a/signal_test.go b/signal_test.go index 9f8e017..88021d1 100644 --- a/signal_test.go +++ b/signal_test.go @@ -10,19 +10,18 @@ import ( "github.com/stretchr/testify/assert" ) -func WaitAny(d time.Duration, wait, timeout func()) { - ch := make(chan struct{}) +func WaitUntil(duration time.Duration, process, timout func()) { + stop := make(chan struct{}) go func() { - wait() - ch <- struct{}{} - close(ch) + process() + close(stop) }() select { - case <-time.After(d): - timeout() - case <-ch: + case <-time.After(duration): + timout() + case <-stop: } } @@ -33,45 +32,67 @@ func NoSignalArrived(t *testing.T) func() { } func TestOnce(t *testing.T) { - var wg sync.WaitGroup - - wg.Add(1) - Once(SigUsr1).Notify(context.TODO(), func(sig os.Signal) { - if assert.Equal(t, SigUsr1, sig) { - wg.Done() + t.Run("normal", func(t *testing.T) { + var wg sync.WaitGroup + + wg.Add(1) + Once(SigUsr1).Notify(context.Background(), func(sig os.Signal) { + if assert.Equal(t, SigUsr1, sig) { + wg.Done() + } + }) + + if pid := os.Getpid(); assert.Greater(t, pid, 0) { + if err := SendSignalUser1(pid); assert.NoError(t, err) { + WaitUntil(time.Second, wg.Wait, NoSignalArrived(t)) + } + + assert.NoError(t, SendSignalUser1(pid)) } }) - if pid := os.Getpid(); assert.Greater(t, pid, 0) { - if err := SendSignalUser1(pid); assert.NoError(t, err) { - WaitAny(time.Second, wg.Wait, NoSignalArrived(t)) - } + t.Run("canceled", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + Once(SigUsr1).Notify(ctx, func(sig os.Signal) { + assert.Equal(t, SigCtx, sig) + }) - assert.NoError(t, SendSignalUser1(pid)) - } + cancel() + }) } func TestWhen(t *testing.T) { - var wg sync.WaitGroup - - wg.Add(1) - When(SigUsr2).Notify(context.TODO(), func(sig os.Signal) { - if assert.Equal(t, SigUsr2, sig) { - wg.Done() + t.Run("normal", func(t *testing.T) { + var wg sync.WaitGroup + + wg.Add(1) + When(SigUsr2).Notify(context.TODO(), func(sig os.Signal) { + if assert.Equal(t, SigUsr2, sig) { + wg.Done() + } + }) + + if pid := os.Getpid(); assert.Greater(t, pid, 0) { + if assert.NoError(t, SendSignalUser2(pid)) { + WaitUntil(time.Second, func() { + wg.Wait() + wg.Add(1) + if assert.NoError(t, SendSignalUser2(pid)) { + wg.Wait() + } + }, NoSignalArrived(t)) + } } }) - if pid := os.Getpid(); assert.Greater(t, pid, 0) { - if assert.NoError(t, SendSignalUser2(pid)) { - WaitAny(time.Second, func() { - wg.Wait() - wg.Add(1) - if assert.NoError(t, SendSignalUser2(pid)) { - wg.Wait() - } - }, NoSignalArrived(t)) - } - } + t.Run("canceled", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + When(SigUsr2).Notify(ctx, func(sig os.Signal) { + assert.Equal(t, SigCtx, sig) + }) + + cancel() + }) } func TestWith(t *testing.T) { @@ -88,7 +109,7 @@ func TestWith(t *testing.T) { if pid := os.Getpid(); assert.Greater(t, pid, 0) { if err := SendSignalUser1(pid); assert.NoError(t, err) { - WaitAny(time.Second, wg.Wait, NoSignalArrived(t)) + WaitUntil(time.Second, wg.Wait, NoSignalArrived(t)) } } }