Clean up Shutdown() vs Close()
This commit is contained in:
19
api.go
19
api.go
@@ -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,
|
||||
|
||||
49
api_test.go
49
api_test.go
@@ -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)
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user