Skip to content

Commit

Permalink
update reference badge
Browse files Browse the repository at this point in the history
  • Loading branch information
wjiec committed Feb 6, 2024
1 parent c9df0d9 commit 6c97b05
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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~
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()
})
}
```
Expand Down
97 changes: 59 additions & 38 deletions signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
}
}

Expand All @@ -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) {
Expand All @@ -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))
}
}
}

0 comments on commit 6c97b05

Please sign in to comment.