From 40377543bf5a6fd2cfcdf3f9fdc246a9c74f00f7 Mon Sep 17 00:00:00 2001 From: Austin Roberts Date: Wed, 25 Aug 2021 14:00:27 -0500 Subject: [PATCH] Add plugin hook tester --- core/blockchain_hooks_test.go | 22 ++++++++++++++++++++++ plugins/hooktester.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 core/blockchain_hooks_test.go create mode 100644 plugins/hooktester.go diff --git a/core/blockchain_hooks_test.go b/core/blockchain_hooks_test.go new file mode 100644 index 000000000..27c9253b8 --- /dev/null +++ b/core/blockchain_hooks_test.go @@ -0,0 +1,22 @@ +package core + +import ( + "testing" + "github.com/ethereum/go-ethereum/plugins" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/common" +) + + +func TestReorgLongHeadersHook(t *testing.T) { + invoked := false + done := plugins.HookTester("NewHead", func(b *types.Block, h common.Hash, logs []*types.Log) { + // invoked = true + if b == nil { t.Errorf("Expected block to be non-nil") } + if h == (common.Hash{}) { t.Errorf("Expected hash to be non-empty") } + if len(logs) > 0 { t.Errorf("Expected some logs") } + }) + defer done() + testReorgLong(t, true) + if !invoked { t.Errorf("Expected plugin invocation")} +} diff --git a/plugins/hooktester.go b/plugins/hooktester.go new file mode 100644 index 000000000..ddf3bb1a3 --- /dev/null +++ b/plugins/hooktester.go @@ -0,0 +1,19 @@ +package plugins + + +// type PluginLoader struct{ +// Plugins []*plugin.Plugin +// Subcommands map[string]Subcommand +// Flags []*flag.FlagSet +// LookupCache map[string][]interface{} +// } + +func HookTester(name string, fn interface{}) func() { + oldDefault := DefaultPluginLoader + DefaultPluginLoader = &PluginLoader{ + LookupCache: map[string][]interface{}{ + name: []interface{}{fn}, + }, + } + return func() { DefaultPluginLoader = oldDefault } +}