mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-23 12:37:07 +00:00
datastore: Concurrency fix, closes #77
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/mail"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jhillyerd/enmime"
|
||||
@@ -22,6 +23,8 @@ var (
|
||||
type DataStore interface {
|
||||
MailboxFor(emailAddress string) (Mailbox, error)
|
||||
AllMailboxes() ([]Mailbox, error)
|
||||
// LockFor is a temporary hack to fix #77 until Datastore revamp
|
||||
LockFor(emailAddress string) (*sync.RWMutex, error)
|
||||
}
|
||||
|
||||
// Mailbox is an interface to get and manipulate messages in a DataStore
|
||||
|
||||
19
datastore/lock.go
Normal file
19
datastore/lock.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type HashLock [4096]sync.RWMutex
|
||||
|
||||
func (h *HashLock) Get(hash string) *sync.RWMutex {
|
||||
if len(hash) < 3 {
|
||||
return nil
|
||||
}
|
||||
i, err := strconv.ParseInt(hash[0:3], 16, 0)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &h[i]
|
||||
}
|
||||
61
datastore/lock_test.go
Normal file
61
datastore/lock_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package datastore_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jhillyerd/inbucket/datastore"
|
||||
)
|
||||
|
||||
func TestHashLock(t *testing.T) {
|
||||
hl := &datastore.HashLock{}
|
||||
|
||||
// Invalid hashes
|
||||
testCases := []struct {
|
||||
name, input string
|
||||
}{
|
||||
{"empty", ""},
|
||||
{"short", "a0"},
|
||||
{"badhex", "zzzzzzzzzzzzzzzzzzzzzzz"},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.input, func(t *testing.T) {
|
||||
l := hl.Get(tc.input)
|
||||
if l != nil {
|
||||
t.Errorf("Expected nil lock for %s %q, got %v", tc.name, tc.input, l)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Valid hashes
|
||||
testStrings := []string{
|
||||
"deadbeef",
|
||||
"00000000",
|
||||
"ffffffff",
|
||||
}
|
||||
for _, ts := range testStrings {
|
||||
t.Run(ts, func(t *testing.T) {
|
||||
l := hl.Get(ts)
|
||||
if l == nil {
|
||||
t.Errorf("Expeced non-nil lock for hex string %q", ts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
a := hl.Get("deadbeef")
|
||||
b := hl.Get("deadbeef")
|
||||
if a != b {
|
||||
t.Errorf("Expected identical locks for identical hashes, got: %p != %p", a, b)
|
||||
}
|
||||
|
||||
a = hl.Get("deadbeef")
|
||||
b = hl.Get("d3adb33f")
|
||||
if a == b {
|
||||
t.Errorf("Expected different locks for different hashes, got: %p == %p", a, b)
|
||||
}
|
||||
|
||||
a = hl.Get("deadbeef")
|
||||
b = hl.Get("deadb33f")
|
||||
if a != b {
|
||||
t.Errorf("Expected identical locks for identical leading hashes, got: %p != %p", a, b)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package datastore
|
||||
import (
|
||||
"io"
|
||||
"net/mail"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jhillyerd/enmime"
|
||||
@@ -26,6 +27,10 @@ func (m *MockDataStore) AllMailboxes() ([]Mailbox, error) {
|
||||
return args.Get(0).([]Mailbox), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockDataStore) LockFor(name string) (*sync.RWMutex, error) {
|
||||
return &sync.RWMutex{}, nil
|
||||
}
|
||||
|
||||
// MockMailbox is a shared mock for unit testing
|
||||
type MockMailbox struct {
|
||||
mock.Mock
|
||||
|
||||
Reference in New Issue
Block a user