@@ -2,6 +2,8 @@ package nsqd
2
2
3
3
import (
4
4
"bufio"
5
+ "fmt"
6
+ "io/ioutil"
5
7
"os"
6
8
"path"
7
9
"strconv"
@@ -15,12 +17,17 @@ func TestDiskQueue(t *testing.T) {
15
17
l := newTestLogger (t )
16
18
17
19
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 )
19
26
nequal (t , dq , nil )
20
27
equal (t , dq .Depth (), int64 (0 ))
21
28
22
29
msg := []byte ("test" )
23
- err : = dq .Put (msg )
30
+ err = dq .Put (msg )
24
31
equal (t , err , nil )
25
32
equal (t , dq .Depth (), int64 (1 ))
26
33
@@ -31,7 +38,12 @@ func TestDiskQueue(t *testing.T) {
31
38
func TestDiskQueueRoll (t * testing.T ) {
32
39
l := newTestLogger (t )
33
40
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 )
35
47
nequal (t , dq , nil )
36
48
equal (t , dq .Depth (), int64 (0 ))
37
49
@@ -55,7 +67,12 @@ func assertFileNotExist(t *testing.T, fn string) {
55
67
func TestDiskQueueEmpty (t * testing.T ) {
56
68
l := newTestLogger (t )
57
69
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 )
59
76
nequal (t , dq , nil )
60
77
equal (t , dq .Depth (), int64 (0 ))
61
78
@@ -118,7 +135,12 @@ func TestDiskQueueEmpty(t *testing.T) {
118
135
func TestDiskQueueCorruption (t * testing.T ) {
119
136
l := newTestLogger (t )
120
137
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 )
122
144
123
145
msg := make ([]byte , 123 )
124
146
for i := 0 ; i < 25 ; i ++ {
@@ -149,7 +171,12 @@ func TestDiskQueueTorture(t *testing.T) {
149
171
150
172
l := newTestLogger (t )
151
173
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 )
153
180
nequal (t , dq , nil )
154
181
equal (t , dq .Depth (), int64 (0 ))
155
182
@@ -190,7 +217,7 @@ func TestDiskQueueTorture(t *testing.T) {
190
217
191
218
t .Logf ("restarting diskqueue" )
192
219
193
- dq = newDiskQueue (dqName , os . TempDir () , 262144 , 2500 , 2 * time .Second , l )
220
+ dq = newDiskQueue (dqName , tmpDir , 262144 , 2500 , 2 * time .Second , l )
194
221
nequal (t , dq , nil )
195
222
equal (t , dq .Depth (), depth )
196
223
@@ -233,7 +260,12 @@ func BenchmarkDiskQueuePut(b *testing.B) {
233
260
b .StopTimer ()
234
261
l := newTestLogger (b )
235
262
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 )
237
269
size := 1024
238
270
b .SetBytes (int64 (size ))
239
271
data := make ([]byte , size )
@@ -247,7 +279,12 @@ func BenchmarkDiskQueuePut(b *testing.B) {
247
279
func BenchmarkDiskWrite (b * testing.B ) {
248
280
b .StopTimer ()
249
281
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 )
251
288
size := 256
252
289
b .SetBytes (int64 (size ))
253
290
data := make ([]byte , size )
@@ -262,7 +299,12 @@ func BenchmarkDiskWrite(b *testing.B) {
262
299
func BenchmarkDiskWriteBuffered (b * testing.B ) {
263
300
b .StopTimer ()
264
301
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 )
266
308
size := 256
267
309
b .SetBytes (int64 (size ))
268
310
data := make ([]byte , size )
@@ -286,7 +328,12 @@ func BenchmarkDiskQueueGet(b *testing.B) {
286
328
b .StopTimer ()
287
329
l := newTestLogger (b )
288
330
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 )
290
337
for i := 0 ; i < b .N ; i ++ {
291
338
dq .Put ([]byte ("aaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
292
339
}
0 commit comments