mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 00:26:37 +08:00
feat(drivers): implement in-process cron scheduler driver plugin
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package driver_inproc_cron_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"Wavelet/core"
|
||||
"Wavelet/plugins/drivers/driver_inproc_cron"
|
||||
"Wavelet/plugins/drivers/driver_inproc_worker"
|
||||
)
|
||||
|
||||
func TestInprocCronPlugin(t *testing.T) {
|
||||
ctx := core.NewContext(context.Background())
|
||||
|
||||
workerPlugin := driver_inproc_worker.New()
|
||||
require.NoError(t, workerPlugin.Apply(ctx))
|
||||
require.NoError(t, workerPlugin.Start(context.Background()))
|
||||
defer func() { _ = workerPlugin.Stop(context.Background()) }()
|
||||
|
||||
cronPlugin := driver_inproc_cron.New()
|
||||
assert.Equal(t, "driver_inproc_cron", cronPlugin.Name())
|
||||
assert.Equal(t, core.DriverTypeScheduler, cronPlugin.Type())
|
||||
require.NoError(t, cronPlugin.Apply(ctx))
|
||||
|
||||
var cronTriggered atomic.Int32
|
||||
ctx.Tasks().Register("cron:heartbeat", func(ctx context.Context) error {
|
||||
cronTriggered.Add(1)
|
||||
return nil
|
||||
})
|
||||
|
||||
// Every second cron spec
|
||||
ctx.Schedules().RegisterCron("* * * * * *", "cron:heartbeat", nil)
|
||||
|
||||
require.NoError(t, cronPlugin.Start(context.Background()))
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
return cronTriggered.Load() >= 1
|
||||
}, 3*time.Second, 100*time.Millisecond)
|
||||
|
||||
require.NoError(t, cronPlugin.Stop(context.Background()))
|
||||
}
|
||||
Reference in New Issue
Block a user