mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #4208 from xiang90/fix_test
backend: make test more reliable
This commit is contained in:
commit
84d7318820
@ -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")
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user