mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-19 10:37:01 +00:00
Message size calculation changes
Message sizes are now calculated as the message is written out to disk, and saved into the index.gob file
This commit is contained in:
@@ -275,6 +275,7 @@ type FileMessage struct {
|
|||||||
Fdate time.Time
|
Fdate time.Time
|
||||||
Ffrom string
|
Ffrom string
|
||||||
Fsubject string
|
Fsubject string
|
||||||
|
Fsize int64
|
||||||
// These are for creating new messages only
|
// These are for creating new messages only
|
||||||
writable bool
|
writable bool
|
||||||
writerFile *os.File
|
writerFile *os.File
|
||||||
@@ -310,11 +311,7 @@ func (m *FileMessage) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *FileMessage) Size() int64 {
|
func (m *FileMessage) Size() int64 {
|
||||||
fi, err := os.Stat(m.rawPath())
|
return m.Fsize
|
||||||
if err != nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return fi.Size()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *FileMessage) rawPath() string {
|
func (m *FileMessage) rawPath() string {
|
||||||
@@ -399,6 +396,7 @@ func (m *FileMessage) Append(data []byte) error {
|
|||||||
m.writer = bufio.NewWriter(file)
|
m.writer = bufio.NewWriter(file)
|
||||||
}
|
}
|
||||||
_, err := m.writer.Write(data)
|
_, err := m.writer.Write(data)
|
||||||
|
m.Fsize += int64(len(data))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func TestFSDirStructure(t *testing.T) {
|
|||||||
mbPath := expect
|
mbPath := expect
|
||||||
expect = filepath.Join(mbPath, "index.gob")
|
expect = filepath.Join(mbPath, "index.gob")
|
||||||
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
||||||
expect = filepath.Join(mbPath, id1 + ".raw")
|
expect = filepath.Join(mbPath, id1+".raw")
|
||||||
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
||||||
|
|
||||||
// Deliver second test message
|
// Deliver second test message
|
||||||
@@ -53,7 +53,7 @@ func TestFSDirStructure(t *testing.T) {
|
|||||||
// Check files
|
// Check files
|
||||||
expect = filepath.Join(mbPath, "index.gob")
|
expect = filepath.Join(mbPath, "index.gob")
|
||||||
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
||||||
expect = filepath.Join(mbPath, id2 + ".raw")
|
expect = filepath.Join(mbPath, id2+".raw")
|
||||||
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
||||||
|
|
||||||
// Delete message
|
// Delete message
|
||||||
@@ -65,7 +65,7 @@ func TestFSDirStructure(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Message should be removed
|
// Message should be removed
|
||||||
expect = filepath.Join(mbPath, id1 + ".raw")
|
expect = filepath.Join(mbPath, id1+".raw")
|
||||||
assert.False(t, isPresent(expect), "Did not expect %q to exist", expect)
|
assert.False(t, isPresent(expect), "Did not expect %q to exist", expect)
|
||||||
expect = filepath.Join(mbPath, "index.gob")
|
expect = filepath.Join(mbPath, "index.gob")
|
||||||
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
assert.True(t, isFile(expect), "Expected %q to be a file", expect)
|
||||||
@@ -77,7 +77,7 @@ func TestFSDirStructure(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
// Message should be removed
|
// Message should be removed
|
||||||
expect = filepath.Join(mbPath, id2 + ".raw")
|
expect = filepath.Join(mbPath, id2+".raw")
|
||||||
assert.False(t, isPresent(expect), "Did not expect %q to exist", expect)
|
assert.False(t, isPresent(expect), "Did not expect %q to exist", expect)
|
||||||
|
|
||||||
// No messages, index & maildir should be removed
|
// No messages, index & maildir should be removed
|
||||||
@@ -87,7 +87,6 @@ func TestFSDirStructure(t *testing.T) {
|
|||||||
assert.False(t, isPresent(expect), "Did not expect %q to exist", expect)
|
assert.False(t, isPresent(expect), "Did not expect %q to exist", expect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Test FileDataStore.AllMailboxes()
|
// Test FileDataStore.AllMailboxes()
|
||||||
func TestFSAllMailboxes(t *testing.T) {
|
func TestFSAllMailboxes(t *testing.T) {
|
||||||
ds := setupDataStore()
|
ds := setupDataStore()
|
||||||
@@ -219,6 +218,37 @@ func TestFSDelete(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test message size calculation
|
||||||
|
func TestFSSize(t *testing.T) {
|
||||||
|
ds := setupDataStore()
|
||||||
|
defer teardownDataStore(ds)
|
||||||
|
|
||||||
|
mbName := "fred"
|
||||||
|
subjects := []string{"a", "br", "much longer than the others"}
|
||||||
|
sentIds := make([]string, len(subjects))
|
||||||
|
sentSizes := make([]int, len(subjects))
|
||||||
|
|
||||||
|
for i, subj := range subjects {
|
||||||
|
// Add a message
|
||||||
|
id, size := deliverMessage(ds, mbName, subj, time.Now())
|
||||||
|
sentIds[i] = id
|
||||||
|
sentSizes[i] = size
|
||||||
|
}
|
||||||
|
|
||||||
|
mb, err := ds.MailboxFor(mbName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for i, id := range sentIds {
|
||||||
|
msg, err := mb.GetMessage(id)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
expect := sentSizes[i]
|
||||||
|
size := msg.Size()
|
||||||
|
assert.Equal(t, expect, size, "Expected size of %v, got %v", expect, size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// setupDataStore creates a new FileDataStore in a temporary directory
|
// setupDataStore creates a new FileDataStore in a temporary directory
|
||||||
func setupDataStore() *FileDataStore {
|
func setupDataStore() *FileDataStore {
|
||||||
path, err := ioutil.TempDir("", "inbucket")
|
path, err := ioutil.TempDir("", "inbucket")
|
||||||
@@ -283,4 +313,3 @@ func isDir(path string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user