Skip to content

Commit

Permalink
Merge pull request #783 from kenjones-cisco/test/improve-diskqueue-test
Browse files Browse the repository at this point in the history
nsqd: adds delayed retry to TestDiskQueueSyncAfterRead
  • Loading branch information
mreiferson authored Aug 5, 2016
2 parents facca8a + ad45f1c commit b2230ee
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions nsqd/diskqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,16 @@ type md struct {
writePos int64
}

func readMetaDataFile(fileName string) md {
func readMetaDataFile(fileName string, retried int) md {
f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
if err != nil {
// provide a simple retry that results in up to
// another 250ms for the file to be written.
if retried < 5 {
retried++
time.Sleep(50 * time.Millisecond)
return readMetaDataFile(fileName, retried)
}
panic(err)
}
defer f.Close()
Expand Down Expand Up @@ -221,9 +228,15 @@ func TestDiskQueueSyncAfterRead(t *testing.T) {
msg := make([]byte, 1000)
dq.Put(msg)

// sync loop is every 50ms so wait 3x to make sure the file has time
// to actually get written prior to attempting to read it.
time.Sleep(150 * time.Millisecond)

d := readMetaDataFile(dq.(*diskQueue).metaDataFileName())
// attempt to read and load metadata of the file; initialize the number
// of retry attempts to 0; the max retries is 5 with a delay of 50ms each
// meaning that a total of 400ms between message put and message metadata
// read should be sufficient for heavy write disks on the travis servers.
d := readMetaDataFile(dq.(*diskQueue).metaDataFileName(), 0)
equal(t, d.depth, int64(1))
equal(t, d.readFileNum, int64(0))
equal(t, d.writeFileNum, int64(0))
Expand All @@ -235,7 +248,7 @@ func TestDiskQueueSyncAfterRead(t *testing.T) {

time.Sleep(150 * time.Millisecond)

d = readMetaDataFile(dq.(*diskQueue).metaDataFileName())
d = readMetaDataFile(dq.(*diskQueue).metaDataFileName(), 0)
equal(t, d.depth, int64(1))
equal(t, d.readFileNum, int64(0))
equal(t, d.writeFileNum, int64(0))
Expand Down

0 comments on commit b2230ee

Please sign in to comment.