swarm: codebase split from go-ethereum (#1405)
This commit is contained in:
committed by
Anton Evangelatov
parent
7a22da98b9
commit
b046760db1
65
testutil/file.go
Normal file
65
testutil/file.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TempFileWithContent is a helper function that creates a temp file that contains the following string content then closes the file handle
|
||||
// it returns the complete file path
|
||||
func TempFileWithContent(t *testing.T, content string) string {
|
||||
tempFile, err := ioutil.TempFile("", "swarm-temp-file")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(tempFile, strings.NewReader(content))
|
||||
if err != nil {
|
||||
os.RemoveAll(tempFile.Name())
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tempFile.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return tempFile.Name()
|
||||
}
|
||||
|
||||
// RandomBytes returns pseudo-random deterministic result
|
||||
// because test fails must be reproducible
|
||||
func RandomBytes(seed, length int) []byte {
|
||||
b := make([]byte, length)
|
||||
reader := rand.New(rand.NewSource(int64(seed)))
|
||||
for n := 0; n < length; {
|
||||
read, err := reader.Read(b[n:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
n += read
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func RandomReader(seed, length int) *bytes.Reader {
|
||||
return bytes.NewReader(RandomBytes(seed, length))
|
||||
}
|
26
testutil/norace.go
Normal file
26
testutil/norace.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2019 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// +build !race
|
||||
|
||||
package testutil
|
||||
|
||||
// RaceEnabled is true when -race flag is provided to the go tool. This const
|
||||
// might be used in tests to skip some cases as the race detector may increase
|
||||
// memory usage 5-10x and execution time by 2-20x. That might causes problems
|
||||
// on Travis. Please, use this flag sparingly and keep your unit tests
|
||||
// as light on resources as possible.
|
||||
const RaceEnabled = false
|
23
testutil/race.go
Normal file
23
testutil/race.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright 2019 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// +build race
|
||||
|
||||
package testutil
|
||||
|
||||
// RaceEnabled is true when -race flag is provided to the go tool.
|
||||
// See norace.go for more.
|
||||
const RaceEnabled = true
|
51
testutil/tag.go
Normal file
51
testutil/tag.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2019 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ethersphere/swarm/chunk"
|
||||
)
|
||||
|
||||
// CheckTag checks the first tag in the api struct to be in a certain state
|
||||
func CheckTag(t *testing.T, tag *chunk.Tag, split, stored, seen, total int64) {
|
||||
t.Helper()
|
||||
if tag == nil {
|
||||
t.Fatal("no tag found")
|
||||
}
|
||||
|
||||
tSplit := tag.Get(chunk.StateSplit)
|
||||
if tSplit != split {
|
||||
t.Fatalf("should have had split chunks, got %d want %d", tSplit, split)
|
||||
}
|
||||
|
||||
tSeen := tag.Get(chunk.StateSeen)
|
||||
if tSeen != seen {
|
||||
t.Fatalf("should have had seen chunks, got %d want %d", tSeen, seen)
|
||||
}
|
||||
|
||||
tStored := tag.Get(chunk.StateStored)
|
||||
if tStored != stored {
|
||||
t.Fatalf("mismatch stored chunks, got %d want %d", tStored, stored)
|
||||
}
|
||||
|
||||
tTotal := tag.Total()
|
||||
if tTotal != total {
|
||||
t.Fatalf("mismatch total chunks, got %d want %d", tTotal, total)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user