Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
to := minInt(start+limit, len(files))

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
}
69 changes: 69 additions & 0 deletions maildir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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))
}

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

result, err = storage.List(30, 40)
if len(*result) != 0 || err != nil {
t.Errorf("len(result) expected: %d, got: %d", 0, 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: "[email protected]",
To: []string{"[email protected]"},
Data: "some data string",
Helo: "helo string",
},
}
}