Skip to content

Commit

Permalink
Merge pull request #1 from wjiec/testing/more-scenes
Browse files Browse the repository at this point in the history
adding the test scenes
  • Loading branch information
wjiec authored Mar 5, 2023
2 parents 47067bc + bf5901e commit d4f595e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 42 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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 d4f595e

Please sign in to comment.