From 4f3fe28905c13c9b719141fc96fb1c3b13bf1ec1 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Sun, 18 Aug 2019 19:24:08 +0200 Subject: [PATCH] adding missingt test in context package --- internal/app/xhttp/pkg/context/context.go | 4 +- .../app/xhttp/pkg/context/context_test.go | 22 +++++++++ internal/pkg/pm2/dummy.go | 46 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 internal/pkg/pm2/dummy.go diff --git a/internal/app/xhttp/pkg/context/context.go b/internal/app/xhttp/pkg/context/context.go index 25047b16..9eb5abaf 100644 --- a/internal/app/xhttp/pkg/context/context.go +++ b/internal/app/xhttp/pkg/context/context.go @@ -26,12 +26,12 @@ type Context struct { } // New creates a new Context. -func New(c echo.Context, logger xlog.Logger, config conf.Config, processess ...pm2.Process) Context { +func New(c echo.Context, logger xlog.Logger, config conf.Config, processes ...pm2.Process) Context { return Context{ c, logger, config, - processess, + processes, resource.Resource{}, time.Now(), } diff --git a/internal/app/xhttp/pkg/context/context_test.go b/internal/app/xhttp/pkg/context/context_test.go index 9b803460..f6a1ac3e 100644 --- a/internal/app/xhttp/pkg/context/context_test.go +++ b/internal/app/xhttp/pkg/context/context_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/thecodingmachine/gotenberg/internal/pkg/conf" + "github.com/thecodingmachine/gotenberg/internal/pkg/pm2" "github.com/thecodingmachine/gotenberg/test" ) @@ -26,6 +27,27 @@ func TestMustCastFromEchoContext(t *testing.T) { }) } +func TestProcessesHealthcheck(t *testing.T) { + // process is viable. + ctx := New( + test.DummyEchoContext(), + test.DebugLogger(), + conf.DefaultConfig(), + pm2.NewDummyProcess(true), + ) + err := ctx.ProcessesHealthcheck() + assert.Nil(t, err) + // process is not viable. + ctx = New( + test.DummyEchoContext(), + test.DebugLogger(), + conf.DefaultConfig(), + pm2.NewDummyProcess(false), + ) + err = ctx.ProcessesHealthcheck() + test.AssertError(t, err) +} + func TestLogRequestResult(t *testing.T) { ctx := New( test.DummyEchoContext(), diff --git a/internal/pkg/pm2/dummy.go b/internal/pkg/pm2/dummy.go new file mode 100644 index 00000000..a65376e4 --- /dev/null +++ b/internal/pkg/pm2/dummy.go @@ -0,0 +1,46 @@ +package pm2 + +type dummyProcess struct { + isViable bool +} + +// NewDummyProcess returns a dummy +// process. +func NewDummyProcess(isViable bool) Process { + return dummyProcess{ + isViable: isViable, + } +} + +func (p dummyProcess) Fullname() string { + return "dummy process" +} + +func (p dummyProcess) Start() error { + return nil +} + +func (p dummyProcess) IsViable() bool { + return p.isViable +} + +func (p dummyProcess) Stop() error { + return nil +} + +func (p dummyProcess) args() []string { + return []string{} +} + +func (p dummyProcess) binary() string { + return "dummy" +} + +func (p dummyProcess) warmup() { + // let's do nothing. +} + +// Compile-time checks to ensure type implements desired interfaces. +var ( + _ = Process(new(dummyProcess)) +)