Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 9884069

Browse files
authored
network/stream: refactor cursors tests (#1786)
1 parent dfc3c98 commit 9884069

File tree

2 files changed

+65
-99
lines changed

2 files changed

+65
-99
lines changed

network/stream/v2/common_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ var (
5757
bucketKeyFileStore = "filestore"
5858
bucketKeyLocalStore = "localstore"
5959
bucketKeyInitialBinIndexes = "bin-indexes"
60-
61-
simContextTimeout = 90 * time.Second
6260
)
6361

6462
func nodeRegistry(sim *simulation.Simulation, id enode.ID) (s *Registry) {

network/stream/v2/cursors_test.go

Lines changed: 65 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -61,61 +61,34 @@ func TestNodesExchangeCorrectBinIndexes(t *testing.T) {
6161
})
6262
defer sim.Close()
6363

64-
ctx, cancel := context.WithTimeout(context.Background(), simContextTimeout)
65-
defer cancel()
6664
_, err := sim.AddNodesAndConnectStar(nodeCount)
6765
if err != nil {
6866
t.Fatal(err)
6967
}
70-
71-
getCursorsCopy := func(sim *simulation.Simulation, idOne, idOther enode.ID) map[string]uint64 {
72-
r := nodeRegistry(sim, idOne)
73-
if r == nil {
74-
return nil
75-
}
76-
p := r.getPeer(idOther)
77-
if p == nil {
78-
return nil
79-
}
80-
return p.getCursorsCopy()
68+
nodeIDs := sim.UpNodeIDs()
69+
if len(nodeIDs) != nodeCount {
70+
t.Error("not enough nodes up")
8171
}
8272

83-
result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) (err error) {
84-
nodeIDs := sim.UpNodeIDs()
85-
if len(nodeIDs) != nodeCount {
86-
return errors.New("not enough nodes up")
87-
}
88-
89-
// periodically check for cursors
90-
for i := 0; i < 100; i++ {
91-
// wait for the nodes to exchange StreamInfo messages
92-
time.Sleep(10 * time.Millisecond)
73+
idOne := nodeIDs[0]
74+
idOther := nodeIDs[1]
9375

94-
idOne := nodeIDs[0]
95-
idOther := nodeIDs[1]
96-
onesCursors := getCursorsCopy(sim, idOne, idOther)
97-
othersCursors := getCursorsCopy(sim, idOther, idOne)
76+
waitForCursors(t, sim, idOne, idOther, true)
77+
waitForCursors(t, sim, idOther, idOne, true)
9878

99-
onesBins := nodeInitialBinIndexes(sim, idOne)
100-
othersBins := nodeInitialBinIndexes(sim, idOther)
79+
onesCursors := getCursorsCopy(sim, idOne, idOther)
80+
othersCursors := getCursorsCopy(sim, idOther, idOne)
10181

102-
err1 := compareNodeBinsToStreams(t, onesCursors, othersBins)
103-
if err1 != nil {
104-
err = err1 // set the resulting error when the loop is done
105-
}
106-
err2 := compareNodeBinsToStreams(t, othersCursors, onesBins)
107-
if err2 != nil {
108-
err = err2 // set the resulting error when the loop is done
109-
}
110-
if err1 == nil && err2 == nil {
111-
return nil
112-
}
113-
}
82+
onesBins := nodeInitialBinIndexes(sim, idOne)
83+
othersBins := nodeInitialBinIndexes(sim, idOther)
11484

115-
return err
116-
})
117-
if result.Error != nil {
118-
t.Fatal(result.Error)
85+
err = compareNodeBinsToStreams(t, onesCursors, othersBins)
86+
if err != nil {
87+
t.Error(err)
88+
}
89+
err = compareNodeBinsToStreams(t, othersCursors, onesBins)
90+
if err != nil {
91+
t.Error(err)
11992
}
12093
}
12194

@@ -136,74 +109,56 @@ func TestNodesCorrectBinsDynamic(t *testing.T) {
136109
})
137110
defer sim.Close()
138111

139-
ctx, cancel := context.WithTimeout(context.Background(), simContextTimeout)
140-
defer cancel()
141112
_, err := sim.AddNodesAndConnectStar(2)
142113
if err != nil {
143114
t.Fatal(err)
144115
}
145116

