backend: make test more reliable

This commit is contained in:
Xiang Li
2016-01-13 21:57:40 -08:00
parent 0cba12d991
commit e1de19bf75
3 changed files with 18 additions and 6 deletions

View File

@ -60,6 +60,9 @@ type backend struct {
batchTx *batchTx batchTx *batchTx
size int64 size int64
// number of commits since start
commits int64
stopc chan struct{} stopc chan struct{}
donec chan struct{} donec chan struct{}
} }
@ -164,6 +167,11 @@ func (b *backend) Close() error {
return b.db.Close() return b.db.Close()
} }
// Commits returns total number of commits since start
func (b *backend) Commits() int64 {
return atomic.LoadInt64(&b.commits)
}
// NewTmpBackend creates a backend implementation for testing. // NewTmpBackend creates a backend implementation for testing.
func NewTmpBackend(batchInterval time.Duration, batchLimit int) (*backend, string) { func NewTmpBackend(batchInterval time.Duration, batchLimit int) (*backend, string) {
dir, err := ioutil.TempDir(os.TempDir(), "etcd_backend_test") dir, err := ioutil.TempDir(os.TempDir(), "etcd_backend_test")

View File

@ -21,7 +21,6 @@ import (
"time" "time"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
"github.com/coreos/etcd/pkg/testutil"
) )
func TestBackendClose(t *testing.T) { func TestBackendClose(t *testing.T) {
@ -86,17 +85,20 @@ func TestBackendBatchIntervalCommit(t *testing.T) {
b, tmpPath := NewTmpBackend(time.Nanosecond, 10000) b, tmpPath := NewTmpBackend(time.Nanosecond, 10000)
defer cleanup(b, tmpPath) defer cleanup(b, tmpPath)
pc := b.Commits()
tx := b.BatchTx() tx := b.BatchTx()
tx.Lock() tx.Lock()
tx.UnsafeCreateBucket([]byte("test")) tx.UnsafeCreateBucket([]byte("test"))
tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar")) tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar"))
tx.Unlock() tx.Unlock()
// give time for batch interval commit to happen for i := 0; i < 10; i++ {
time.Sleep(time.Nanosecond) if b.Commits() >= pc+1 {
testutil.WaitSchedule() break
// give time for commit to finish, including possible disk IO }
time.Sleep(50 * time.Millisecond) time.Sleep(time.Duration(i*100) * time.Millisecond)
}
// check whether put happens via db view // check whether put happens via db view
b.db.View(func(tx *bolt.Tx) error { b.db.View(func(tx *bolt.Tx) error {

View File

@ -137,6 +137,8 @@ func (t *batchTx) commit(stop bool) {
return return
} }
err = t.tx.Commit() err = t.tx.Commit()
atomic.AddInt64(&t.backend.commits, 1)
t.pending = 0 t.pending = 0
if err != nil { if err != nil {
log.Fatalf("storage: cannot commit tx (%s)", err) log.Fatalf("storage: cannot commit tx (%s)", err)