mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-19 10:37:01 +00:00
datastore: Concurrency fix, closes #77
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user