42 lines
716 B
Go
42 lines
716 B
Go
package memdb
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestMemDB_SingleWriter_MultiReader(t *testing.T) {
|
|
db, err := NewMemDB(testValidSchema())
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
|
|
tx1 := db.Txn(true)
|
|
tx2 := db.Txn(false) // Should not block!
|
|
tx3 := db.Txn(false) // Should not block!
|
|
tx4 := db.Txn(false) // Should not block!
|
|
|
|
doneCh := make(chan struct{})
|
|
go func() {
|
|
defer close(doneCh)
|
|
db.Txn(true)
|
|
}()
|
|
|
|
select {
|
|
case <-doneCh:
|
|
t.Fatalf("should not allow another writer")
|
|
case <-time.After(10 * time.Millisecond):
|
|
}
|
|
|
|
tx1.Abort()
|
|
tx2.Abort()
|
|
tx3.Abort()
|
|
tx4.Abort()
|
|
|
|
select {
|
|
case <-doneCh:
|
|
case <-time.After(10 * time.Millisecond):
|
|
t.Fatalf("should allow another writer")
|
|
}
|
|
}
|