micro/sync/map_test.go
罗泽轩 8579c8b321 avoid deadlock in syncMap.Iterate (#909)
Previously, when syncMap iterates a list of records which have the same
content in different order, a deadlock might happen. By enforcing a certain
order, the deadlock can be avoided.
2019-11-03 08:18:48 +00:00

40 lines
710 B
Go

package sync
import (
"testing"
"time"
store "github.com/micro/go-micro/store"
mem_store "github.com/micro/go-micro/store/memory"
mem_lock "github.com/micro/go-micro/sync/lock/memory"
)
func TestIterate(t *testing.T) {
s1 := mem_store.NewStore()
s2 := mem_store.NewStore()
recA := &store.Record{
Key: "A",
Value: nil,
}
recB := &store.Record{
Key: "B",
Value: nil,
}
s1.Write(recA)
s1.Write(recB)
s2.Write(recB)
s2.Write(recA)
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)
}