Skip to content

Commit

Permalink
fix: paginate messages for maildir storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalahutdinov committed Nov 3, 2017
1 parent 6d871fb commit bdeece4
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
13 changes: 11 additions & 2 deletions maildir.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ func (maildir *Maildir) List(start, limit int) (*data.Messages, error) {
}
defer dir.Close()

n, err := dir.Readdir(0)
files, err := dir.Readdir(0)
if err != nil {
return nil, err
}
from := minInt(start, len(files)-1)
to := minInt(start+limit, len(files)-1)

for _, fileinfo := range n {
for _, fileinfo := range files[from:to] {
b, err := ioutil.ReadFile(filepath.Join(maildir.Path, fileinfo.Name()))
if err != nil {
return nil, err
Expand Down Expand Up @@ -182,3 +184,10 @@ func (maildir *Maildir) Load(id string) (*data.Message, error) {
m.ID = data.MessageID(id)
return m, nil
}

func minInt(x, y int) int {
if x < y {
return x
}
return y
}
59 changes: 59 additions & 0 deletions maildir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package storage

import (
"testing"
)

func TestMaildirStore(t *testing.T) {
storage := CreateMaildir("testdata")

if storage.Count() != 0 {
t.Errorf("storage.Count() expected: %d, got: %d", 0, storage.Count())
}

fillStorage(storage, 25)

if storage.Count() != 25 {
t.Errorf("storage.Count() expected: %d, got: %d", 25, storage.Count())
}

storage.DeleteAll()

if storage.Count() != 0 {
t.Errorf("storage.Count() expected: %d, got: %d", 0, storage.Count())
}
}

func TestMaildirLoad(t *testing.T) {
storage := CreateMaildir("testdata")

storage.Store(newMessage("123"))

item, err := storage.Load("123")
if item == nil || err != nil {
t.Errorf("storage.Load(\"123\") expected: not nil, got: nil")
}

unexisting, err := storage.Load("321")
if unexisting != nil || err == nil {
t.Errorf("storage.Load(\"321\") expected: nil, got: not nil")
}
storage.DeleteAll()
}

func TestMaildirList(t *testing.T) {
storage := CreateMaildir("testdata")

fillStorage(storage, 25)

result, err := storage.List(0, 10)
if len(*result) != 10 || err != nil {
t.Errorf("len(result) expected: %d, got: %d", 10, len(*result))
}

result, err = storage.List(20, 30)
if len(*result) != 5 || err != nil {
t.Errorf("len(result) expected: %d, got: %d", 5, len(*result))
}
storage.DeleteAll()
}
6 changes: 3 additions & 3 deletions memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/mailhog/data"
)

func TestStore(t *testing.T) {
func TestImMemoryStore(t *testing.T) {
storage := CreateInMemory()

if storage.Count() != 0 {
Expand All @@ -35,7 +35,7 @@ func TestStore(t *testing.T) {
}
}

func TestDeleteAll(t *testing.T) {
func TestImMemoryDeleteAll(t *testing.T) {
storage := CreateInMemory()

if storage.Count() != 0 {
Expand All @@ -57,7 +57,7 @@ func TestDeleteAll(t *testing.T) {
}
}

func TestDeleteOne(t *testing.T) {
func TestImMemoryDeleteOne(t *testing.T) {
storage := CreateInMemory()

if storage.Count() != 0 {
Expand Down
35 changes: 35 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package storage

import (
"strconv"
"sync"
"time"

"github.com/mailhog/data"
)

func fillStorage(storage Storage, itemsCount int) {
var wg sync.WaitGroup
wg.Add(itemsCount)
for i := 0; i < itemsCount; i++ {
go func(i int) {
msg := newMessage(strconv.Itoa(i))
storage.Store(msg)
wg.Done()
}(i)
}
wg.Wait()
}

func newMessage(messageID string) *data.Message {
return &data.Message{
ID: data.MessageID(messageID),
Created: time.Now(),
Raw: &data.SMTPMessage{
From: "from@email.com",
To: []string{"to@email.com"},
Data: "some data string",
Helo: "helo string",
},
}
}

0 comments on commit bdeece4

Please sign in to comment.