Files
chaisql/internal/database/database_test.go
Jean Hadrien Chabran 4a6e68439a Refactor to handle errors with internal/errors (#432)
All new error handling code now rely on internal/errors package
which provides a compilation time toggle that enables to capture
stacktraces for easier debugging while developing.

It also comes with a new testutil/assert package which replaces the require
package when it comes to checking or comparing errors and printing the
stack traces if needed.

Finally, the test target of the Makefile uses the debug build tag by default. 
A testnodebug target is also provided for convenience and to make sure no
tests are broken due to not having used the internal/errors or testutil/assert package.

See #431 for more details
2021-08-22 11:47:54 +03:00

70 lines
1.2 KiB
Go

package database_test
import (
"testing"
"time"
"github.com/genjidb/genji"
"github.com/genjidb/genji/internal/testutil/assert"
)
// See issue https://github.com/genjidb/genji/issues/298
func TestConcurrentTransactionManagement(t *testing.T) {
db, err := genji.Open(":memory:")
assert.NoError(t, err)
defer func() {
assert.NoError(t, db.Close())
}()
ch := make(chan struct{})
done := make(chan struct{})
go func() {
// 1. Start transaction T1.
tx, err := db.Begin(true)
assert.NoError(t, err)
// Start transaction T2.
ch <- struct{}{}
// Wait in case goroutine gets rescheduled.
time.Sleep(time.Millisecond)
// 3. Commit or rollback T1.
assert.NoError(t, tx.Rollback())
// Wait for T2 to finish and return.
<-ch
done <- struct{}{}
}()
go func() {
<-ch // wait for T1 to start.
// 2. Attempt to start transaction T2.
// Waits for T1 to finish.
tx, err := db.Begin(true)
assert.NoError(t, err)
assert.NoError(t, tx.Rollback())
ch <- struct{}{}
}()
r := make(chan bool)
go func() {
t := time.NewTimer(time.Second)
select {
case <-t.C:
r <- false
case <-done:
r <- true
}
if !t.Stop() {
<-t.C
}
}()
if ok := <-r; !ok {
t.Fatal("deadlock")
}
}