<commit_before>package hostdb

import (
	// "crypto/rand"
	"path/filepath"
	"testing"

	// "github.com/NebulousLabs/Sia/build"
	"github.com/NebulousLabs/Sia/modules"
	// "github.com/NebulousLabs/Sia/modules/gateway"
	// "github.com/NebulousLabs/Sia/types"
)

// quitAfterLoadDeps will quit startup in newHostDB
type quitAfterLoadDeps struct {
	prodDependencies
}

// Send a disrupt signal to the quitAfterLoad codebreak.
func (quitAfterLoadDeps) disrupt(s string) bool {
	if s == "quitAfterLoad" {
		return true
	}
	return false
}

// TestSaveLoad tests that the hostdb can save and load itself.
//
// TODO: By extending the hdbTester and adding some helper functions, we can
// eliminate the necessary disruption by adding real hosts + blocks instead of
// fake ones.
func TestSaveLoad(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	hdbt, err := newHDBTester("TestSaveLoad")
	if err != nil {
		t.Fatal(err)
	}

	// Add fake hosts and a fake consensus change. The fake consensus change
	// would normally be detected and routed around, but we stunt the loading
	// process to only load the persistent fields.
	var host1, host2, host3 modules.HostDBEntry
	host1.PublicKey.Key = []byte("foo")
	host2.PublicKey.Key = []byte("bar")
	host3.PublicKey.Key = []byte("baz")
	hdbt.hdb.hostTree.Insert(host1)
	hdbt.hdb.hostTree.Insert(host2)
	hdbt.hdb.hostTree.Insert(host3)
	hdbt.hdb.lastChange = modules.ConsensusChangeID{1, 2, 3}
	stashedLC := hdbt.hdb.lastChange

	// Save, close, and reload.
	err = hdbt.hdb.save()
	if err != nil {
		t.Fatal(err)
	}
	err = hdbt.hdb.Close()
	if err != nil {
		t.Fatal(err)
	}
	hdbt.hdb, err = newHostDB(hdbt.gateway, hdbt.cs, filepath.Join(hdbt.persistDir, modules.RenterDir), quitAfterLoadDeps{})
	if err != nil {
		t.Fatal(err)
	}

	// Last change should have been reloaded.
	if hdbt.hdb.lastChange != stashedLC {
		t.Error("wrong consensus change ID was loaded:", hdbt.hdb.lastChange)
	}<commit_msg>fix hostdb test race conditions<commit_after>package hostdb

import (
	// "crypto/rand"
	"path/filepath"
	"testing"

	// "github.com/NebulousLabs/Sia/build"
	"github.com/NebulousLabs/Sia/modules"
	// "github.com/NebulousLabs/Sia/modules/gateway"
	// "github.com/NebulousLabs/Sia/types"
)

// quitAfterLoadDeps will quit startup in newHostDB
type quitAfterLoadDeps struct {
	prodDependencies
}

// Send a disrupt signal to the quitAfterLoad codebreak.
func (quitAfterLoadDeps) disrupt(s string) bool {
	if s == "quitAfterLoad" {
		return true
	}
	return false
}

// TestSaveLoad tests that the hostdb can save and load itself.
//
// TODO: By extending the hdbTester and adding some helper functions, we can
// eliminate the necessary disruption by adding real hosts + blocks instead of
// fake ones.
func TestSaveLoad(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	hdbt, err := newHDBTester("TestSaveLoad")
	if err != nil {
		t.Fatal(err)
	}

	// Add fake hosts and a fake consensus change. The fake consensus change
	// would normally be detected and routed around, but we stunt the loading
	// process to only load the persistent fields.
	var host1, host2, host3 modules.HostDBEntry
	host1.PublicKey.Key = []byte("foo")
	host2.PublicKey.Key = []byte("bar")
	host3.PublicKey.Key = []byte("baz")
	hdbt.hdb.hostTree.Insert(host1)
	hdbt.hdb.hostTree.Insert(host2)
	hdbt.hdb.hostTree.Insert(host3)

	// Save, close, and reload.
	hdbt.hdb.mu.Lock()
	hdbt.hdb.lastChange = modules.ConsensusChangeID{1, 2, 3}
	stashedLC := hdbt.hdb.lastChange
	err = hdbt.hdb.save()
	hdbt.hdb.mu.Unlock()
	if err != nil {
		t.Fatal(err)
	}
	err = hdbt.hdb.Close()
	if err != nil {
		t.Fatal(err)
	}
	hdbt.hdb, err = newHostDB(hdbt.gateway, hdbt.cs, filepath.Join(hdbt.persistDir, modules.RenterDir), quitAfterLoadDeps{})
	if err != nil {
		t.Fatal(err)
	}

	// Last change should have been reloaded.
	hdbt.hdb.mu.Lock()
	lastChange := hdbt.hdb.lastChange
	hdbt.hdb.mu.Unlock()
	if lastChange != stashedLC {
		t.Error("wrong consensus change ID was loaded:", hdbt.hdb.lastChange)
	}
{'fraction_non_alphanumeric': 0.1284722222222222, 'fraction_numerical': 0.005952380952380952, 'mean_word_length': 3.8531889290012034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1066080', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1491, 'n_words': 376}