txscript/hashcache_test: call rand.Seed once in init

This resolves the more fundamental flake in the unit tests noted in the
prior commit.

Because multiple unit tests call rand.Seed in parallel, it's possible
they can be executed with the same unix timestamp (in seconds). If the
second call happens between generating the hash cache and checking that
the cache doesn't contain a random txn, the random transaction is in
fact a duplicate of one generated earlier since the RNG state was reset.

To remedy, we initialize rand.Seed once in the init function.
This commit is contained in:
Conner Fromknecht 2021-02-02 13:24:27 -08:00
parent 1dd693480c
commit 5300a19d06
No known key found for this signature in database
GPG key ID: E7D737B67FA592C7

View file

@ -13,6 +13,10 @@ import (
"github.com/davecgh/go-spew/spew"
)
func init() {
rand.Seed(time.Now().Unix())
}
// genTestTx creates a random transaction for uses within test cases.
func genTestTx() (*wire.MsgTx, error) {
tx := wire.NewMsgTx(2)
@ -56,8 +60,6 @@ func genTestTx() (*wire.MsgTx, error) {
func TestHashCacheAddContainsHashes(t *testing.T) {
t.Parallel()
rand.Seed(time.Now().Unix())
cache := NewHashCache(10)
var err error
@ -109,8 +111,6 @@ func TestHashCacheAddContainsHashes(t *testing.T) {
func TestHashCacheAddGet(t *testing.T) {
t.Parallel()
rand.Seed(time.Now().Unix())
cache := NewHashCache(10)
// To start, we'll generate a random transaction and compute the set of
@ -144,8 +144,6 @@ func TestHashCacheAddGet(t *testing.T) {
func TestHashCachePurge(t *testing.T) {
t.Parallel()
rand.Seed(time.Now().Unix())
cache := NewHashCache(10)
var err error