77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
// ------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
// ------------------------------------------------------------
|
|
|
|
package twitter
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/dapr/components-contrib/bindings"
|
|
"github.com/dapr/dapr/pkg/logger"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func getTestMetadata(schedule string) bindings.Metadata {
|
|
m := bindings.Metadata{}
|
|
m.Properties = map[string]string{
|
|
"schedule": schedule,
|
|
}
|
|
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 TestCronInitSuccess(t *testing.T) {
|
|
c := getNewCron()
|
|
err := c.Init(getTestMetadata("@every 1h"))
|
|
assert.Nilf(t, err, "error initializing valid schedule")
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
// 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)
|
|
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
|
|
})
|
|
assert.Nilf(t, err, "error on read")
|
|
}
|