-
Notifications
You must be signed in to change notification settings - Fork 5
/
testutils.go
78 lines (62 loc) · 1.63 KB
/
testutils.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
package testutils
import (
"context"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/google/uuid"
"github.com/c4t-but-s4d/neo/v2/pkg/joblogger"
)
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand = rand.New(rand.NewSource(1337))
func LessString(v1, v2 string) bool {
return v1 < v2
}
func CanceledContext() context.Context {
ctx, c := context.WithCancel(context.Background())
defer c()
return ctx
}
func TimedOutContext() context.Context {
ctx, c := context.WithTimeout(context.Background(), time.Second*0)
defer c()
return ctx
}
func RandomStringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func RandomString(length int) string {
return RandomStringWithCharset(length, charset)
}
func RandomInt(min, max int) int {
n := seededRand.Intn(max - min)
return min + n
}
func RandomIP() string {
gen := func() int {
return RandomInt(0, 256)
}
return fmt.Sprintf("%d.%d.%d.%d", gen(), gen(), gen(), gen())
}
func MetricsNamespace() string {
return fmt.Sprintf("test_%s", uuid.NewString()[:8])
}
type HTTPRequestChecker func(*testing.T, *http.Request)
func NewCheckedTestServer(t *testing.T, checker HTTPRequestChecker) *httptest.Server {
t.Helper()
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
checker(t, r)
w.WriteHeader(http.StatusOK)
}))
return s
}
func DummyJobLogger(name, team string) *joblogger.JobLogger {
return joblogger.New(name, 1, team, joblogger.NewDummySender())
}