-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: paginate messages for maildir storage
Fixes mailhog/MailHog#178
- Loading branch information
1 parent
6d871fb
commit bdeece4
Showing
4 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
} | ||
} |