From d662a1829f3f727d387910973c6012e90d2ead1a Mon Sep 17 00:00:00 2001 From: Mark Chmarny Date: Wed, 8 Jul 2020 04:58:09 -0700 Subject: [PATCH] fixes lint errors, adds invoke test --- bindings/cron/cron.go | 21 ++++++-------- bindings/cron/cron_test.go | 57 ++++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/bindings/cron/cron.go b/bindings/cron/cron.go index 5269b60bf..e997368e8 100644 --- a/bindings/cron/cron.go +++ b/bindings/cron/cron.go @@ -69,26 +69,23 @@ func (b *Binding) Read(handler func(*bindings.ReadResponse) error) error { return errors.Wrapf(err, "error scheduling %s", b.schedule) } c.Start() - b.logger.Debugf("next run: %v", c.Entry(id).Next.Sub(time.Now())) - for { - select { - case stop := <-b.stopCh: - if stop { - c.Stop() - return nil - } - } - } + b.logger.Debugf("next run: %v", time.Until(c.Entry(id).Next)) + <-b.stopCh + b.logger.Debugf("stopping schedule: %s", b.schedule) + c.Stop() + return nil } // Invoke exposes way to stop previously started cron func (b *Binding) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) { b.logger.Debugf("operation: %v", req.Operation) if req.Operation == bindings.DeleteOperation { - b.logger.Debugf("stopping schedule: %s", b.schedule) b.stopCh <- true } return &bindings.InvokeResponse{ - Metadata: req.Metadata, + Metadata: map[string]string{ + "schedule": b.schedule, + "stopTimeUTC": time.Now().UTC().String(), + }, }, nil } diff --git a/bindings/cron/cron_test.go b/bindings/cron/cron_test.go index c81fe2bf4..ab50cd25a 100644 --- a/bindings/cron/cron_test.go +++ b/bindings/cron/cron_test.go @@ -22,34 +22,55 @@ func getTestMetadata(schedule string) bindings.Metadata { return m } +func getNewCron() *Binding { + l := logger.NewLogger("cron") + if os.Getenv("DEBUG") != "" { + l.SetOutputLevel(logger.DebugLevel) + } + return NewCron(l) +} + // go test -v -timeout 15s -count=1 ./bindings/cron/ -func TestInitSuccess(t *testing.T) { - c := NewCron(logger.NewLogger("test")) +func TestCronInitSuccess(t *testing.T) { + c := getNewCron() err := c.Init(getTestMetadata("@every 1h")) assert.Nilf(t, err, "error initializing valid schedule") } -func TestInitFailure(t *testing.T) { - c := NewCron(logger.NewLogger("test")) +func TestCronInitWithSeconds(t *testing.T) { + c := getNewCron() + err := c.Init(getTestMetadata("15 * * * * *")) + assert.Nilf(t, err, "error initializing schedule with seconds") +} + +func TestCronInitFailure(t *testing.T) { + c := getNewCron() err := c.Init(getTestMetadata("invalid schedule")) assert.NotNilf(t, err, "no error while initializing invalid schedule") } -// TestRead excutes the Read method -// go test -v -count=1 -timeout 15s -run TestRead ./bindings/cron/ -func TestRead(t *testing.T) { - l := logger.NewLogger("test") - l.SetOutputLevel(logger.DebugLevel) - c := NewCron(l) - err := c.Init(getTestMetadata("@every 1s")) - assert.Nilf(t, err, "error initializing valid schedule") - - h := func(res *bindings.ReadResponse) error { +// TestLongRead +// go test -v -count=1 -timeout 15s -run TestLongRead ./bindings/cron/ +func TestCronReadWithDeleteInvoke(t *testing.T) { + c := getNewCron() + schedule := "@every 1s" + assert.Nilf(t, c.Init(getTestMetadata(schedule)), "error initializing valid schedule") + testsNum := 3 + i := 0 + err := c.Read(func(res *bindings.ReadResponse) error { assert.NotNil(t, res) - os.Exit(0) + assert.LessOrEqualf(t, i, testsNum, "Invoke didn't stop the schedule") + i++ + if i == testsNum { + resp, err := c.Invoke(&bindings.InvokeRequest{ + Operation: bindings.DeleteOperation, + }) + assert.Nil(t, err) + scheduleVal, exists := resp.Metadata["schedule"] + assert.Truef(t, exists, "Response metadata doesn't include the expected 'schedule' key") + assert.Equal(t, schedule, scheduleVal) + } return nil - } - - err = c.Read(h) + }) assert.Nilf(t, err, "error on read") }