Skip to content

Commit

Permalink
Fix incorrect double-dequeue after restarting (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
izquierdo authored Nov 6, 2023
1 parent dba729e commit 963d19a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,32 @@ func TestQueueCorruptedWritingFile(t *testing.T) {
var e entry.Entry
require.False(t, q.Dequeue(&e))
}

func TestQueueReopen(t *testing.T) {
dataDir := filepath.Join(tmpDir, "pqueue_reopen")
_ = os.RemoveAll(dataDir)
err := os.MkdirAll(dataDir, 0o777)
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(dataDir)
}()

q, err := New(dataDir, 3)
require.NoError(t, err)

require.NoError(t, q.Enqueue([]byte{1, 2, 3}))

var e entry.Entry
require.True(t, q.Dequeue(&e))
require.EqualValues(t, e, []byte{1, 2, 3})

err = q.Close()
require.NoError(t, err)

q, err = New(dataDir, 3)
require.NoError(t, err)

// only one value was enqueued and it was already dequeued, so there shouldn't be anything else
require.False(t, q.Dequeue(&e))
q.Close()
}
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func loadFileInfos(dir string, infoExtractor func(os.DirEntry) (os.FileInfo, err

files := make([]file, 0, len(fileList))
for i := range fileList {
if strings.HasPrefix(fileList[i].Name(), segPrefix) {
if strings.HasPrefix(fileList[i].Name(), segPrefix) && !strings.HasSuffix(fileList[i].Name(), segOffsetFileSuffix) {
info, e := infoExtractor(fileList[i])
if e != nil {
return nil, e
Expand Down

0 comments on commit 963d19a

Please sign in to comment.