diff --git a/maildir.go b/maildir.go index d102549..b81a79e 100644 --- a/maildir.go +++ b/maildir.go @@ -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 @@ -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 +} diff --git a/maildir_test.go b/maildir_test.go new file mode 100644 index 0000000..96c691c --- /dev/null +++ b/maildir_test.go @@ -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() +} diff --git a/memory_test.go b/memory_test.go index a3fb8a2..8aaa2b5 100644 --- a/memory_test.go +++ b/memory_test.go @@ -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 { @@ -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 { @@ -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 { diff --git a/storage_test.go b/storage_test.go new file mode 100644 index 0000000..67158a6 --- /dev/null +++ b/storage_test.go @@ -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", + }, + } +}