@@ -33,6 +33,7 @@ type fakeHandler struct {
33
33
mtx * sync.Mutex
34
34
localPeerID p2p.Peer
35
35
synced map [addedKey ]struct {}
36
+ committed map [string ]struct {}
36
37
}
37
38
38
39
func (fh * fakeHandler ) Receive (k rangesync.KeyBytes , peer p2p.Peer ) (bool , error ) {
@@ -50,11 +51,21 @@ func (fh *fakeHandler) Commit(peer p2p.Peer, base, new multipeer.OrderedSet) err
50
51
fh .mtx .Lock ()
51
52
defer fh .mtx .Unlock ()
52
53
for k := range fh .synced {
53
- base .( * multipeer. DumbSet ). AddUnchecked ( rangesync . KeyBytes ( k .key ))
54
+ fh . committed [ k .key ] = struct {}{}
54
55
}
56
+ clear (fh .synced )
55
57
return nil
56
58
}
57
59
60
+ func (fh * fakeHandler ) committedItems () (items []rangesync.KeyBytes ) {
61
+ fh .mtx .Lock ()
62
+ defer fh .mtx .Unlock ()
63
+ for k := range fh .committed {
64
+ items = append (items , rangesync .KeyBytes (k ))
65
+ }
66
+ return items
67
+ }
68
+
58
69
func TestP2P (t * testing.T ) {
59
70
const (
60
71
numNodes = 4
@@ -65,8 +76,8 @@ func TestP2P(t *testing.T) {
65
76
logger := zaptest .NewLogger (t )
66
77
mesh , err := mocknet .FullMeshConnected (numNodes )
67
78
require .NoError (t , err )
68
- synced := make (map [addedKey ]struct {})
69
79
hs := make ([]* sync2.P2PHashSync , numNodes )
80
+ handlers := make ([]* fakeHandler , numNodes )
70
81
initialSet := make ([]rangesync.KeyBytes , numHashes )
71
82
for n := range initialSet {
72
83
initialSet [n ] = rangesync .RandomKeyBytes (32 )
@@ -85,10 +96,11 @@ func TestP2P(t *testing.T) {
85
96
cfg .EnableActiveSync = true
86
97
cfg .SyncInterval = 100 * time .Millisecond
87
98
host := mesh .Hosts ()[n ]
88
- handler : = & fakeHandler {
99
+ handlers [ n ] = & fakeHandler {
89
100
mtx : & mtx ,
90
101
localPeerID : host .ID (),
91
- synced : synced ,
102
+ synced : make (map [addedKey ]struct {}),
103
+ committed : make (map [string ]struct {}),
92
104
}
93
105
os := multipeer .NewDumbHashSet ()
94
106
d := rangesync .NewDispatcher (logger )
@@ -98,7 +110,7 @@ func TestP2P(t *testing.T) {
98
110
eg .Go (func () error { return srv .Run (ctx ) })
99
111
hs [n ] = sync2 .NewP2PHashSync (
100
112
logger .Named (fmt .Sprintf ("node%d" , n )),
101
- d , "test" , os , keyLen , maxDepth , ps , handler , cfg , srv )
113
+ d , "test" , os , keyLen , maxDepth , ps , handlers [ n ] , cfg , srv )
102
114
require .NoError (t , hs [n ].Load ())
103
115
is := hs [n ].Set ().(* multipeer.DumbSet )
104
116
is .SetAllowMultiReceive (true )
@@ -112,12 +124,15 @@ func TestP2P(t *testing.T) {
112
124
}
113
125
114
126
require .Eventually (t , func () bool {
115
- for _ , hsync := range hs {
127
+ for n , hsync := range hs {
116
128
// use a snapshot to avoid races
117
129
if ! hsync .Synced () {
118
130
return false
119
131
}
120
132
os := hsync .Set ().Copy (false )
133
+ for _ , k := range handlers [n ].committedItems () {
134
+ os .(* multipeer.DumbSet ).AddUnchecked (k )
135
+ }
121
136
empty , err := os .Empty ()
122
137
require .NoError (t , err )
123
138
if empty {
@@ -134,9 +149,13 @@ func TestP2P(t *testing.T) {
134
149
return true
135
150
}, 30 * time .Second , 300 * time .Millisecond )
136
151
137
- for _ , hsync := range hs {
152
+ for n , hsync := range hs {
138
153
hsync .Stop ()
139
- actualItems , err := hsync .Set ().Items ().Collect ()
154
+ os := hsync .Set ().Copy (false )
155
+ for _ , k := range handlers [n ].committedItems () {
156
+ os .(* multipeer.DumbSet ).AddUnchecked (k )
157
+ }
158
+ actualItems , err := os .Items ().Collect ()
140
159
require .NoError (t , err )
141
160
require .ElementsMatch (t , initialSet , actualItems )
142
161
}
0 commit comments