refactor: rename plugin.create() to plugin.instance()

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-12-25 18:52:33 -05:00
parent 20c7e6c915
commit 8bfb14814e
5 changed files with 27 additions and 16 deletions

View File

@ -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))
})

View File

@ -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())

View File

@ -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)
}

View File

@ -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:<id>": 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
}

View File

@ -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 .