This repository has been archived by the owner on Feb 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
idpot_test.go
217 lines (185 loc) · 4.46 KB
/
idpot_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package idpot
import (
"crypto/sha1"
"fmt"
"github.com/lestrrat/go-idpot/client"
"github.com/lestrrat/go-tcptest"
"github.com/lestrrat/go-test-mysqld"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
buildCommandLineTools()
// Create a mysqld instance for testing
}
func randomName() string {
h := sha1.New()
io.WriteString(h,
fmt.Sprintf(
"idpot.test.%d.%d",
os.Getpid(),
time.Now().UnixNano(),
),
)
return fmt.Sprintf("%x", h.Sum(nil))
}
func buildCommandLineTools() {
// compile command
cmd := exec.Command("go", "build", "-o", "bin/idpot-server", "cli/idpot-server/idpot-server.go")
err := cmd.Run()
if err != nil {
panic(fmt.Sprintf("Failed to compile idpot-server: %s", err))
}
// Must add it to PATH so we can invoke it
os.Setenv("PATH",
strings.Join([]string{
"bin",
os.Getenv("PATH"),
}, ":"),
)
}
func getMaxFetches() int {
const defaultMaxFetches int = 10000
maxFetchesStr := os.Getenv("IDPOT_MAX_FETCHES")
if maxFetchesStr == "" {
return defaultMaxFetches
}
n, err := strconv.ParseInt(maxFetchesStr, 10, 64)
if err != nil {
return defaultMaxFetches
}
return int(n)
}
func TestServer(t *testing.T) {
mysqld, err := mysqltest.NewMysqld(nil)
if err != nil {
t.Fatalf("Failed to start mysqld: %s", err)
}
defer mysqld.Stop()
fullpath, err := exec.LookPath("idpot-server")
if err != nil {
t.Skipf("Could not find idpot-server in PATH")
}
// Create a config file to be used by idpot-server
dir, err := ioutil.TempDir("", "idpot-test")
if err != nil {
t.Fatalf("Failed to create temporary directory: %s", err)
}
defer os.RemoveAll(dir)
cfgfile, err := ioutil.TempFile(dir, "idpot.gcfg")
if err != nil {
t.Fatalf("Failed to create temp file: %s", err)
}
io.WriteString(cfgfile, fmt.Sprintf(
`
[Server]
LogFile=idpot-%%Y%%m%%d-access_log
LogLinkName=idpot-access_log
[Mysql]
ConnectString=%s
`,
mysqld.Datasource("test", "", "", 0),
))
cfgfile.Close()
var cmd *exec.Cmd
start := func(port int) {
numprocs := runtime.GOMAXPROCS(-1)
os.Setenv("GOMAXPROCS", fmt.Sprintf("%d", numprocs))
cmd = exec.Command(
fullpath,
"--listen", fmt.Sprintf("127.0.0.1:%d", port),
"--config", cfgfile.Name(),
)
t.Logf("Starting command %v", cmd.Args)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
out, _ := cmd.CombinedOutput()
t.Logf(string(out))
}
// Launch server in separate process
server, err := tcptest.Start(start, 30*time.Second)
if err != nil {
t.Fatalf("Failed to start idpot-server: %s", err)
}
port := server.Port()
// First, declare a new pot
base := fmt.Sprintf("http://127.0.0.1:%d", port)
client := client.New(base)
for _, min := range []uint64{0, 100, 1000, 10000} {
pot := randomName()
err = client.CreatePot(pot, min)
if err != nil {
t.Fatalf("Failed to create new pot %s: %s", pot, err)
}
t.Logf("Created new pot %s", pot)
sem := make(chan bool, 1)
wg := &sync.WaitGroup{}
ids := make(map[uint64]bool)
maxGoros := 10
maxFetches := getMaxFetches()
t0 := time.Now()
for i := 0; i < maxGoros; i++ {
wg.Add(1)
go func() {
defer func() { wg.Done() }()
for j := 0; j < maxFetches; j++ {
id, err := client.NextID(pot)
if err != nil {
t.Errorf("Failed to fetch id: %s", err)
continue
}
sem <- true
if _, ok := ids[id]; ok {
// collision!
t.Errorf("Collision detected for id %d :/", id)
} else {
ids[id] = true
}
<-sem
}
}()
}
wg.Wait()
elapsed := time.Since(t0)
// I should have 1000 ids
count := 0
for v := range ids {
if v < min {
t.Errorf("error: ID given is out of range: %d (wanted > %d)", v, min)
}
count++
}
if count != maxGoros*maxFetches {
t.Errorf("Did not get %d ids, got %d", maxGoros*maxFetches, count)
}
t.Logf("Fetched %d ids in %f secs (%f fetches/sec)", count, elapsed.Seconds(), float64(count)/elapsed.Seconds())
/* Now test client.CurrentId(), make sure that the id does not change */
id, err := client.CurrentID(pot)
if err != nil {
t.Fatalf("Failed to call CurrentID: %s", err)
}
for i := 0; i < maxFetches; i++ {
newID, err := client.CurrentID(pot)
if err != nil {
t.Fatalf("Failed to call CurrentID: %s", err)
}
if id != newID {
t.Errorf("CurrentID returned different id. Got %d, expected %d", newID, id)
}
}
}
cmd.Process.Signal(syscall.SIGTERM)
server.Wait()
}