navidrome/plugins/host_scheduler_test.go
Deluan Quintão 5b73a4d5b7
feat(plugins): add TimeNow function to SchedulerService (#4337)
* feat: add TimeNow function to SchedulerService plugin

Added new TimeNow RPC method to the SchedulerService host service that returns
the current time in two formats: RFC3339Nano string and Unix milliseconds int64.
This provides plugins with a standardized way to get current time information
from the host system.

The implementation includes:
- TimeNowRequest/TimeNowResponse protobuf message definitions
- Go host service implementation using time.Now()
- Complete test coverage with format validation
- Generated WASM interface code for plugin communication

* feat: add LocalTimeZone field to TimeNow response

Added LocalTimeZone field to TimeNowResponse message in the SchedulerService
plugin host service. This field contains the server's local timezone name
(e.g., 'America/New_York', 'UTC') providing plugins with timezone context
alongside the existing RFC3339Nano and Unix milliseconds timestamps.

The implementation includes:
- New local_time_zone protobuf field definition
- Go implementation using time.Now().Location().String()
- Updated test coverage with timezone validation
- Generated protobuf serialization/deserialization code

* docs: update plugin README with TimeNow function documentation

Updated the plugins README.md to document the new TimeNow function in the
SchedulerService. The documentation includes detailed descriptions of the
three return formats (RFC3339Nano, UnixMilli, LocalTimeZone), practical
use cases, and a comprehensive Go code example showing how plugins can
access current time information for logging, calculations, and timezone-aware
operations.

* docs: remove wrong comment from InitRequest

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: add missing TimeNow method to namedSchedulerService

Added TimeNow method implementation to namedSchedulerService struct to satisfy the scheduler.SchedulerService interface contract. This method was recently added to the interface but the namedSchedulerService wrapper was not updated, causing compilation failures in plugin tests. The implementation is a simple pass-through to the underlying scheduler service since TimeNow doesn't require any special handling for named callbacks.

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2025-07-13 14:23:58 -04:00

193 lines
6.2 KiB
Go

package plugins
import (
"context"
"time"
"github.com/navidrome/navidrome/core/metrics"
"github.com/navidrome/navidrome/plugins/host/scheduler"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("SchedulerService", func() {
var (
ss *schedulerService
manager *managerImpl
pluginName = "test_plugin"
)
BeforeEach(func() {
manager = createManager(nil, metrics.NewNoopInstance())
ss = manager.schedulerService
})
Describe("One-time scheduling", func() {
It("schedules one-time jobs successfully", func() {
req := &scheduler.ScheduleOneTimeRequest{
DelaySeconds: 1,
Payload: []byte("test payload"),
ScheduleId: "test-job",
}
resp, err := ss.scheduleOneTime(context.Background(), pluginName, req)
Expect(err).ToNot(HaveOccurred())
Expect(resp.ScheduleId).To(Equal("test-job"))
Expect(ss.hasSchedule(pluginName + ":" + "test-job")).To(BeTrue())
Expect(ss.getScheduleType(pluginName + ":" + "test-job")).To(Equal(ScheduleTypeOneTime))
// Test auto-generated ID
req.ScheduleId = ""
resp, err = ss.scheduleOneTime(context.Background(), pluginName, req)
Expect(err).ToNot(HaveOccurred())
Expect(resp.ScheduleId).ToNot(BeEmpty())
})
It("cancels one-time jobs successfully", func() {
req := &scheduler.ScheduleOneTimeRequest{
DelaySeconds: 10,
ScheduleId: "test-job",
}
_, err := ss.scheduleOneTime(context.Background(), pluginName, req)
Expect(err).ToNot(HaveOccurred())
cancelReq := &scheduler.CancelRequest{
ScheduleId: "test-job",
}
resp, err := ss.cancelSchedule(context.Background(), pluginName, cancelReq)
Expect(err).ToNot(HaveOccurred())
Expect(resp.Success).To(BeTrue())
Expect(ss.hasSchedule(pluginName + ":" + "test-job")).To(BeFalse())
})
})
Describe("Recurring scheduling", func() {
It("schedules recurring jobs successfully", func() {
req := &scheduler.ScheduleRecurringRequest{
CronExpression: "* * * * *", // Every minute
Payload: []byte("test payload"),
ScheduleId: "test-cron",
}
resp, err := ss.scheduleRecurring(context.Background(), pluginName, req)
Expect(err).ToNot(HaveOccurred())
Expect(resp.ScheduleId).To(Equal("test-cron"))
Expect(ss.hasSchedule(pluginName + ":" + "test-cron")).To(BeTrue())
Expect(ss.getScheduleType(pluginName + ":" + "test-cron")).To(Equal(ScheduleTypeRecurring))
// Test auto-generated ID
req.ScheduleId = ""
resp, err = ss.scheduleRecurring(context.Background(), pluginName, req)
Expect(err).ToNot(HaveOccurred())
Expect(resp.ScheduleId).ToNot(BeEmpty())
})
It("cancels recurring jobs successfully", func() {
req := &scheduler.ScheduleRecurringRequest{
CronExpression: "* * * * *", // Every minute
ScheduleId: "test-cron",
}
_, err := ss.scheduleRecurring(context.Background(), pluginName, req)
Expect(err).ToNot(HaveOccurred())
cancelReq := &scheduler.CancelRequest{
ScheduleId: "test-cron",
}
resp, err := ss.cancelSchedule(context.Background(), pluginName, cancelReq)
Expect(err).ToNot(HaveOccurred())
Expect(resp.Success).To(BeTrue())
Expect(ss.hasSchedule(pluginName + ":" + "test-cron")).To(BeFalse())
})
})
Describe("Replace existing schedules", func() {
It("replaces one-time jobs with new ones", func() {
// Create first job
req1 := &scheduler.ScheduleOneTimeRequest{
DelaySeconds: 10,
Payload: []byte("test payload 1"),
ScheduleId: "replace-job",
}
_, err := ss.scheduleOneTime(context.Background(), pluginName, req1)
Expect(err).ToNot(HaveOccurred())
// Verify that the initial job exists
scheduleId := pluginName + ":" + "replace-job"
Expect(ss.hasSchedule(scheduleId)).To(BeTrue(), "Initial schedule should exist")
beforeCount := ss.scheduleCount()
// Replace with second job using same ID
req2 := &scheduler.ScheduleOneTimeRequest{
DelaySeconds: 60, // Use a longer delay to ensure it doesn't execute during the test
Payload: []byte("test payload 2"),
ScheduleId: "replace-job",
}
_, err = ss.scheduleOneTime(context.Background(), pluginName, req2)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
return ss.hasSchedule(scheduleId)
}).Should(BeTrue(), "Schedule should exist after replacement")
Expect(ss.scheduleCount()).To(Equal(beforeCount), "Job count should remain the same after replacement")
})
It("replaces recurring jobs with new ones", func() {
// Create first job
req1 := &scheduler.ScheduleRecurringRequest{
CronExpression: "0 * * * *",
Payload: []byte("test payload 1"),
ScheduleId: "replace-cron",
}
_, err := ss.scheduleRecurring(context.Background(), pluginName, req1)
Expect(err).ToNot(HaveOccurred())
beforeCount := ss.scheduleCount()
// Replace with second job using same ID
req2 := &scheduler.ScheduleRecurringRequest{
CronExpression: "*/5 * * * *",
Payload: []byte("test payload 2"),
ScheduleId: "replace-cron",
}
_, err = ss.scheduleRecurring(context.Background(), pluginName, req2)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
return ss.hasSchedule(pluginName + ":" + "replace-cron")
}).Should(BeTrue(), "Schedule should exist after replacement")
Expect(ss.scheduleCount()).To(Equal(beforeCount), "Job count should remain the same after replacement")
})
})
Describe("TimeNow", func() {
It("returns current time in RFC3339Nano, Unix milliseconds, and local timezone", func() {
now := time.Now()
req := &scheduler.TimeNowRequest{}
resp, err := ss.timeNow(context.Background(), req)
Expect(err).ToNot(HaveOccurred())
Expect(resp.UnixMilli).To(BeNumerically(">=", now.UnixMilli()))
Expect(resp.LocalTimeZone).ToNot(BeEmpty())
// Validate RFC3339Nano format can be parsed
parsedTime, parseErr := time.Parse(time.RFC3339Nano, resp.Rfc3339Nano)
Expect(parseErr).ToNot(HaveOccurred())
// Validate that Unix milliseconds is reasonably close to the RFC3339Nano time
expectedMillis := parsedTime.UnixMilli()
Expect(resp.UnixMilli).To(Equal(expectedMillis))
// Validate local timezone matches the current system timezone
expectedTimezone := now.Location().String()
Expect(resp.LocalTimeZone).To(Equal(expectedTimezone))
})
})
})