Clean up Shutdown() vs Close()

This commit is contained in:
Ian Gulliver
2023-04-30 20:57:18 -07:00
parent 7a6457555d
commit 1773738aba
3 changed files with 54 additions and 15 deletions

19
api.go
View File

@@ -258,12 +258,22 @@ func (api *API) Addr() *net.TCPAddr {
}
func (api *API) Serve() error {
// TODO: Validate that api.listener is set
if api.listener == nil {
return jsrest.Errorf(jsrest.ErrInternalServerError, "Serve() called before Listen*()")
}
return api.srv.Serve(api.listener)
}
func (api *API) Shutdown(ctx context.Context) error {
return api.srv.Shutdown(ctx)
err := api.srv.Shutdown(ctx)
if err != nil {
return err
}
api.sb.Close()
return nil
}
func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -308,11 +318,6 @@ func (api *API) serveHTTP(w http.ResponseWriter, r *http.Request) error {
return nil
}
func (api *API) Close() {
// TODO: Merge Shutdown() and Close()
api.sb.Close()
}
func (api *API) registerHandlers(base string, cfg *config) {
api.router.GET(
base,

View File

@@ -14,12 +14,17 @@ import (
func TestRegisterMissingMetadata(t *testing.T) {
t.Parallel()
ctx := context.Background()
dbname := fmt.Sprintf("file:%s?mode=memory&cache=shared", uniuri.New())
api, err := patchy.NewAPI(dbname)
require.NoError(t, err)
defer api.Close()
defer func() {
err := api.Shutdown(ctx)
require.NoError(t, err)
}()
require.Panics(t, func() {
patchy.Register[missingMetadata](api)
@@ -29,12 +34,17 @@ func TestRegisterMissingMetadata(t *testing.T) {
func TestIsSafeSuccess(t *testing.T) {
t.Parallel()
ctx := context.Background()
dbname := fmt.Sprintf("file:%s?mode=memory&cache=shared", uniuri.New())
api, err := patchy.NewAPI(dbname)
require.NoError(t, err)
defer api.Close()
defer func() {
err := api.Shutdown(ctx)
require.NoError(t, err)
}()
patchy.Register[testType3](api)
@@ -44,12 +54,17 @@ func TestIsSafeSuccess(t *testing.T) {
func TestIsSafeWithoutWrite(t *testing.T) {
t.Parallel()
ctx := context.Background()
dbname := fmt.Sprintf("file:%s?mode=memory&cache=shared", uniuri.New())
api, err := patchy.NewAPI(dbname)
require.NoError(t, err)
defer api.Close()
defer func() {
err := api.Shutdown(ctx)
require.NoError(t, err)
}()
require.NoError(t, api.IsSafe())
@@ -61,12 +76,17 @@ func TestIsSafeWithoutWrite(t *testing.T) {
func TestIsSafeWithoutRead(t *testing.T) {
t.Parallel()
ctx := context.Background()
dbname := fmt.Sprintf("file:%s?mode=memory&cache=shared", uniuri.New())
api, err := patchy.NewAPI(dbname)
require.NoError(t, err)
defer api.Close()
defer func() {
err := api.Shutdown(ctx)
require.NoError(t, err)
}()
require.NoError(t, api.IsSafe())
@@ -78,12 +98,17 @@ func TestIsSafeWithoutRead(t *testing.T) {
func TestCheckSafeSuccess(t *testing.T) {
t.Parallel()
ctx := context.Background()
dbname := fmt.Sprintf("file:%s?mode=memory&cache=shared", uniuri.New())
api, err := patchy.NewAPI(dbname)
require.NoError(t, err)
defer api.Close()
defer func() {
err := api.Shutdown(ctx)
require.NoError(t, err)
}()
patchy.Register[testType3](api)
@@ -93,12 +118,17 @@ func TestCheckSafeSuccess(t *testing.T) {
func TestCheckSafeWithoutWrite(t *testing.T) {
t.Parallel()
ctx := context.Background()
dbname := fmt.Sprintf("file:%s?mode=memory&cache=shared", uniuri.New())
api, err := patchy.NewAPI(dbname)
require.NoError(t, err)
defer api.Close()
defer func() {
err := api.Shutdown(ctx)
require.NoError(t, err)
}()
require.NotPanics(t, api.CheckSafe)
@@ -110,12 +140,17 @@ func TestCheckSafeWithoutWrite(t *testing.T) {
func TestCheckSafeWithoutRead(t *testing.T) {
t.Parallel()
ctx := context.Background()
dbname := fmt.Sprintf("file:%s?mode=memory&cache=shared", uniuri.New())
api, err := patchy.NewAPI(dbname)
require.NoError(t, err)
defer api.Close()
defer func() {
err := api.Shutdown(ctx)
require.NoError(t, err)
}()
require.NotPanics(t, api.CheckSafe)

View File

@@ -284,7 +284,6 @@ func (ta *testAPI) shutdown(t *testing.T) {
err := ta.api.Shutdown(context.Background())
require.NoError(t, err)
ta.api.Close()
ta.proxy.Close()
}