Skip to content
Snippets Groups Projects
Commit faa6a583 authored by Hans Moog's avatar Hans Moog
Browse files

Feat: added additional methods to node for easier testing

parent 4bfcb797
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
......@@ -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 ...")
......
......@@ -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)
}
......@@ -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
}
......@@ -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()
}
......@@ -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
......
......@@ -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()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment