Browse Source

backend: make test more reliable

Xiang Li 10 năm trước cách đây
mục cha
commit
e1de19bf75

+ 8 - 0
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")

+ 8 - 6
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 {

+ 2 - 0
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)