fixes lint errors, adds invoke test
This commit is contained in:
parent
e98b3fc968
commit
d662a1829f
|
@ -69,26 +69,23 @@ func (b *Binding) Read(handler func(*bindings.ReadResponse) error) error {
|
||||||
return errors.Wrapf(err, "error scheduling %s", b.schedule)
|
return errors.Wrapf(err, "error scheduling %s", b.schedule)
|
||||||
}
|
}
|
||||||
c.Start()
|
c.Start()
|
||||||
b.logger.Debugf("next run: %v", c.Entry(id).Next.Sub(time.Now()))
|
b.logger.Debugf("next run: %v", time.Until(c.Entry(id).Next))
|
||||||
for {
|
<-b.stopCh
|
||||||
select {
|
b.logger.Debugf("stopping schedule: %s", b.schedule)
|
||||||
case stop := <-b.stopCh:
|
c.Stop()
|
||||||
if stop {
|
return nil
|
||||||
c.Stop()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke exposes way to stop previously started cron
|
// Invoke exposes way to stop previously started cron
|
||||||
func (b *Binding) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
|
func (b *Binding) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
|
||||||
b.logger.Debugf("operation: %v", req.Operation)
|
b.logger.Debugf("operation: %v", req.Operation)
|
||||||
if req.Operation == bindings.DeleteOperation {
|
if req.Operation == bindings.DeleteOperation {
|
||||||
b.logger.Debugf("stopping schedule: %s", b.schedule)
|
|
||||||
b.stopCh <- true
|
b.stopCh <- true
|
||||||
}
|
}
|
||||||
return &bindings.InvokeResponse{
|
return &bindings.InvokeResponse{
|
||||||
Metadata: req.Metadata,
|
Metadata: map[string]string{
|
||||||
|
"schedule": b.schedule,
|
||||||
|
"stopTimeUTC": time.Now().UTC().String(),
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,34 +22,55 @@ func getTestMetadata(schedule string) bindings.Metadata {
|
||||||
return m
|
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/
|
// go test -v -timeout 15s -count=1 ./bindings/cron/
|
||||||
func TestInitSuccess(t *testing.T) {
|
func TestCronInitSuccess(t *testing.T) {
|
||||||
c := NewCron(logger.NewLogger("test"))
|
c := getNewCron()
|
||||||
err := c.Init(getTestMetadata("@every 1h"))
|
err := c.Init(getTestMetadata("@every 1h"))
|
||||||
assert.Nilf(t, err, "error initializing valid schedule")
|
assert.Nilf(t, err, "error initializing valid schedule")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInitFailure(t *testing.T) {
|
func TestCronInitWithSeconds(t *testing.T) {
|
||||||
c := NewCron(logger.NewLogger("test"))
|
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"))
|
err := c.Init(getTestMetadata("invalid schedule"))
|
||||||
assert.NotNilf(t, err, "no error while initializing invalid schedule")
|
assert.NotNilf(t, err, "no error while initializing invalid schedule")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRead excutes the Read method
|
// TestLongRead
|
||||||
// go test -v -count=1 -timeout 15s -run TestRead ./bindings/cron/
|
// go test -v -count=1 -timeout 15s -run TestLongRead ./bindings/cron/
|
||||||
func TestRead(t *testing.T) {
|
func TestCronReadWithDeleteInvoke(t *testing.T) {
|
||||||
l := logger.NewLogger("test")
|
c := getNewCron()
|
||||||
l.SetOutputLevel(logger.DebugLevel)
|
schedule := "@every 1s"
|
||||||
c := NewCron(l)
|
assert.Nilf(t, c.Init(getTestMetadata(schedule)), "error initializing valid schedule")
|
||||||
err := c.Init(getTestMetadata("@every 1s"))
|
testsNum := 3
|
||||||
assert.Nilf(t, err, "error initializing valid schedule")
|
i := 0
|
||||||
|
err := c.Read(func(res *bindings.ReadResponse) error {
|
||||||
h := func(res *bindings.ReadResponse) error {
|
|
||||||
assert.NotNil(t, res)
|
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
|
return nil
|
||||||
}
|
})
|
||||||
|
|
||||||
err = c.Read(h)
|
|
||||||
assert.Nilf(t, err, "error on read")
|
assert.Nilf(t, err, "error on read")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue