From e1de19bf75a3506184deef8c18b84a25ad3b7294 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 13 Jan 2016 21:57:40 -0800 Subject: [PATCH] backend: make test more reliable --- storage/backend/backend.go | 8 ++++++++ storage/backend/backend_test.go | 14 ++++++++------ storage/backend/batch_tx.go | 2 ++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/storage/backend/backend.go b/storage/backend/backend.go index 2f77b7e90..ffda62230 100644 --- a/storage/backend/backend.go +++ b/storage/backend/backend.go @@ -60,6 +60,9 @@ type backend struct { batchTx *batchTx size int64 + // number of commits since start + commits int64 + stopc chan struct{} donec chan struct{} } @@ -164,6 +167,11 @@ func (b *backend) Close() error { 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. func NewTmpBackend(batchInterval time.Duration, batchLimit int) (*backend, string) { dir, err := ioutil.TempDir(os.TempDir(), "etcd_backend_test") diff --git a/storage/backend/backend_test.go b/storage/backend/backend_test.go index 039ab4a66..3482cb7af 100644 --- a/storage/backend/backend_test.go +++ b/storage/backend/backend_test.go @@ -21,7 +21,6 @@ import ( "time" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt" - "github.com/coreos/etcd/pkg/testutil" ) func TestBackendClose(t *testing.T) { @@ -86,17 +85,20 @@ func TestBackendBatchIntervalCommit(t *testing.T) { b, tmpPath := NewTmpBackend(time.Nanosecond, 10000) defer cleanup(b, tmpPath) + pc := b.Commits() + tx := b.BatchTx() tx.Lock() tx.UnsafeCreateBucket([]byte("test")) tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar")) tx.Unlock() - // give time for batch interval commit to happen - time.Sleep(time.Nanosecond) - testutil.WaitSchedule() - // give time for commit to finish, including possible disk IO - time.Sleep(50 * time.Millisecond) + for i := 0; i < 10; i++ { + if b.Commits() >= pc+1 { + break + } + time.Sleep(time.Duration(i*100) * time.Millisecond) + } // check whether put happens via db view b.db.View(func(tx *bolt.Tx) error { diff --git a/storage/backend/batch_tx.go b/storage/backend/batch_tx.go index d0d0df4a4..51eb02f80 100644 --- a/storage/backend/batch_tx.go +++ b/storage/backend/batch_tx.go @@ -137,6 +137,8 @@ func (t *batchTx) commit(stop bool) { return } err = t.tx.Commit() + atomic.AddInt64(&t.backend.commits, 1) + t.pending = 0 if err != nil { log.Fatalf("storage: cannot commit tx (%s)", err)