From faa6a58300e2073ff5204a12548f445bac8ae201 Mon Sep 17 00:00:00 2001 From: Hans Moog <hm@mkjc.net> Date: Thu, 11 Jul 2019 16:08:41 +0200 Subject: [PATCH] Feat: added additional methods to node for easier testing --- packages/daemon/daemon.go | 24 ++++++++++++++++- packages/node/node.go | 27 +++++++++++++++++++ packages/node/plugin.go | 13 --------- .../transactionspammer/transactionspammer.go | 27 +++++++++++++++++-- .../bundleprocessor/bundleprocessor_test.go | 8 +++++- plugins/tangle/bundle.go | 2 +- plugins/tangle/solidifier_test.go | 14 ++++++---- 7 files changed, 92 insertions(+), 23 deletions(-) diff --git a/packages/daemon/daemon.go b/packages/daemon/daemon.go index d6961424..2e6aaadd 100644 --- a/packages/daemon/daemon.go +++ b/packages/daemon/daemon.go @@ -58,7 +58,7 @@ func BackgroundWorker(name string, handler func()) { lock.Unlock() } -func Run() { +func Start() { if !running { lock.Lock() @@ -76,6 +76,10 @@ func Run() { lock.Unlock() } +} + +func Run() { + Start() wg.Wait() } @@ -96,6 +100,24 @@ func Shutdown() { } } +func ShutdownAndWait() { + if running { + lock.Lock() + + if running { + close(ShutdownSignal) + + running = false + + Events.Shutdown.Trigger() + } + + lock.Unlock() + } + + wg.Wait() +} + func IsRunning() bool { return running } diff --git a/packages/node/node.go b/packages/node/node.go index bedaa490..9f86209f 100644 --- a/packages/node/node.go +++ b/packages/node/node.go @@ -28,6 +28,13 @@ func Load(plugins ...*Plugin) *Node { return node } +func Start(plugins ...*Plugin) *Node { + node := Load(plugins...) + node.Start() + + return node +} + func Run(plugins ...*Plugin) *Node { node := Load(plugins...) node.Run() @@ -35,6 +42,10 @@ func Run(plugins ...*Plugin) *Node { return node } +func Shutdown() { + daemon.ShutdownAndWait() +} + func (node *Node) AddLogger(logger *Logger) { node.loggers = append(node.loggers, logger) } @@ -106,6 +117,22 @@ func (node *Node) Load(plugins ...*Plugin) { } } +func (node *Node) Start() { + node.LogInfo("Node", "Executing plugins ...") + + if len(node.loadedPlugins) >= 1 { + for _, plugin := range node.loadedPlugins { + plugin.Events.Run.Trigger(plugin) + + node.LogSuccess("Node", "Starting Plugin: "+plugin.Name+" ... done") + } + } + + node.LogSuccess("Node", "Starting background workers ...") + + daemon.Start() +} + func (node *Node) Run() { node.LogInfo("Node", "Executing plugins ...") diff --git a/packages/node/plugin.go b/packages/node/plugin.go index 1a6cb527..a98a22be 100644 --- a/packages/node/plugin.go +++ b/packages/node/plugin.go @@ -55,16 +55,3 @@ func (plugin *Plugin) LogFailure(message string) { func (plugin *Plugin) LogDebug(message string) { plugin.Node.LogDebug(plugin.Name, message) } - -var TestNode = &Node{ - loggers: make([]*Logger, 0), - wg: &sync.WaitGroup{}, - loadedPlugins: make([]*Plugin, 0), -} - -func (plugin *Plugin) InitTest() { - plugin.Node = TestNode - - plugin.Events.Configure.Trigger(plugin) - plugin.Events.Run.Trigger(plugin) -} diff --git a/packages/transactionspammer/transactionspammer.go b/packages/transactionspammer/transactionspammer.go index c59c6f54..777671a2 100644 --- a/packages/transactionspammer/transactionspammer.go +++ b/packages/transactionspammer/transactionspammer.go @@ -16,7 +16,9 @@ var startMutex sync.Mutex var shutdownSignal chan int -func Start(tps int64) { +var sentCounter = uint(0) + +func Start(tps uint) { startMutex.Lock() if !spamming { @@ -26,7 +28,6 @@ func Start(tps int64) { daemon.BackgroundWorker("Transaction Spammer", func() { for { start := time.Now() - sentCounter := int64(0) totalSentCounter := int64(0) for { @@ -81,3 +82,25 @@ func Stop() { startMutex.Unlock() } + +func GenerateBundle(bundleLength int) (result []*value_transaction.ValueTransaction) { + result = make([]*value_transaction.ValueTransaction, bundleLength) + + branch := tipselection.GetRandomTip() + trunk := tipselection.GetRandomTip() + + for i := 0; i < bundleLength; i++ { + tx := value_transaction.New() + tx.SetTail(i == 0) + tx.SetHead(i == bundleLength - 1) + tx.SetTimestamp(sentCounter) + tx.SetBranchTransactionHash(branch) + tx.SetTrunkTransactionHash(trunk) + + result[i] = tx + + trunk = tx.GetHash() + } + + return result +} diff --git a/plugins/bundleprocessor/bundleprocessor_test.go b/plugins/bundleprocessor/bundleprocessor_test.go index ee4cc3d4..a4853715 100644 --- a/plugins/bundleprocessor/bundleprocessor_test.go +++ b/plugins/bundleprocessor/bundleprocessor_test.go @@ -4,6 +4,8 @@ import ( "fmt" "testing" + "github.com/iotaledger/goshimmer/packages/node" + "github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/goshimmer/packages/model/bundle" "github.com/iotaledger/goshimmer/packages/model/value_transaction" @@ -13,7 +15,8 @@ import ( ) func TestProcessSolidBundleHead(t *testing.T) { - tangle.PLUGIN.InitTest() + // start a test node + node.Start(tangle.PLUGIN, PLUGIN) tx := value_transaction.New() tx.SetTail(true) @@ -39,4 +42,7 @@ func TestProcessSolidBundleHead(t *testing.T) { assert.Equal(t, result.GetHash(), trinary.Trytes("UFWJYEWKMEQDNSQUCUWBGOFRHVBGHVVYEZCLCGRDTRQSMAFALTIPMJEEYFDPMQCNJWLXUWFMBZGHQRO99"), "invalid bundle hash") assert.Equal(t, result.IsValueBundle(), true, "invalid value bundle status") } + + // shutdown test node + node.Shutdown() } diff --git a/plugins/tangle/bundle.go b/plugins/tangle/bundle.go index 759041c4..4907c44c 100644 --- a/plugins/tangle/bundle.go +++ b/plugins/tangle/bundle.go @@ -81,7 +81,7 @@ const ( var bundleDatabase database.Database func configureBundleDatabase(plugin *node.Plugin) { - if db, err := database.Get("bundle"); err != nil { + if db, err := database.Get("bundles"); err != nil { panic(err) } else { bundleDatabase = db diff --git a/plugins/tangle/solidifier_test.go b/plugins/tangle/solidifier_test.go index c7b04e9f..9db5e7c8 100644 --- a/plugins/tangle/solidifier_test.go +++ b/plugins/tangle/solidifier_test.go @@ -6,16 +6,17 @@ import ( "github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/goshimmer/packages/model/value_transaction" + "github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/plugins/gossip" "github.com/iotaledger/iota.go/trinary" ) func TestSolidifier(t *testing.T) { - // initialize plugin - configureTransactionDatabase(nil) - configureTransactionMetaDataDatabase(nil) - configureApproversDatabase(nil) - configureSolidifier(nil) + // show all error messages for tests + *node.LOG_LEVEL.Value = node.LOG_LEVEL_DEBUG + + // start a test node + node.Start(PLUGIN) // create transactions and chain them together transaction1 := value_transaction.New() @@ -42,4 +43,7 @@ func TestSolidifier(t *testing.T) { // wait until all are solid wg.Wait() + + // shutdown test node + node.Shutdown() } -- GitLab