146-
result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error {
117+
nodeIDs := sim.UpNodeIDs()
118+
if len(nodeIDs) != 2 {
119+
t.Fatal("not enough nodes up")
120+
}
121+
122+
waitForCursors(t, sim, nodeIDs[0], nodeIDs[1], true)
123+
waitForCursors(t, sim, nodeIDs[1], nodeIDs[0], true)
124+
125+
for j := 2; j <= nodeCount; j++ {
126+
// append a node to the simulation
127+
id, err := sim.AddNodes(1)
128+
if err != nil {
129+
t.Fatal(err)
130+
}
131+
err = sim.Net.ConnectNodesStar(id, nodeIDs[0])
132+
if err != nil {
133+
t.Fatal(err)
134+
}
147135
nodeIDs := sim.UpNodeIDs()
148-
if len(nodeIDs) != 2 {
149-
return errors.New("not enough nodes up")
136+
if len(nodeIDs) != j+1 {
137+
t.Fatalf("not enough nodes up. got %d, want %d", len(nodeIDs), j+1)
150138
}
139+
idPivot := nodeIDs[0]
151140

152-
// wait for the nodes to exchange StreamInfo messages
153-
wantCursorsCount := 17
154-
for i := 499; i >= 0; i-- { // wait time 5s
155-
time.Sleep(10 * time.Millisecond)
156-
count1 := nodeRegistry(sim, nodeIDs[0]).getPeer(nodeIDs[1]).cursorsCount()
157-
count2 := nodeRegistry(sim, nodeIDs[1]).getPeer(nodeIDs[0]).cursorsCount()
158-
if count1 >= wantCursorsCount && count2 >= wantCursorsCount {
159-
break
160-
}
161-
if i == 0 {
162-
return fmt.Errorf("got cursors %v and %v, want %v", count1, count2, wantCursorsCount)
163-
}
164-
}
141+
waitForCursors(t, sim, idPivot, nodeIDs[j], true)
142+
waitForCursors(t, sim, nodeIDs[j], idPivot, true)
165143

166-
idPivot := nodeIDs[0]
167144
pivotSyncer := nodeRegistry(sim, idPivot)
168145
pivotKademlia := nodeKademlia(sim, idPivot)
169146
pivotDepth := uint(pivotKademlia.NeighbourhoodDepth())
170147

171-
for j := 2; j <= nodeCount; j++ {
172-
// append a node to the simulation
173-
id, err := sim.AddNodes(1)
174-
if err != nil {
175-
return err
176-
}
177-
err = sim.Net.ConnectNodesStar(id, nodeIDs[0])
178-
if err != nil {
179-
return err
180-
}
181-
nodeIDs := sim.UpNodeIDs()
182-
if len(nodeIDs) != j+1 {
183-
return fmt.Errorf("not enough nodes up. got %d, want %d", len(nodeIDs), j)
184-
}
185-
time.Sleep(50 * time.Millisecond)
186-
idPivot = nodeIDs[0]
187-
for i := 1; i < j; i++ {
188-
idOther := nodeIDs[i]
189-
otherKademlia := sim.MustNodeItem(idOther, simulation.BucketKeyKademlia).(*network.Kademlia)
190-
po := chunk.Proximity(otherKademlia.BaseAddr(), pivotKademlia.BaseAddr())
191-
depth := pivotKademlia.NeighbourhoodDepth()
192-
pivotCursors := pivotSyncer.getPeer(idOther).getCursorsCopy()
193-
194-
// check that the pivot node is interested just in bins >= depth
195-
if po >= depth {
196-
othersBins := nodeInitialBinIndexes(sim, idOther)
197-
if err := compareNodeBinsToStreamsWithDepth(t, pivotCursors, othersBins, pivotDepth); err != nil {
198-
return err
199-
}
148+
for i := 1; i < j; i++ {
149+
idOther := nodeIDs[i]
150+
otherKademlia := sim.MustNodeItem(idOther, simulation.BucketKeyKademlia).(*network.Kademlia)
151+
po := chunk.Proximity(otherKademlia.BaseAddr(), pivotKademlia.BaseAddr())
152+
pivotCursors := pivotSyncer.getPeer(idOther).getCursorsCopy()
153+
154+
// check that the pivot node is interested just in bins >= depth
155+
if po >= int(pivotDepth) {
156+
othersBins := nodeInitialBinIndexes(sim, idOther)
157+
if err := compareNodeBinsToStreamsWithDepth(t, pivotCursors, othersBins, pivotDepth); err != nil {
158+
t.Error(err)
200159
}
201160
}
202161
}
203-
return nil
204-
})
205-
if result.Error != nil {
206-
t.Fatal(result.Error)
207162
}
208163
}
209164

@@ -424,6 +379,19 @@ func waitForCursors(t *testing.T, sim *simulation.Simulation, pivotEnode, lookup
424379
}
425380
}
426381

382+
// getCursorsCopy returns cursors on node idOne for its peer idOther.
383+
func getCursorsCopy(sim *simulation.Simulation, idOne, idOther enode.ID) map[string]uint64 {
384+
r := nodeRegistry(sim, idOne)
385+
if r == nil {
386+
return nil
387+
}
388+
p := r.getPeer(idOther)
389+
if p == nil {
390+
return nil
391+
}
392+
return p.getCursorsCopy()
393+
}
394+
427395
// compareNodeBinsToStreams checks that the values on `onesCursors` correlate to the values in `othersBins`
428396
// onesCursors represents the stream cursors that node A knows about node B (i.e. they shoud reflect directly in this case
429397
// the values which node B retrieved from its local store)

0 commit comments

Comments
 (0)