Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f416e73611 |
No files matched your search
+4
-5
@@ -4,7 +4,6 @@ package app
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -79,7 +78,7 @@ func run(_ *cobra.Command, _ []string) {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
exitCode := runApp(app, os.Stderr)
|
exitCode := runApp(app)
|
||||||
if exitCode != 0 {
|
if exitCode != 0 {
|
||||||
os.Exit(exitCode)
|
os.Exit(exitCode)
|
||||||
}
|
}
|
||||||
@@ -89,13 +88,13 @@ func run(_ *cobra.Command, _ []string) {
|
|||||||
// and returns the status pixad exits with, as fx.App.Run does. It prints
|
// and returns the status pixad exits with, as fx.App.Run does. It prints
|
||||||
// a start or stop error to stderr, once, as the error that caused it:
|
// a start or stop error to stderr, once, as the error that caused it:
|
||||||
// dig.RootCause drops the frames fx and dig wrap around it.
|
// dig.RootCause drops the frames fx and dig wrap around it.
|
||||||
func runApp(app *fx.App, stderr io.Writer) int {
|
func runApp(app *fx.App) int {
|
||||||
startCtx, cancelStart := context.WithTimeout(context.Background(), app.StartTimeout())
|
startCtx, cancelStart := context.WithTimeout(context.Background(), app.StartTimeout())
|
||||||
defer cancelStart()
|
defer cancelStart()
|
||||||
|
|
||||||
err := app.Start(startCtx)
|
err := app.Start(startCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _ = fmt.Fprintln(stderr, dig.RootCause(err))
|
fmt.Fprintln(os.Stderr, dig.RootCause(err))
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
@@ -107,7 +106,7 @@ func runApp(app *fx.App, stderr io.Writer) int {
|
|||||||
|
|
||||||
err = app.Stop(stopCtx)
|
err = app.Stop(stopCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _ = fmt.Fprintln(stderr, dig.RootCause(err))
|
fmt.Fprintln(os.Stderr, dig.RootCause(err))
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"go.uber.org/fx"
|
|
||||||
)
|
|
||||||
|
|
||||||
var errTestStopFailed = errors.New("test stop hook failed")
|
|
||||||
|
|
||||||
// TestRunAppPrintsStopErrorOnce checks that when a stop hook fails, runApp
|
|
||||||
// prints the hook's error once and returns exit status 1.
|
|
||||||
func TestRunAppPrintsStopErrorOnce(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
app := fx.New(
|
|
||||||
fx.NopLogger,
|
|
||||||
fx.Invoke(func(lifecycle fx.Lifecycle, shutdowner fx.Shutdowner) {
|
|
||||||
lifecycle.Append(fx.Hook{
|
|
||||||
// Ask to shut down as soon as the app has started, so
|
|
||||||
// that runApp goes on to stop it.
|
|
||||||
OnStart: func(context.Context) error { return shutdowner.Shutdown() },
|
|
||||||
OnStop: func(context.Context) error { return errTestStopFailed },
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
var stderr bytes.Buffer
|
|
||||||
|
|
||||||
exitCode := runApp(app, &stderr)
|
|
||||||
if exitCode != 1 {
|
|
||||||
t.Errorf("runApp() = %d, want 1", exitCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
want := errTestStopFailed.Error() + "\n"
|
|
||||||
if stderr.String() != want {
|
|
||||||
t.Errorf("stderr = %q, want %q", stderr.String(), want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestRunAppReturnsShutdownExitCode checks that runApp returns the exit
|
|
||||||
// code a shutdown request names.
|
|
||||||
func TestRunAppReturnsShutdownExitCode(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
app := fx.New(
|
|
||||||
fx.NopLogger,
|
|
||||||
fx.Invoke(func(lifecycle fx.Lifecycle, shutdowner fx.Shutdowner) {
|
|
||||||
lifecycle.Append(fx.Hook{
|
|
||||||
OnStart: func(context.Context) error {
|
|
||||||
return shutdowner.Shutdown(fx.ExitCode(3))
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
exitCode := runApp(app, io.Discard)
|
|
||||||
if exitCode != 3 {
|
|
||||||
t.Errorf("runApp() = %d, want 3", exitCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user