plugin_test API

plugin_test

package

API reference for the plugin_test package.

S
struct
Implements: Plugin

testPlugin

core/plugin/registry_test.go:11-16
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.startFail
}
Stop
Method

Returns

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

Fields

Name Type Description
name string
order *[]string
startFail error
stopFail error
S
struct
Implements: Plugin

retryStopPlugin

core/plugin/registry_test.go:18-21
type retryStopPlugin struct

Methods

Name
Method

Returns

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

Returns

error
func (*retryStopPlugin) Start() error
{
	return nil
}
Stop
Method

Returns

error
func (*retryStopPlugin) Stop() error
{
	p.stopCalls++
	if p.stopCalls == 1 {
		return errors.New("temporary stop failure")
	}
	return nil
}

Fields

Name Type Description
name string
stopCalls int
F
function

TestRegistryPreservesLifecycleOrder

Parameters

core/plugin/registry_test.go:55-82
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)
	}
	if errs := registry.StopAll(); len(errs) != 0 {
		t.Fatalf("second StopAll() errors = %v", errs)
	}
	if !reflect.DeepEqual(order, want) {
		t.Fatalf("second StopAll() repeated stops: %v", order)
	}
}
F
function

TestRegistryRejectsDuplicatesAndSupportsUnregister

Parameters

core/plugin/registry_test.go:84-98
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

TestRegistryRollsBackStartedPluginsOnFailure

Parameters

core/plugin/registry_test.go:100-127
func TestRegistryRollsBackStartedPluginsOnFailure(t *testing.T)

{
	order := []string{}
	registry := plugin.NewRegistry()
	if err := registry.Register(testPlugin{name: "first", order: &order}); err != nil {
		t.Fatal(err)
	}
	if err := registry.Register(testPlugin{
		name:      "second",
		order:     &order,
		startFail: errors.New("start failed"),
	}); err != nil {
		t.Fatal(err)
	}

	if errs := registry.StartAll(); len(errs) != 1 {
		t.Fatalf("StartAll() errors = %v, want one", errs)
	}
	want := []string{"start:first", "start:second", "stop:first"}
	if !reflect.DeepEqual(order, want) {
		t.Fatalf("lifecycle order = %v, want %v", order, want)
	}
	if errs := registry.StopAll(); len(errs) != 0 {
		t.Fatalf("StopAll() after rollback errors = %v", errs)
	}
	if !reflect.DeepEqual(order, want) {
		t.Fatalf("StopAll() repeated rollback stops: %v", order)
	}
}
F
function

TestRegistryRetriesFailedRollbackStop

Parameters

core/plugin/registry_test.go:129-150
func TestRegistryRetriesFailedRollbackStop(t *testing.T)

{
	first := &retryStopPlugin{name: "first"}
	registry := plugin.NewRegistry()
	if err := registry.Register(first); err != nil {
		t.Fatal(err)
	}
	if err := registry.Register(testPlugin{
		name:      "second",
		startFail: errors.New("start failed"),
	}); err != nil {
		t.Fatal(err)
	}
	if errors := registry.StartAll(); len(errors) != 2 {
		t.Fatalf("StartAll() errors = %v, want start and stop errors", errors)
	}
	if errors := registry.StopAll(); len(errors) != 0 {
		t.Fatalf("StopAll() retry errors = %v", errors)
	}
	if first.stopCalls != 2 {
		t.Fatalf("Stop() calls = %d, want 2", first.stopCalls)
	}
}
F
function

TestDiscoverFromValuesRegistersPlugins

Parameters

core/plugin/registry_test.go:152-164
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

core/plugin/registry_test.go:166-185
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)
	}
}