Skip to content

Commit

Permalink
Merge pull request #6 from istyle-inc/fix/use-tebata
Browse files Browse the repository at this point in the history
Use tebata
  • Loading branch information
銀シャリ committed Feb 27, 2018
2 parents 784337a + 9075f73 commit e3612f7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@
[[constraint]]
name = "github.com/koron/go-dproxy"
version = "1.1.0"

[[constraint]]
branch = "master"
name = "github.com/syossan27/tebata"
4 changes: 4 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"net"
"os"

"github.com/istyle-inc/iceflake/foundation"
)

type Connector struct {
Expand Down Expand Up @@ -53,6 +55,8 @@ func (c *Connector) AcceptListener() error {
}

func (c *Connector) SignalTearDown() {
foundation.SLogger.Infof("Shutting down.\n")
c.Listener.Close()
os.Remove(c.SocketFilePath)
os.Exit(0)
}
29 changes: 20 additions & 9 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/istyle-inc/iceflake/foundation"

"github.com/koron/go-dproxy"
"github.com/syossan27/tebata"
"go.uber.org/zap"
)

Expand All @@ -31,6 +32,8 @@ func newConnector() *Connector {
}

func TestNewConnector(t *testing.T) {
defer os.Remove(testSocketFilePath)

generator := NewGenerator(testWorkerId)

connector := &Connector{
Expand All @@ -51,6 +54,8 @@ func TestNewConnector(t *testing.T) {
}

func TestConnector_Listen(t *testing.T) {
defer os.Remove(testSocketFilePath)

err := connector.Listen()
if err != nil {
t.Error("Fail connector listen: ", err)
Expand All @@ -59,14 +64,10 @@ func TestConnector_Listen(t *testing.T) {
}

func TestConnector_AcceptListener(t *testing.T) {
defer os.Remove(testSocketFilePath)

// Override exit function used for signalHandler
done := make(chan int, 1)
foundation.Exit = func(signal int) {
if signal != 0 {
t.Error("Catch illegal signal")
}
done <- 1
}

// Change zap output to none
stderr := os.Stderr
Expand All @@ -81,9 +82,19 @@ func TestConnector_AcceptListener(t *testing.T) {
t.Error("Fail connector listen: ", err)
}

foundation.SignalHandling(connector)
tbt := tebata.New(syscall.SIGINT, syscall.SIGKILL)
tbt.Reserve(
func() {
foundation.SLogger.Infof("Shutting down.\n")
done <- 1
},
)
go connector.AcceptListener()
foundation.SignalCh <- syscall.SIGINT // Send interrupt signal
p, err := os.FindProcess(os.Getpid())
if err != nil {
t.Error("Fail find process: ", err)
}
p.Signal(os.Interrupt)

// Waiting done signalHandler
<-done
Expand All @@ -101,7 +112,7 @@ func TestConnector_AcceptListener(t *testing.T) {
var stdOutJSON interface{}
json.Unmarshal(buf.Bytes(), &stdOutJSON)
msg, err := dproxy.New(stdOutJSON).M("msg").String()
if msg != "Catch signal interrupt: Shutting down.\n" {
if msg != "Shutting down.\n" {
t.Error("Invalid output")
}
}
24 changes: 0 additions & 24 deletions foundation/signal.go

This file was deleted.

9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (

"github.com/istyle-inc/iceflake/foundation"

"syscall"

"github.com/syossan27/tebata"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -33,7 +36,11 @@ func main() {
if err != nil {
foundation.Logger.Fatal("Error: ", zap.Error(err))
}
foundation.SignalHandling(connector) // Catch interrupt signal for stop listen

// Catch interrupt signal for stop listen
t := tebata.New(syscall.SIGINT, syscall.SIGKILL)
t.Reserve(connector.SignalTearDown)

err = connector.AcceptListener()
if err != nil {
foundation.Logger.Fatal("Error: ", zap.Error(err))
Expand Down

0 comments on commit e3612f7

Please sign in to comment.