diff --git a/engine/NetStart.go b/engine/NetStart.go index 32176f7e3e..f76697ec70 100644 --- a/engine/NetStart.go +++ b/engine/NetStart.go @@ -720,3 +720,8 @@ func AddNode() { AddSimPeer(fnodes, i, i-1) // KLUDGE peer w/ only last node startServer(i, fnodes[i], true) } + +// ResetFNodes clears the fnode slice for unit tests +func ResetFNodes() { + fnodes = nil +} diff --git a/simTest/EntriesBeforeChain_test.go b/simTest/EntriesBeforeChain_test.go index 042d516afc..265b698891 100644 --- a/simTest/EntriesBeforeChain_test.go +++ b/simTest/EntriesBeforeChain_test.go @@ -13,7 +13,7 @@ import ( ) func TestEntriesBeforeChain(t *testing.T) { - + ResetSimHome(t) encode := func(s string) []byte { b := bytes.Buffer{} b.WriteString(s) diff --git a/simTest/EntryBatch_test.go b/simTest/EntryBatch_test.go index 3039bdaeb3..4f0f475915 100644 --- a/simTest/EntryBatch_test.go +++ b/simTest/EntryBatch_test.go @@ -14,7 +14,7 @@ import ( // this applies chain & entry creation in 'proper' chronological order func TestEntryBatch(t *testing.T) { - + ResetSimHome(t) encode := func(s string) []byte { b := bytes.Buffer{} b.WriteString(s) diff --git a/simTest/HoldingRebound_test.go b/simTest/HoldingRebound_test.go index 12c711a066..a431d14a32 100644 --- a/simTest/HoldingRebound_test.go +++ b/simTest/HoldingRebound_test.go @@ -14,6 +14,7 @@ import ( ) func TestHoldingRebound(t *testing.T) { + ResetSimHome(t) encode := func(s string) []byte { b := bytes.Buffer{} b.WriteString(s) diff --git a/simTest/MessageFilteringInput_test.go b/simTest/MessageFilteringInput_test.go index 776fda1bbb..e47fa4796e 100644 --- a/simTest/MessageFilteringInput_test.go +++ b/simTest/MessageFilteringInput_test.go @@ -9,7 +9,7 @@ import ( ) func TestFilterAPIInput(t *testing.T) { - + ResetSimHome(t) state0 := SetupSim("LLLAF", map[string]string{}, 25, 1, 1, t) RunCmd("1") diff --git a/simTest/MessageFilteringOutput_test.go b/simTest/MessageFilteringOutput_test.go index f702025914..0803bf0a24 100644 --- a/simTest/MessageFilteringOutput_test.go +++ b/simTest/MessageFilteringOutput_test.go @@ -9,7 +9,7 @@ import ( ) func TestFilterAPIOutput(t *testing.T) { - + ResetSimHome(t) state0 := SetupSim("LLLLLAAF", map[string]string{}, 25, 1, 1, t) RunCmd("1") diff --git a/simTest/PermFCTBalancesAfterMin9Election_test.go b/simTest/PermFCTBalancesAfterMin9Election_test.go index 720d5b141f..6e37b935c8 100644 --- a/simTest/PermFCTBalancesAfterMin9Election_test.go +++ b/simTest/PermFCTBalancesAfterMin9Election_test.go @@ -22,6 +22,7 @@ func createDepositAddresses() { } func TestPermFCTBalancesAfterMin9Election(t *testing.T) { + ResetSimHome(t) createDepositAddresses() state0 := SetupSim("LLAL", map[string]string{"--debuglog": "", "--faulttimeout": "10"}, 10, 1, 1, t) diff --git a/simTest/SetupANetwork_test.go b/simTest/SetupANetwork_test.go index 5ea4a83dca..f6aef7ada8 100644 --- a/simTest/SetupANetwork_test.go +++ b/simTest/SetupANetwork_test.go @@ -1,16 +1,15 @@ package simtest -import "time" - import ( "testing" + "time" . "github.com/FactomProject/factomd/engine" . "github.com/FactomProject/factomd/testHelper" ) func TestSetupANetwork(t *testing.T) { - + ResetSimHome(t) state0 := SetupSim("LLLLAAAFFF", map[string]string{"--debuglog": ""}, 20, 0, 0, t) RunCmd("9") // Puts the focus on node 9 diff --git a/simTest/SimWallet_test.go b/simTest/SimWallet_test.go index 7b2c204d36..72940c932d 100644 --- a/simTest/SimWallet_test.go +++ b/simTest/SimWallet_test.go @@ -9,7 +9,7 @@ import ( ) func TestSimWallet(t *testing.T) { - + ResetSimHome(t) a := AccountFromFctSecret("Fs31kMMKBSCDwa7tSEzjQ4EfGeXARdUS22oBEJKNSJWbLAMTsEHr") b := AccountFromFctSecret("Fs2BNvoDgSoGJpWg4PvRUxqvLE28CQexp5FZM9X5qU6QvzFBUn6D") diff --git a/simTest/factomd_test.go b/simTest/factomd_test.go index 50998c2291..5926ee68b9 100644 --- a/simTest/factomd_test.go +++ b/simTest/factomd_test.go @@ -32,6 +32,7 @@ func TestOne(t *testing.T) { return } state.MMR_enable = false // No MMR for you! + ResetSimHome(t) RanSimTest = true @@ -53,6 +54,7 @@ func TestDualElections(t *testing.T) { state.MMR_enable = false // No MMR for you! RanSimTest = true + ResetSimHome(t) // 01234567 state0 := SetupSim("LALLLALFFLLFFFF", map[string]string{"--debuglog": ".", "--blktime": "20"}, 12, 0, 0, t) @@ -78,6 +80,7 @@ func TestLoad(t *testing.T) { } RanSimTest = true + ResetSimHome(t) // use a tree so the messages get reordered state0 := SetupSim("LLLLFFFF", map[string]string{"--debuglog": ".", "--blktime": "30"}, 15, 0, 0, t) @@ -101,6 +104,7 @@ func TestErr(t *testing.T) { } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LF", map[string]string{"--debuglog": ".", "--db": "LDB", "--controlpanelsetting": "readwrite", "--network": "LOCAL", "--fastsaverate": "4", "--checkheads": "false", "--net": "alot", "--blktime": "15", "--faulttimeout": "120000", "--enablenet": "false", "--startdelay": "1"}, @@ -122,6 +126,7 @@ func TestCatchup(t *testing.T) { } RanSimTest = true + ResetSimHome(t) // use a tree so the messages get reordered state0 := SetupSim("LF", map[string]string{}, 15, 0, 0, t) @@ -153,6 +158,7 @@ func TestTXTimestampsAndBlocks(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) go RunCmd("Re") // Turn on tight allocation of EC as soon as the simulator is up and running state0 := SetupSim("LLLAAAFFF", map[string]string{}, 24, 0, 0, t) @@ -173,6 +179,7 @@ func TestLoad2(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) // use tree node setup so messages get reordered go RunCmd("Re") // Turn on tight allocation of EC as soon as the simulator is up and running @@ -219,6 +226,7 @@ func TestLoadScrambled(t *testing.T) { }() RanSimTest = true + ResetSimHome(t) // use a tree so the messages get reordered state0 := SetupSim("LLFFFFFF", map[string]string{"--net": "tree"}, 32, 0, 0, t) @@ -241,6 +249,7 @@ func TestMinute9Election(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) // use a tree so the messages get reordered state0 := SetupSim("LLAL", map[string]string{"--net": "line"}, 10, 1, 1, t) @@ -264,6 +273,7 @@ func TestMakeALeader(t *testing.T) { } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LF", map[string]string{}, 5, 0, 0, t) RunCmd("g1") @@ -371,6 +381,7 @@ func TestAnElection(t *testing.T) { } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLLAAF", map[string]string{"--blktime": "15"}, 9, 1, 1, t) @@ -386,6 +397,7 @@ func TestAnElection(t *testing.T) { // wait for the election WaitMinutes(state0, 2) //bring him back + RunCmd("2") RunCmd("x") // wait for him to update via dbstate and become an audit @@ -412,6 +424,7 @@ func TestDBsigEOMElection(t *testing.T) { } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLLLLAAF", map[string]string{}, 9, 4, 4, t) @@ -474,6 +487,7 @@ func TestMultiple2Election(t *testing.T) { } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLLLLAAF", map[string]string{}, 7, 2, 2, t) @@ -502,6 +516,7 @@ func TestMultiple3Election(t *testing.T) { } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLLLLLLAAAAF", map[string]string{}, 9, 3, 3, t) @@ -532,6 +547,7 @@ func TestSimCtrl(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) type walletcallHelper struct { Status string `json:"status"` @@ -598,6 +614,7 @@ func TestMultipleFTAccountsAPI(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) // only have one leader because if you are not the leader responcible for the FCT transaction then // you will return transACK before teh balance is updated which will make thsi test fail. state0 := SetupSim("LAF", map[string]string{}, 6, 0, 0, t) @@ -789,6 +806,7 @@ func TestMultipleECAccountsAPI(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) // only have one leader because if you are not the leader responcible for the FCT transaction then // you will return transACK before teh balance is updated which will make thsi test fail. @@ -995,6 +1013,7 @@ func TestDBSigElection(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLLAF", map[string]string{"--faulttimeout": "10"}, 8, 1, 1, t) @@ -1025,6 +1044,7 @@ func TestCoinbaseCancel(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LFFFFF", map[string]string{"-blktime": "5"}, 30, 0, 0, t) // Make it quicker @@ -1164,6 +1184,7 @@ func TestElection9(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLAL", map[string]string{"--debuglog": "", "--faulttimeout": "10"}, 8, 1, 1, t) StatusEveryMinute(state0) @@ -1195,6 +1216,7 @@ func TestBadDBStateUnderflow(t *testing.T) { } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LF", map[string]string{}, 6, 0, 0, t) RunCmd("g1") WaitBlocks(state0, 2) @@ -1229,6 +1251,7 @@ func TestFactoidDBState(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LAF", map[string]string{"--faulttimeout": "10", "--blktime": "5"}, 120, 0, 0, t) WaitBlocks(state0, 1) @@ -1257,6 +1280,7 @@ func TestNoMMR(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLLAAFFFFF", map[string]string{}, 10, 0, 0, t) state.MMR_enable = false // turn off MMR processing @@ -1273,6 +1297,7 @@ func TestDBStateCatchup(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LFF", map[string]string{}, 100, 0, 0, t) state.MMR_enable = false // turn off MMR processing @@ -1302,6 +1327,7 @@ func TestDBState(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) state0 := SetupSim("LLLFFFF", map[string]string{"--net": "line"}, 100, 0, 0, t) state1 := GetFnodes()[1].State @@ -1333,6 +1359,7 @@ func TestCatchupEveryMinute(t *testing.T) { } RanSimTest = true + ResetSimHome(t) // 01234567890 state0 := SetupSim("LFFFFFFFFFF", map[string]string{"--debuglog": ".", "--blktime": "6"}, 20, 1, 1, t) @@ -1369,6 +1396,7 @@ func TestDebugLocation(t *testing.T) { return } RanSimTest = true + ResetSimHome(t) tempdir := os.TempDir() + string(os.PathSeparator) + "logs" + string(os.PathSeparator) // get os agnostic path to the temp directory diff --git a/testHelper/simulation.go b/testHelper/simulation.go index bafd67f6d3..ec35a09e40 100644 --- a/testHelper/simulation.go +++ b/testHelper/simulation.go @@ -24,8 +24,6 @@ import ( "github.com/stretchr/testify/assert" ) -var par = globals.FactomParams{} - var quit = make(chan struct{}) var ExpectedHeight, Leaders, Audits, Followers int var startTime, endTime time.Time @@ -133,6 +131,7 @@ func StartPeer(CmdLineOptions map[string]string) *state.State { // this is useful for creating scripts that will start/stop a simulation outside of the context of a unit test // this allows for consistent tweaking of a simulation to induce load add message loss or adjust timing func StartSim(nodeCount int, UserAddedOptions map[string]string) *state.State { + quit = make(chan struct{}) UserAddedOptions["--count"] = fmt.Sprintf("%v", nodeCount) params := optionsToParams(UserAddedOptions) return engine.Factomd(params, false).(*state.State) @@ -618,11 +617,16 @@ func GetLongTestHome(t *testing.T) string { // remove files from a home dir and remake .factom config dir func ResetTestHome(homeDir string, t *testing.T) { t.Logf("Removing old test run in %s", homeDir) - os.RemoveAll(homeDir) - os.MkdirAll(homeDir+"/.factom/m2", 0755) + if err := os.RemoveAll(homeDir); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(homeDir+"/.factom/m2", 0755); err != nil { + t.Fatal(err) + } } func ResetSimHome(t *testing.T) string { + engine.ResetFNodes() h := GetSimTestHome(t) ResetTestHome(h, t) return h