From 8bfb14814e4d5026af6b5c0c491690d9f6cfd803 Mon Sep 17 00:00:00 2001 From: Deluan Date: Thu, 25 Dec 2025 18:52:33 -0500 Subject: [PATCH] refactor: rename plugin.create() to plugin.instance() Signed-off-by: Deluan --- plugins/host_scheduler_test.go | 14 +++++++------- plugins/host_subsonicapi_test.go | 6 +++--- plugins/manager.go | 4 ++-- plugins/testdata/fake-scheduler/main.go | 17 ++++++++++++++--- reflex.conf | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/plugins/host_scheduler_test.go b/plugins/host_scheduler_test.go index 24ff48c5e..5880ceb85 100644 --- a/plugins/host_scheduler_test.go +++ b/plugins/host_scheduler_test.go @@ -234,14 +234,14 @@ var _ = Describe("SchedulerService", Ordered, func() { Describe("Scheduler Service Isolation", func() { It("should share the same scheduler service across multiple plugin instances", func() { - // This test verifies that when we call plugin.create() multiple times + // This test verifies that when we call plugin.instance() multiple times // (creating multiple instances from the same compiled plugin), they all // share the same scheduler service. This is the expected behavior since // the scheduler service is registered once per plugin at compile time. - // Get the plugin instance + // Get the plugin manager.mu.RLock() - instance, ok := manager.plugins["fake-scheduler"] + plugin, ok := manager.plugins["fake-scheduler"] manager.mu.RUnlock() Expect(ok).To(BeTrue()) @@ -250,10 +250,10 @@ var _ = Describe("SchedulerService", Ordered, func() { Expect(err).ToNot(HaveOccurred()) Expect(testService.GetScheduleCount()).To(Equal(1)) - // Create a second plugin instance - instance2, err := instance.create() + // Create a plugin instance + instance, err := plugin.instance() Expect(err).ToNot(HaveOccurred()) - defer instance2.Close(GinkgoT().Context()) + defer instance.Close(GinkgoT().Context()) // The scheduler service is shared, so the schedule ID should clash // if another instance tries to use the same ID @@ -262,7 +262,7 @@ var _ = Describe("SchedulerService", Ordered, func() { Expect(err.Error()).To(ContainSubstring("already exists")) // But different IDs should work fine - _, err = testService.ScheduleOneTime(GinkgoT().Context(), 60, "instance2-data", "instance2-id") + _, err = testService.ScheduleOneTime(GinkgoT().Context(), 60, "instance2-data", "otherx-id") Expect(err).ToNot(HaveOccurred()) Expect(testService.GetScheduleCount()).To(Equal(2)) }) diff --git a/plugins/host_subsonicapi_test.go b/plugins/host_subsonicapi_test.go index a9d391481..e1455ea63 100644 --- a/plugins/host_subsonicapi_test.go +++ b/plugins/host_subsonicapi_test.go @@ -110,7 +110,7 @@ var _ = Describe("SubsonicAPI Host Function", Ordered, func() { }) It("successfully calls the ping endpoint", func() { - instance, err := plugin.create() + instance, err := plugin.instance() Expect(err).ToNot(HaveOccurred()) defer instance.Close(GinkgoT().Context()) @@ -129,7 +129,7 @@ var _ = Describe("SubsonicAPI Host Function", Ordered, func() { }) It("adds required parameters (c, f, v) to the request", func() { - instance, err := plugin.create() + instance, err := plugin.instance() Expect(err).ToNot(HaveOccurred()) defer instance.Close(GinkgoT().Context()) @@ -146,7 +146,7 @@ var _ = Describe("SubsonicAPI Host Function", Ordered, func() { }) It("returns error when username is missing", func() { - instance, err := plugin.create() + instance, err := plugin.instance() Expect(err).ToNot(HaveOccurred()) defer instance.Close(GinkgoT().Context()) diff --git a/plugins/manager.go b/plugins/manager.go index 5b8f31a60..76ffc125a 100644 --- a/plugins/manager.go +++ b/plugins/manager.go @@ -72,7 +72,7 @@ type pluginInstance struct { closers []io.Closer // Cleanup functions to call on unload } -func (p *pluginInstance) create() (*extism.Plugin, error) { +func (p *pluginInstance) instance() (*extism.Plugin, error) { plugin, err := p.compiled.Instance(context.Background(), extism.PluginInstanceConfig{ ModuleConfig: wazero.NewModuleConfig().WithSysWalltime().WithRandSource(rand.Reader), }) @@ -557,7 +557,7 @@ func callPluginFunction[I any, O any](ctx context.Context, plugin *pluginInstanc var result O // Create plugin instance - p, err := plugin.create() + p, err := plugin.instance() if err != nil { return result, fmt.Errorf("failed to create plugin: %w", err) } diff --git a/plugins/testdata/fake-scheduler/main.go b/plugins/testdata/fake-scheduler/main.go index 7d31fa0db..2bf48f420 100644 --- a/plugins/testdata/fake-scheduler/main.go +++ b/plugins/testdata/fake-scheduler/main.go @@ -51,20 +51,31 @@ func ndManifest() int32 { // Magic payloads trigger specific behaviors to test host functions: // - "schedule-followup": schedules a one-time task via host function // - "schedule-recurring": schedules a recurring task via host function +// - "schedule-duplicate:": attempts to schedule with the given ID (for testing duplicate detection) func NdSchedulerCallback(input SchedulerCallbackInput) (SchedulerCallbackOutput, error) { - switch input.Payload { - case "schedule-followup": + switch { + case input.Payload == "schedule-followup": _, err := SchedulerScheduleOneTime(1, "followup-created", "followup-id") if err != nil { errStr := err.Error() return SchedulerCallbackOutput{Error: &errStr}, nil } - case "schedule-recurring": + case input.Payload == "schedule-recurring": _, err := SchedulerScheduleRecurring("@every 1s", "recurring-created", "recurring-from-plugin") if err != nil { errStr := err.Error() return SchedulerCallbackOutput{Error: &errStr}, nil } + case len(input.Payload) > 19 && input.Payload[:19] == "schedule-duplicate:": + duplicateID := input.Payload[19:] + resp, err := SchedulerScheduleOneTime(60, "duplicate-attempt", duplicateID) + if err != nil { + errStr := err.Error() + return SchedulerCallbackOutput{Error: &errStr}, nil + } + if resp.Error != "" { + return SchedulerCallbackOutput{Error: &resp.Error}, nil + } } return SchedulerCallbackOutput{}, nil } diff --git a/reflex.conf b/reflex.conf index 4cd64baf9..2eb4d131c 100644 --- a/reflex.conf +++ b/reflex.conf @@ -1 +1 @@ --s -r "(\.go$$|\.cpp$$|\.h$$|\.wasm$$|navidrome.toml|resources|token_received.html)" -R "(^ui|^data|^db/migrations)" -- go run -race -tags netgo . +-s -r "(\.go$$|\.cpp$$|\.h$$|navidrome.toml|resources|token_received.html)" -R "(^ui|^data|^db/migrations)" -- go run -race -tags netgo .