forked from ethersphere/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
80 lines (64 loc) · 1.74 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2023 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testutil
import (
"crypto/rand"
"io"
mrand "math/rand"
"reflect"
"testing"
"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/util/ioutil"
)
// RandBytes returns bytes slice of specified size filled with random values.
func RandBytes(tb testing.TB, size int) []byte {
tb.Helper()
buf := make([]byte, size)
n, err := rand.Read(buf)
if err != nil {
tb.Fatal(err)
}
if n != size {
tb.Fatalf("expected to read %d, got %d", size, n)
}
return buf
}
// RandBytesWithSeed returns bytes slice of specified size filled with random values generated using seed.
func RandBytesWithSeed(tb testing.TB, size int, seed int64) []byte {
tb.Helper()
buf := make([]byte, size)
r := mrand.New(mrand.NewSource(seed))
n, err := io.ReadFull(r, buf)
if err != nil {
tb.Fatal(err)
}
if n != size {
tb.Fatalf("expected to read %d, got %d", size, n)
}
return buf
}
// CleanupCloser adds Cleanup function to Test which will close supplied Closers.
func CleanupCloser(t *testing.T, closers ...io.Closer) {
t.Helper()
t.Cleanup(func() {
for _, c := range closers {
if c == nil {
continue
}
if err := c.Close(); err != nil {
t.Fatalf("failed to gracefully close %s: %s", reflect.TypeOf(c), err)
}
}
})
}
// NewLogger returns a new log.Logger that uses t.Log method
// as the log sink. It is particularly useful for debugging tests.
func NewLogger(t *testing.T) log.Logger {
t.Helper()
testWriter := ioutil.WriterFunc(func(p []byte) (int, error) {
t.Log(string(p))
return len(p), nil
})
return log.NewLogger(t.Name(), log.WithSink(testWriter))
}