Skip to content

Commit

Permalink
fix: clean up sock file
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed May 31, 2021
1 parent 4ad02de commit cfb8bbf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,16 @@ func Run() {
}
log.Infof("listening to %s", sockAddr)

// clean up sock file created by others
if err := os.RemoveAll(sockAddr); err != nil {
log.Fatalf("remove file %s: %s", sockAddr, err)
}
// clean up sock file created by me
defer func() {
if err := os.RemoveAll(sockAddr); err != nil {
log.Errorf("remove file %s: %s", sockAddr, err)
}
}()

l, err := net.Listen("unix", sockAddr)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/binary"
"net"
"os"
"syscall"
"testing"
"time"

Expand All @@ -34,7 +35,8 @@ func TestGetSockAddr(t *testing.T) {
}

func TestRun(t *testing.T) {
addr := "unix:/tmp/x.sock"
path := "/tmp/x.sock"
addr := "unix:" + path
os.Setenv(SockAddrEnv, addr)
os.Setenv(ConfCacheTTLEnv, "60")

Expand Down Expand Up @@ -65,4 +67,10 @@ func TestRun(t *testing.T) {
defer conn.Close()
conn.Write(c.header)
}

syscall.Kill(syscall.Getpid(), syscall.SIGINT)
time.Sleep(10 * time.Millisecond)

_, err := os.Stat(path)
assert.True(t, os.IsNotExist(err))
}

0 comments on commit cfb8bbf

Please sign in to comment.