Skip to content

Commit 9e74e8c

Browse files
committed
Merge pull request #553 from mreiferson/test_cleanup_553
Tests/benchmarks should clean up after themselves
2 parents a2a5938 + f5f1c91 commit 9e74e8c

7 files changed

+142
-17
lines changed

nsqd/channel_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nsqd
22

33
import (
4+
"os"
45
"strconv"
56
"testing"
67
"time"
@@ -11,6 +12,7 @@ func TestPutMessage(t *testing.T) {
1112
opts := NewNSQDOptions()
1213
opts.Logger = newTestLogger(t)
1314
_, _, nsqd := mustStartNSQD(opts)
15+
defer os.RemoveAll(opts.DataPath)
1416
defer nsqd.Exit()
1517

1618
topicName := "test_put_message" + strconv.Itoa(int(time.Now().Unix()))
@@ -31,6 +33,7 @@ func TestPutMessage2Chan(t *testing.T) {
3133
opts := NewNSQDOptions()
3234
opts.Logger = newTestLogger(t)
3335
_, _, nsqd := mustStartNSQD(opts)
36+
defer os.RemoveAll(opts.DataPath)
3437
defer nsqd.Exit()
3538

3639
topicName := "test_put_message_2chan" + strconv.Itoa(int(time.Now().Unix()))
@@ -58,6 +61,7 @@ func TestInFlightWorker(t *testing.T) {
5861
opts.Logger = newTestLogger(t)
5962
opts.MsgTimeout = 100 * time.Millisecond
6063
_, _, nsqd := mustStartNSQD(opts)
64+
defer os.RemoveAll(opts.DataPath)
6165
defer nsqd.Exit()
6266

6367
topicName := "test_in_flight_worker" + strconv.Itoa(int(time.Now().Unix()))
@@ -98,6 +102,7 @@ func TestChannelEmpty(t *testing.T) {
98102
opts := NewNSQDOptions()
99103
opts.Logger = newTestLogger(t)
100104
_, _, nsqd := mustStartNSQD(opts)
105+
defer os.RemoveAll(opts.DataPath)
101106
defer nsqd.Exit()
102107

103108
topicName := "test_channel_empty" + strconv.Itoa(int(time.Now().Unix()))
@@ -130,6 +135,7 @@ func TestChannelEmptyConsumer(t *testing.T) {
130135
opts := NewNSQDOptions()
131136
opts.Logger = newTestLogger(t)
132137
tcpAddr, _, nsqd := mustStartNSQD(opts)
138+
defer os.RemoveAll(opts.DataPath)
133139
defer nsqd.Exit()
134140

135141
conn, _ := mustConnectNSQD(tcpAddr)

nsqd/diskqueue_test.go

+58-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package nsqd
22

33
import (
44
"bufio"
5+
"fmt"
6+
"io/ioutil"
57
"os"
68
"path"
79
"strconv"
@@ -15,12 +17,17 @@ func TestDiskQueue(t *testing.T) {
1517
l := newTestLogger(t)
1618

1719
dqName := "test_disk_queue" + strconv.Itoa(int(time.Now().Unix()))
18-
dq := newDiskQueue(dqName, os.TempDir(), 1024, 2500, 2*time.Second, l)
20+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
21+
if err != nil {
22+
panic(err)
23+
}
24+
defer os.RemoveAll(tmpDir)
25+
dq := newDiskQueue(dqName, tmpDir, 1024, 2500, 2*time.Second, l)
1926
nequal(t, dq, nil)
2027
equal(t, dq.Depth(), int64(0))
2128

2229
msg := []byte("test")
23-
err := dq.Put(msg)
30+
err = dq.Put(msg)
2431
equal(t, err, nil)
2532
equal(t, dq.Depth(), int64(1))
2633

@@ -31,7 +38,12 @@ func TestDiskQueue(t *testing.T) {
3138
func TestDiskQueueRoll(t *testing.T) {
3239
l := newTestLogger(t)
3340
dqName := "test_disk_queue_roll" + strconv.Itoa(int(time.Now().Unix()))
34-
dq := newDiskQueue(dqName, os.TempDir(), 100, 2500, 2*time.Second, l)
41+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
42+
if err != nil {
43+
panic(err)
44+
}
45+
defer os.RemoveAll(tmpDir)
46+
dq := newDiskQueue(dqName, tmpDir, 100, 2500, 2*time.Second, l)
3547
nequal(t, dq, nil)
3648
equal(t, dq.Depth(), int64(0))
3749

@@ -55,7 +67,12 @@ func assertFileNotExist(t *testing.T, fn string) {
5567
func TestDiskQueueEmpty(t *testing.T) {
5668
l := newTestLogger(t)
5769
dqName := "test_disk_queue_empty" + strconv.Itoa(int(time.Now().Unix()))
58-
dq := newDiskQueue(dqName, os.TempDir(), 100, 2500, 2*time.Second, l)
70+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
71+
if err != nil {
72+
panic(err)
73+
}
74+
defer os.RemoveAll(tmpDir)
75+
dq := newDiskQueue(dqName, tmpDir, 100, 2500, 2*time.Second, l)
5976
nequal(t, dq, nil)
6077
equal(t, dq.Depth(), int64(0))
6178

@@ -118,7 +135,12 @@ func TestDiskQueueEmpty(t *testing.T) {
118135
func TestDiskQueueCorruption(t *testing.T) {
119136
l := newTestLogger(t)
120137
dqName := "test_disk_queue_corruption" + strconv.Itoa(int(time.Now().Unix()))
121-
dq := newDiskQueue(dqName, os.TempDir(), 1000, 5, 2*time.Second, l)
138+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
139+
if err != nil {
140+
panic(err)
141+
}
142+
defer os.RemoveAll(tmpDir)
143+
dq := newDiskQueue(dqName, tmpDir, 1000, 5, 2*time.Second, l)
122144

123145
msg := make([]byte, 123)
124146
for i := 0; i < 25; i++ {
@@ -149,7 +171,12 @@ func TestDiskQueueTorture(t *testing.T) {
149171

150172
l := newTestLogger(t)
151173
dqName := "test_disk_queue_torture" + strconv.Itoa(int(time.Now().Unix()))
152-
dq := newDiskQueue(dqName, os.TempDir(), 262144, 2500, 2*time.Second, l)
174+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
175+
if err != nil {
176+
panic(err)
177+
}
178+
defer os.RemoveAll(tmpDir)
179+
dq := newDiskQueue(dqName, tmpDir, 262144, 2500, 2*time.Second, l)
153180
nequal(t, dq, nil)
154181
equal(t, dq.Depth(), int64(0))
155182

@@ -190,7 +217,7 @@ func TestDiskQueueTorture(t *testing.T) {
190217

191218
t.Logf("restarting diskqueue")
192219

193-
dq = newDiskQueue(dqName, os.TempDir(), 262144, 2500, 2*time.Second, l)
220+
dq = newDiskQueue(dqName, tmpDir, 262144, 2500, 2*time.Second, l)
194221
nequal(t, dq, nil)
195222
equal(t, dq.Depth(), depth)
196223

@@ -233,7 +260,12 @@ func BenchmarkDiskQueuePut(b *testing.B) {
233260
b.StopTimer()
234261
l := newTestLogger(b)
235262
dqName := "bench_disk_queue_put" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
236-
dq := newDiskQueue(dqName, os.TempDir(), 1024768*100, 2500, 2*time.Second, l)
263+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
264+
if err != nil {
265+
panic(err)
266+
}
267+
defer os.RemoveAll(tmpDir)
268+
dq := newDiskQueue(dqName, tmpDir, 1024768*100, 2500, 2*time.Second, l)
237269
size := 1024
238270
b.SetBytes(int64(size))
239271
data := make([]byte, size)
@@ -247,7 +279,12 @@ func BenchmarkDiskQueuePut(b *testing.B) {
247279
func BenchmarkDiskWrite(b *testing.B) {
248280
b.StopTimer()
249281
fileName := "bench_disk_queue_put" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
250-
f, _ := os.OpenFile(path.Join(os.TempDir(), fileName), os.O_RDWR|os.O_CREATE, 0600)
282+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
283+
if err != nil {
284+
panic(err)
285+
}
286+
defer os.RemoveAll(tmpDir)
287+
f, _ := os.OpenFile(path.Join(tmpDir, fileName), os.O_RDWR|os.O_CREATE, 0600)
251288
size := 256
252289
b.SetBytes(int64(size))
253290
data := make([]byte, size)
@@ -262,7 +299,12 @@ func BenchmarkDiskWrite(b *testing.B) {
262299
func BenchmarkDiskWriteBuffered(b *testing.B) {
263300
b.StopTimer()
264301
fileName := "bench_disk_queue_put" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
265-
f, _ := os.OpenFile(path.Join(os.TempDir(), fileName), os.O_RDWR|os.O_CREATE, 0600)
302+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
303+
if err != nil {
304+
panic(err)
305+
}
306+
defer os.RemoveAll(tmpDir)
307+
f, _ := os.OpenFile(path.Join(tmpDir, fileName), os.O_RDWR|os.O_CREATE, 0600)
266308
size := 256
267309
b.SetBytes(int64(size))
268310
data := make([]byte, size)
@@ -286,7 +328,12 @@ func BenchmarkDiskQueueGet(b *testing.B) {
286328
b.StopTimer()
287329
l := newTestLogger(b)
288330
dqName := "bench_disk_queue_get" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
289-
dq := newDiskQueue(dqName, os.TempDir(), 1024768, 2500, 2*time.Second, l)
331+
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
332+
if err != nil {
333+
panic(err)
334+
}
335+
defer os.RemoveAll(tmpDir)
336+
dq := newDiskQueue(dqName, tmpDir, 1024768, 2500, 2*time.Second, l)
290337
for i := 0; i < b.N; i++ {
291338
dq.Put([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
292339
}

nsqd/http_test.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"net"
99
"net/http"
10+
"os"
1011
"runtime"
1112
"strconv"
1213
"sync"
@@ -21,6 +22,7 @@ func TestHTTPput(t *testing.T) {
2122
opts := NewNSQDOptions()
2223
opts.Logger = newTestLogger(t)
2324
_, httpAddr, nsqd := mustStartNSQD(opts)
25+
defer os.RemoveAll(opts.DataPath)
2426
defer nsqd.Exit()
2527

2628
topicName := "test_http_put" + strconv.Itoa(int(time.Now().Unix()))
@@ -43,6 +45,7 @@ func TestHTTPputEmpty(t *testing.T) {
4345
opts := NewNSQDOptions()
4446
opts.Logger = newTestLogger(t)
4547
_, httpAddr, nsqd := mustStartNSQD(opts)
48+
defer os.RemoveAll(opts.DataPath)
4649
defer nsqd.Exit()
4750

4851
topicName := "test_http_put_empty" + strconv.Itoa(int(time.Now().Unix()))
@@ -66,6 +69,7 @@ func TestHTTPmput(t *testing.T) {
6669
opts := NewNSQDOptions()
6770
opts.Logger = newTestLogger(t)
6871
_, httpAddr, nsqd := mustStartNSQD(opts)
72+
defer os.RemoveAll(opts.DataPath)
6973
defer nsqd.Exit()
7074

7175
topicName := "test_http_mput" + strconv.Itoa(int(time.Now().Unix()))
@@ -94,6 +98,7 @@ func TestHTTPmputEmpty(t *testing.T) {
9498
opts := NewNSQDOptions()
9599
opts.Logger = newTestLogger(t)
96100
_, httpAddr, nsqd := mustStartNSQD(opts)
101+
defer os.RemoveAll(opts.DataPath)
97102
defer nsqd.Exit()
98103

99104
topicName := "test_http_mput_empty" + strconv.Itoa(int(time.Now().Unix()))
@@ -124,6 +129,7 @@ func TestHTTPmputBinary(t *testing.T) {
124129
opts := NewNSQDOptions()
125130
opts.Logger = newTestLogger(t)
126131
_, httpAddr, nsqd := mustStartNSQD(opts)
132+
defer os.RemoveAll(opts.DataPath)
127133
defer nsqd.Exit()
128134

129135
topicName := "test_http_mput_bin" + strconv.Itoa(int(time.Now().Unix()))
@@ -156,6 +162,7 @@ func TestHTTPSRequire(t *testing.T) {
156162
opts.TLSKey = "./test/certs/server.key"
157163
opts.TLSClientAuthPolicy = "require"
158164
_, httpAddr, nsqd := mustStartNSQD(opts)
165+
defer os.RemoveAll(opts.DataPath)
159166
defer nsqd.Exit()
160167

161168
topicName := "test_http_put_req" + strconv.Itoa(int(time.Now().Unix()))
@@ -201,10 +208,10 @@ func TestHTTPSRequireVerify(t *testing.T) {
201208
opts.TLSRootCAFile = "./test/certs/ca.pem"
202209
opts.TLSClientAuthPolicy = "require-verify"
203210
_, httpAddr, nsqd := mustStartNSQD(opts)
204-
httpsAddr := nsqd.httpsListener.Addr().(*net.TCPAddr)
205-
211+
defer os.RemoveAll(opts.DataPath)
206212
defer nsqd.Exit()
207213

214+
httpsAddr := nsqd.httpsListener.Addr().(*net.TCPAddr)
208215
topicName := "test_http_put_req_verf" + strconv.Itoa(int(time.Now().Unix()))
209216
topic := nsqd.GetTopic(topicName)
210217

@@ -266,7 +273,7 @@ func TestTLSRequireVerifyExceptHTTP(t *testing.T) {
266273
opts.TLSClientAuthPolicy = "require-verify"
267274
opts.TLSRequired = TLSRequiredExceptHTTP
268275
_, httpAddr, nsqd := mustStartNSQD(opts)
269-
276+
defer os.RemoveAll(opts.DataPath)
270277
defer nsqd.Exit()
271278

272279
topicName := "test_http_req_verf_except_http" + strconv.Itoa(int(time.Now().Unix()))
@@ -290,6 +297,7 @@ func TestHTTPDeprecatedTopicChannel(t *testing.T) {
290297
opts := NewNSQDOptions()
291298
opts.Logger = newTestLogger(t)
292299
_, httpAddr, nsqd := mustStartNSQD(opts)
300+
defer os.RemoveAll(opts.DataPath)
293301
defer nsqd.Exit()
294302

295303
topicName := "test_http_topic_channel" + strconv.Itoa(int(time.Now().Unix()))
@@ -386,6 +394,7 @@ func TestHTTPTransitionTopicChannel(t *testing.T) {
386394
opts := NewNSQDOptions()
387395
opts.Logger = newTestLogger(t)
388396
_, httpAddr, nsqd := mustStartNSQD(opts)
397+
defer os.RemoveAll(opts.DataPath)
389398
defer nsqd.Exit()
390399

391400
client := http.Client{}
@@ -507,6 +516,7 @@ func TestHTTPV1TopicChannel(t *testing.T) {
507516
opts := NewNSQDOptions()
508517
opts.Logger = newTestLogger(t)
509518
_, httpAddr, nsqd := mustStartNSQD(opts)
519+
defer os.RemoveAll(opts.DataPath)
510520
defer nsqd.Exit()
511521

512522
topicName := "test_http_topic_channel2" + strconv.Itoa(int(time.Now().Unix()))
@@ -614,6 +624,7 @@ func BenchmarkHTTPput(b *testing.B) {
614624
opts.Logger = newTestLogger(b)
615625
opts.MemQueueSize = int64(b.N)
616626
_, httpAddr, nsqd := mustStartNSQD(opts)
627+
defer os.RemoveAll(opts.DataPath)
617628
msg := make([]byte, 256)
618629
topicName := "bench_http_put" + strconv.Itoa(int(time.Now().Unix()))
619630
url := fmt.Sprintf("http://%s/put?topic=%s", httpAddr, topicName)
@@ -653,9 +664,11 @@ func TestHTTPgetStatusJSON(t *testing.T) {
653664
opts := NewNSQDOptions()
654665
opts.Logger = newTestLogger(t)
655666
_, httpAddr, nsqd := mustStartNSQD(opts)
667+
defer os.RemoveAll(opts.DataPath)
668+
defer nsqd.Exit()
669+
656670
nsqd.startTime = testTime
657671
expectedJSON := fmt.Sprintf(`{"status_code":200,"status_txt":"OK","data":{"version":"%v","health":"OK","start_time":%v,"topics":[]}}`, version.Binary, testTime.Unix())
658-
defer nsqd.Exit()
659672

660673
url := fmt.Sprintf("http://%s/stats?format=json", httpAddr)
661674
resp, err := http.Get(url)
@@ -671,9 +684,11 @@ func TestHTTPgetStatusText(t *testing.T) {
671684
opts := NewNSQDOptions()
672685
opts.Logger = newTestLogger(t)
673686
_, httpAddr, nsqd := mustStartNSQD(opts)
674-
nsqd.startTime = testTime
687+
defer os.RemoveAll(opts.DataPath)
675688
defer nsqd.Exit()
676689

690+
nsqd.startTime = testTime
691+
677692
url := fmt.Sprintf("http://%s/stats?format=text", httpAddr)
678693
resp, err := http.Get(url)
679694
equal(t, err, nil)

nsqd/nsqd_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package nsqd
33
import (
44
"fmt"
55
"io/ioutil"
6+
"os"
67
"path"
78
"path/filepath"
89
"reflect"
@@ -81,6 +82,9 @@ func TestStartup(t *testing.T) {
8182
opts.MemQueueSize = 100
8283
opts.MaxBytesPerFile = 10240
8384
_, _, nsqd := mustStartNSQD(opts)
85+
defer os.RemoveAll(opts.DataPath)
86+
87+
origDataPath := opts.DataPath
8488

8589
topicName := "nsqd_test" + strconv.Itoa(int(time.Now().Unix()))
8690

@@ -147,6 +151,7 @@ func TestStartup(t *testing.T) {
147151
opts.Logger = newTestLogger(t)
148152
opts.MemQueueSize = 100
149153
opts.MaxBytesPerFile = 10240
154+
opts.DataPath = origDataPath
150155
_, _, nsqd = mustStartNSQD(opts)
151156

152157
go func() {
@@ -190,6 +195,7 @@ func TestEphemeralTopicsAndChannels(t *testing.T) {
190195
opts.Logger = newTestLogger(t)
191196
opts.MemQueueSize = 100
192197
_, _, nsqd := mustStartNSQD(opts)
198+
defer os.RemoveAll(opts.DataPath)
193199

194200
topicName := "ephemeral_topic" + strconv.Itoa(int(time.Now().Unix())) + "#ephemeral"
195201
doneExitChan := make(chan int)
@@ -240,6 +246,7 @@ func TestPauseMetadata(t *testing.T) {
240246
opts := NewNSQDOptions()
241247
opts.Logger = newTestLogger(t)
242248
_, _, nsqd := mustStartNSQD(opts)
249+
defer os.RemoveAll(opts.DataPath)
243250
defer nsqd.Exit()
244251

245252
// avoid concurrency issue of async PersistMetadata() calls

0 commit comments

Comments
 (0)