Skip to content

Commit

Permalink
Ensure that socket file is free when starting job api server
Browse files Browse the repository at this point in the history
  • Loading branch information
moskyb committed Feb 28, 2023
1 parent d9ca75f commit 4d63043
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 1 addition & 4 deletions jobapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ func NewServer(socketPath string, environ env.Environment) (server *Server, toke
}

if exists {
err = os.RemoveAll(socketPath)
if err != nil {
return nil, "", fmt.Errorf("removing existing socket: %w", err)
}
return nil, "", fmt.Errorf("creating server: file already exists at socket path %s", socketPath)
}

token, err = generateToken(32)
Expand Down
24 changes: 24 additions & 0 deletions jobapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ func TestServerStartStop(t *testing.T) {
}
}

func TestServerStartStop_WithPreExistingSocket(t *testing.T) {
sockName := path.Join(os.TempDir(), "test-socket-collision.sock")
srv1, _, err := jobapi.NewServer(sockName, env.Environment{})
if err != nil {
t.Fatalf("expected initial server creation to succeed, got %v", err)
}

err = srv1.Start()
if err != nil {
t.Fatalf("expected initial server start to succeed, got %v", err)
}
defer srv1.Stop()

expectedErr := fmt.Sprintf("creating server: file already exists at socket path %s", sockName)
_, _, err = jobapi.NewServer(sockName, env.Environment{})
if err == nil {
t.Fatalf("expected second server creation to fail with %s, got nil", expectedErr)
}

if err.Error() != expectedErr {
t.Fatalf("expected second server start to fail with %v, got %v", expectedErr, err)
}
}

type apiTestCase[Req, Resp any] struct {
name string
requestBody *Req
Expand Down

0 comments on commit 4d63043

Please sign in to comment.