@@ -4,13 +4,15 @@ import (
4
4
"bufio"
5
5
"bytes"
6
6
"fmt"
7
+ "io/fs"
7
8
"io/ioutil"
8
9
"os"
9
10
"path"
10
11
"path/filepath"
11
12
"reflect"
12
13
"runtime"
13
14
"strconv"
15
+ "strings"
14
16
"sync"
15
17
"sync/atomic"
16
18
"testing"
@@ -308,17 +310,22 @@ func TestDiskQueueCorruption(t *testing.T) {
308
310
dqFn := dq .(* diskQueue ).fileName (1 )
309
311
os .Truncate (dqFn , 400 ) // 3 valid messages, 5 corrupted
310
312
311
- for i := 0 ; i < 19 ; i ++ { // 1 message leftover in 4th file
312
- Equal (t , msg , <- dq .ReadChan ())
313
- }
314
-
315
313
// corrupt the 4th (current) file
316
314
dqFn = dq .(* diskQueue ).fileName (3 )
317
315
os .Truncate (dqFn , 100 )
318
316
317
+ for i := 0 ; i < 18 ; i ++ {
318
+ Equal (t , msg , <- dq .ReadChan ())
319
+ }
320
+ Equal (t , int64 (7 ), dq .Depth ())
321
+
322
+ Equal (t , msg , <- dq .ReadChan ())
323
+ Equal (t , int64 (0 ), dq .Depth ())
324
+
319
325
dq .Put (msg ) // in 5th file
320
326
321
327
Equal (t , msg , <- dq .ReadChan ())
328
+ Equal (t , int64 (0 ), dq .Depth ())
322
329
323
330
// write a corrupt (len 0) message at the 5th (current) file
324
331
dq .(* diskQueue ).writeFile .Write ([]byte {0 , 0 , 0 , 0 })
@@ -331,14 +338,15 @@ func TestDiskQueueCorruption(t *testing.T) {
331
338
332
339
dq .Put (msg )
333
340
dq .Put (msg )
341
+
334
342
// corrupt the last file
335
343
dqFn = dq .(* diskQueue ).fileName (5 )
336
344
os .Truncate (dqFn , 100 )
337
345
338
346
Equal (t , int64 (2 ), dq .Depth ())
339
347
340
348
// return one message and try reading again from corrupted file
341
- <- dq .ReadChan ()
349
+ Equal ( t , msg , <- dq .ReadChan () )
342
350
343
351
// give diskqueue time to handle read error
344
352
time .Sleep (50 * time .Millisecond )
@@ -773,3 +781,39 @@ func benchmarkDiskQueueGet(size int64, b *testing.B) {
773
781
<- dq .ReadChan ()
774
782
}
775
783
}
784
+
785
+ func TestDiskQueueRollAsync (t * testing.T ) {
786
+ l := NewTestLogger (t )
787
+ dqName := "test_disk_queue_roll" + strconv .Itoa (int (time .Now ().Unix ()))
788
+ tmpDir , err := ioutil .TempDir ("" , fmt .Sprintf ("nsq-test-%d" , time .Now ().UnixNano ()))
789
+ if err != nil {
790
+ panic (err )
791
+ }
792
+ defer os .RemoveAll (tmpDir )
793
+ msg := []byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 }
794
+ ml := int64 (len (msg ))
795
+ dq := New (dqName , tmpDir , 10 * (ml + 4 ), int32 (ml ), 1 << 10 , 2500 , 2 * time .Second , l )
796
+ defer dq .Close ()
797
+ NotNil (t , dq )
798
+ Equal (t , int64 (0 ), dq .Depth ())
799
+
800
+ for i := 0 ; i < 11 ; i ++ {
801
+ err := dq .Put (msg )
802
+ Nil (t , err )
803
+ Equal (t , int64 (1 ), dq .Depth ())
804
+
805
+ Equal (t , msg , <- dq .ReadChan ())
806
+ Equal (t , int64 (0 ), dq .Depth ())
807
+ }
808
+
809
+ Equal (t , int64 (1 ), dq .(* diskQueue ).writeFileNum )
810
+ Equal (t , int64 (ml + 4 ), dq .(* diskQueue ).writePos )
811
+
812
+ filepath .Walk (tmpDir , func (path string , info fs.FileInfo , err error ) error {
813
+ if strings .HasSuffix (path , ".bad" ) {
814
+ t .FailNow ()
815
+ }
816
+
817
+ return err
818
+ })
819
+ }
0 commit comments