From 64a251d69a7c643aa2ef9c13b0ccaf435c96b175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Sat, 23 Nov 2019 22:13:17 +0800 Subject: [PATCH] improve the syncMap.Iterate test to make it 100% reproducible (#970) * improve the syncMap.Iterate test to make it 100% reproducible * rename store/mocks/Store.go * rename mocks/store to mock/store --- go.mod | 1 + go.sum | 1 + store/mock/store.go | 103 ++++++++++++++++++++++++++++++++++++++++++++ sync/map_test.go | 17 ++++---- 4 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 store/mock/store.go diff --git a/go.mod b/go.mod index 48696ba9..0812f043 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( github.com/nlopes/slack v0.6.0 github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c github.com/pkg/errors v0.8.1 + github.com/stretchr/testify v1.4.0 go.uber.org/zap v1.12.0 // indirect golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a golang.org/x/net v0.0.0-20191109021931-daa7c04131f5 diff --git a/go.sum b/go.sum index 00550ff0..1be95770 100644 --- a/go.sum +++ b/go.sum @@ -360,6 +360,7 @@ github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4= github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/store/mock/store.go b/store/mock/store.go new file mode 100644 index 00000000..fe296b02 --- /dev/null +++ b/store/mock/store.go @@ -0,0 +1,103 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. + +package mock + +import mock "github.com/stretchr/testify/mock" +import store "github.com/micro/go-micro/store" + +// Store is an autogenerated mock type for the Store type +type Store struct { + mock.Mock +} + +// Delete provides a mock function with given fields: key +func (_m *Store) Delete(key ...string) error { + _va := make([]interface{}, len(key)) + for _i := range key { + _va[_i] = key[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 error + if rf, ok := ret.Get(0).(func(...string) error); ok { + r0 = rf(key...) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// List provides a mock function with given fields: +func (_m *Store) List() ([]*store.Record, error) { + ret := _m.Called() + + var r0 []*store.Record + if rf, ok := ret.Get(0).(func() []*store.Record); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*store.Record) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Read provides a mock function with given fields: key +func (_m *Store) Read(key ...string) ([]*store.Record, error) { + _va := make([]interface{}, len(key)) + for _i := range key { + _va[_i] = key[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 []*store.Record + if rf, ok := ret.Get(0).(func(...string) []*store.Record); ok { + r0 = rf(key...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*store.Record) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(...string) error); ok { + r1 = rf(key...) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Write provides a mock function with given fields: rec +func (_m *Store) Write(rec ...*store.Record) error { + _va := make([]interface{}, len(rec)) + for _i := range rec { + _va[_i] = rec[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 error + if rf, ok := ret.Get(0).(func(...*store.Record) error); ok { + r0 = rf(rec...) + } else { + r0 = ret.Error(0) + } + + return r0 +} diff --git a/sync/map_test.go b/sync/map_test.go index 8e3e0574..fa54cd2c 100644 --- a/sync/map_test.go +++ b/sync/map_test.go @@ -4,14 +4,13 @@ import ( "testing" "time" - store "github.com/micro/go-micro/store" - mem_store "github.com/micro/go-micro/store/memory" + "github.com/micro/go-micro/store" + store_mock "github.com/micro/go-micro/store/mock" mem_lock "github.com/micro/go-micro/sync/lock/memory" + "github.com/stretchr/testify/mock" ) func TestIterate(t *testing.T) { - s1 := mem_store.NewStore() - s2 := mem_store.NewStore() recA := &store.Record{ Key: "A", Value: nil, @@ -20,10 +19,12 @@ func TestIterate(t *testing.T) { Key: "B", Value: nil, } - s1.Write(recA) - s1.Write(recB) - s2.Write(recB) - s2.Write(recA) + s1 := &store_mock.Store{} + s2 := &store_mock.Store{} + s1.On("List").Return([]*store.Record{recA, recB}, nil) + s2.On("List").Return([]*store.Record{recB, recA}, nil) + s1.On("Write", mock.Anything).Return(nil) + s2.On("Write", mock.Anything).Return(nil) f := func(key, val interface{}) error { time.Sleep(1 * time.Millisecond)