-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
- Loading branch information
Showing
2 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package signal | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var buffer bytes.Buffer | ||
|
||
func TestServer(t *testing.T) { | ||
srv := newServer() | ||
|
||
go srv.Start(context.Background()) //nolint:errcheck | ||
|
||
time.Sleep(2 * time.Second) | ||
|
||
assert.NoError(t, syscall.Kill(os.Getpid(), syscall.SIGUSR1)) | ||
assert.NoError(t, syscall.Kill(os.Getpid(), syscall.SIGUSR2)) | ||
|
||
time.Sleep(2 * time.Second) | ||
|
||
assert.Equal(t, `exampleHandler signal: user defined signal 1 | ||
signal: user defined signal 1, handler: *signal.example2Handler, err: example2Handler panic | ||
exampleHandler signal: user defined signal 2 | ||
`, buffer.String()) | ||
|
||
srv.Stop(context.Background()) //nolint:errcheck | ||
} | ||
|
||
func newServer() *Server { | ||
srv := NewServer( | ||
WithRecoveryHandler(func(err interface{}, signal os.Signal, handler Handler) { | ||
buffer.WriteString(fmt.Sprintf("signal: %s, handler: %T, err: %v\n", signal, handler, err)) | ||
}), | ||
) | ||
|
||
srv.Register(&exampleHandler{}, &example2Handler{}) | ||
|
||
return srv | ||
} | ||
|
||
type exampleHandler struct{} | ||
|
||
func (h *exampleHandler) Listen() []os.Signal { | ||
return []os.Signal{syscall.SIGUSR1, syscall.SIGUSR2} | ||
} | ||
|
||
func (h *exampleHandler) Handle(sig os.Signal) { | ||
buffer.WriteString(fmt.Sprintf("exampleHandler signal: %s\n", sig)) | ||
} | ||
|
||
type example2Handler struct{} | ||
|
||
func (h *example2Handler) Listen() []os.Signal { | ||
return []os.Signal{syscall.SIGUSR1} | ||
} | ||
|
||
func (h *example2Handler) Handle(os.Signal) { | ||
panic("example2Handler panic") | ||
} |