From 2ab851094b53902809fa6530667bfdbe75fcc96d Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 6 Jul 2024 13:52:48 +0300 Subject: [PATCH] chore: add app tests --- pkg/app.go | 14 ++++-- pkg/app_test.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 pkg/app_test.go diff --git a/pkg/app.go b/pkg/app.go index 2013fb0..89f9dcb 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -29,6 +29,7 @@ type App struct { ReportGenerator *report.Generator StateGenerator *state.Generator ReportDispatcher *report.Dispatcher + StopChannel chan bool } func NewApp(configPath string, filesystem fs.FS, version string) *App { @@ -93,6 +94,7 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App { ReportGenerator: reportGenerator, StateGenerator: stateGenerator, ReportDispatcher: reportDispatcher, + StopChannel: make(chan bool), } } @@ -103,15 +105,19 @@ func (a *App) Start() { } c := cron.New() - if _, err := c.AddFunc(a.Config.Interval, func() { - a.Report() - }); err != nil { + if _, err := c.AddFunc(a.Config.Interval, a.Report); err != nil { a.Logger.Panic().Err(err).Msg("Error processing cron pattern") } c.Start() a.Logger.Info().Str("interval", a.Config.Interval).Msg("Scheduled proposals reporting") - select {} + <-a.StopChannel + a.Logger.Info().Msg("Shutting down...") + c.Stop() +} + +func (a *App) Stop() { + a.StopChannel <- true } func (a *App) Report() { diff --git a/pkg/app_test.go b/pkg/app_test.go new file mode 100644 index 0000000..6ba4cf4 --- /dev/null +++ b/pkg/app_test.go @@ -0,0 +1,123 @@ +package pkg + +import ( + "main/pkg/fs" + reportersPkg "main/pkg/reporters" + "sync" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAppFailToLoadConfig(t *testing.T) { + t.Parallel() + + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + filesystem := &fs.TestFS{} + + NewApp("notexisting.toml", filesystem, "1.2.3") +} + +func TestAppFailInvalidConfig(t *testing.T) { + t.Parallel() + + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + filesystem := &fs.TestFS{} + + NewApp("config-invalid.toml", filesystem, "1.2.3") +} + +func TestAppCreateConfigWithWarnings(t *testing.T) { + t.Parallel() + + filesystem := &fs.TestFS{} + + app := NewApp("config-with-warnings.toml", filesystem, "1.2.3") + require.NotNil(t, app) +} + +func TestAppCreateConfigValid(t *testing.T) { + t.Parallel() + + filesystem := &fs.TestFS{} + + app := NewApp("config-valid.toml", filesystem, "1.2.3") + require.NotNil(t, app) +} + +func TestAppStartReporterFailedToInit(t *testing.T) { + t.Parallel() + + filesystem := &fs.TestFS{} + + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + app := NewApp("config-valid.toml", filesystem, "1.2.3") + app.ReportDispatcher.Reporters = []reportersPkg.Reporter{&reportersPkg.TestReporter{WithInitFail: true}} + + app.Start() + require.NotNil(t, app) +} + +func TestAppStartInvalidCronPattern(t *testing.T) { + t.Parallel() + + filesystem := &fs.TestFS{} + + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + app := NewApp("config-valid.toml", filesystem, "1.2.3") + app.Config.Interval = "invalid" + + app.Start() + require.NotNil(t, app) +} + +func TestAppStartOk(t *testing.T) { + t.Parallel() + + filesystem := &fs.TestFS{} + app := NewApp("config-valid.toml", filesystem, "1.2.3") + app.ReportDispatcher.Reporters = []reportersPkg.Reporter{&reportersPkg.TestReporter{}} + + var wg sync.WaitGroup + wg.Add(1) + + go func() { + app.Start() + wg.Done() + }() + + app.Stop() + wg.Wait() + assert.True(t, true) +} + +func TestAppReport(t *testing.T) { + t.Parallel() + + filesystem := &fs.TestFS{} + app := NewApp("config-valid.toml", filesystem, "1.2.3") + app.Report() + + assert.True(t, true) +}