plugin_test API

plugin_test

package

API reference for the plugin_test package.

S
struct
Implements: Plugin

testPlugin

pkg/plugin/registry_test.go:11-15
type testPlugin struct

Methods

Name
Method

Returns

string
func (testPlugin) Name() string
{ return p.name }
Start
Method

Returns

error
func (testPlugin) Start() error
{
	if p.order != nil {
		*p.order = append(*p.order, "start:"+p.name)
	}
	return p.fail
}
Stop
Method

Returns

error
func (testPlugin) Stop() error
{
	if p.order != nil {
		*p.order = append(*p.order, "stop:"+p.name)
	}
	return p.fail
}

Fields

Name Type Description
name string
order *[]string
fail error
F
function

TestRegistryPreservesLifecycleOrder

Parameters

pkg/plugin/registry_test.go:33-54
func TestRegistryPreservesLifecycleOrder(t *testing.T)

{
	order := []string{}
	registry := plugin.NewRegistry()
	if err := registry.Register(testPlugin{name: "first", order: &order}); err != nil {
		t.Fatalf("Register() error = %v", err)
	}
	if err := registry.Register(testPlugin{name: "second", order: &order}); err != nil {
		t.Fatalf("Register() error = %v", err)
	}

	if errs := registry.StartAll(); len(errs) != 0 {
		t.Fatalf("StartAll() errors = %v, want none", errs)
	}
	if errs := registry.StopAll(); len(errs) != 0 {
		t.Fatalf("StopAll() errors = %v, want none", errs)
	}

	want := []string{"start:first", "start:second", "stop:second", "stop:first"}
	if !reflect.DeepEqual(order, want) {
		t.Fatalf("lifecycle order = %v, want %v", order, want)
	}
}
F
function

TestRegistryRejectsDuplicatesAndSupportsUnregister

Parameters

pkg/plugin/registry_test.go:56-70
func TestRegistryRejectsDuplicatesAndSupportsUnregister(t *testing.T)

{
	registry := plugin.NewRegistry()
	p := testPlugin{name: "dup"}
	if err := registry.Register(p); err != nil {
		t.Fatalf("Register() error = %v", err)
	}
	if err := registry.Register(p); !errors.Is(err, plugin.ErrAlreadyRegistered) {
		t.Fatalf("Register() error = %v, want ErrAlreadyRegistered", err)
	}

	registry.Unregister("dup")
	if _, ok := registry.Get("dup"); ok {
		t.Fatalf("Get() after Unregister() = true, want false")
	}
}
F
function

TestDiscoverFromValuesRegistersPlugins

Parameters

pkg/plugin/registry_test.go:72-84
func TestDiscoverFromValuesRegistersPlugins(t *testing.T)

{
	registry := plugin.NewRegistry()
	count := registry.DiscoverFromValues(testPlugin{name: "one"}, nil, testPlugin{name: "two"})
	if count != 2 {
		t.Fatalf("DiscoverFromValues() = %d, want %d", count, 2)
	}

	got := registry.Names()
	want := []string{"one", "two"}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("Names() = %v, want %v", got, want)
	}
}
F
function

TestFactoryRegistryCreatesPlugins

Parameters

pkg/plugin/registry_test.go:86-105
func TestFactoryRegistryCreatesPlugins(t *testing.T)

{
	factories := plugin.NewFactoryRegistry()
	if err := factories.Register("sample", func() plugin.Plugin { return testPlugin{name: "sample"} }); err != nil {
		t.Fatalf("Register() error = %v", err)
	}
	if err := factories.Register("sample", func() plugin.Plugin { return testPlugin{name: "sample"} }); !errors.Is(err, plugin.ErrFactoryExists) {
		t.Fatalf("Register() duplicate error = %v, want ErrFactoryExists", err)
	}

	created, err := factories.Create("sample")
	if err != nil {
		t.Fatalf("Create() error = %v", err)
	}
	if created.Name() != "sample" {
		t.Fatalf("Create() plugin name = %q, want %q", created.Name(), "sample")
	}
	if _, err := factories.Create("missing"); !errors.Is(err, plugin.ErrFactoryNotFound) {
		t.Fatalf("Create() missing error = %v, want ErrFactoryNotFound", err)
	}
}