2019-11-03 16:18:48 +08:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-11-23 22:13:17 +08:00
|
|
|
"github.com/micro/go-micro/store"
|
|
|
|
store_mock "github.com/micro/go-micro/store/mock"
|
2019-11-03 16:18:48 +08:00
|
|
|
mem_lock "github.com/micro/go-micro/sync/lock/memory"
|
2019-11-23 22:13:17 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2019-11-03 16:18:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIterate(t *testing.T) {
|
|
|
|
recA := &store.Record{
|
|
|
|
Key: "A",
|
|
|
|
Value: nil,
|
|
|
|
}
|
|
|
|
recB := &store.Record{
|
|
|
|
Key: "B",
|
|
|
|
Value: nil,
|
|
|
|
}
|
2019-11-23 22:13:17 +08:00
|
|
|
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)
|
2019-11-03 16:18:48 +08:00
|
|
|
|
|
|
|
f := func(key, val interface{}) error {
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
l := mem_lock.NewLock()
|
|
|
|
m1 := NewMap(WithStore(s1), WithLock(l))
|
|
|
|
m2 := NewMap(WithStore(s2), WithLock(l))
|
|
|
|
go func() {
|
|
|
|
m2.Iterate(f)
|
|
|
|
}()
|
|
|
|
m1.Iterate(f)
|
|
|
|
}
|