This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathutils_internal_test.go
98 lines (86 loc) · 2.48 KB
/
utils_internal_test.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package pilosa
import (
"fmt"
"testing"
"time"
"github.com/featurebasedb/featurebase/v3/disco"
"github.com/featurebasedb/featurebase/v3/etcd"
pnet "github.com/featurebasedb/featurebase/v3/net"
"github.com/featurebasedb/featurebase/v3/testhook"
)
// utilities used by tests
// NewTestCluster returns a cluster with n nodes and uses a mod-based hasher.
func NewTestCluster(tb testing.TB, n int) *cluster {
if n > 1 && !etcd.AllowCluster() {
tb.Skipf("cluster size %d not supported in unclustered mode", n)
}
path, err := testhook.TempDir(tb, "pilosa-cluster-")
if err != nil {
panic(err)
}
availableShardFileFlushDuration.Set(100 * time.Millisecond)
c := newCluster()
c.ReplicaN = 1
c.Hasher = &disco.Jmphasher{}
c.Path = path
nodes := make([]*disco.Node, 0, n)
for i := 0; i < n; i++ {
nodes = append(nodes, &disco.Node{
ID: fmt.Sprintf("node%d", i),
URI: NewTestURI("http", fmt.Sprintf("host%d", i), uint16(0)),
})
}
c.noder = disco.NewLocalNoder(nodes)
cNodes := c.noder.Nodes()
c.Node = cNodes[0]
return c
}
// NewTestURI is a test URI creator that intentionally swallows errors.
func NewTestURI(scheme, host string, port uint16) pnet.URI {
uri := pnet.DefaultURI()
_ = uri.SetScheme(scheme)
_ = uri.SetHost(host)
uri.SetPort(port)
return *uri
}
func NewTestURIFromHostPort(host string, port uint16) pnet.URI {
uri := pnet.DefaultURI()
_ = uri.SetHost(host)
uri.SetPort(port)
return *uri
}
func TestReplaceFirstFromBack(t *testing.T) {
for name, test := range map[string]struct {
input string
exp string
toReplace string
replacement string
}{
"url": {
input: "https://login.microsoftonline.com/4a137d66-d161-4ae4-b1e6-07e9920874b8/oauth2/v2.0/authorize",
exp: "https://login.microsoftonline.com/4a137d66-d161-4ae4-b1e6-07e9920874b8/oauth2/v2.0/devicecode",
toReplace: "authorize",
replacement: "devicecode",
},
"unicode": {
input: "那不是兽人号角",
exp: "那是一只兽人号角",
toReplace: "不是",
replacement: "是一只",
},
"multiple": {
input: "cowscowscowscowscows",
exp: "cowscowscowscowscats",
toReplace: "cows",
replacement: "cats",
},
} {
t.Run(name, func(t *testing.T) {
if got := ReplaceFirstFromBack(test.input, test.toReplace, test.replacement); got != test.exp {
t.Fatalf("expected %v, got %v", test.exp, got)
}
})
}
}