@@ -17,6 +17,7 @@ limitations under the License.
17
17
package fsblkstorage
18
18
19
19
import (
20
+ "sync"
20
21
"testing"
21
22
"time"
22
23
@@ -74,6 +75,68 @@ func TestBlockItrClose(t *testing.T) {
74
75
testutil .AssertNil (t , bh )
75
76
}
76
77
78
+ func TestBlockItrCloseWithoutRetrieve (t * testing.T ) {
79
+ env := newTestEnv (t , NewConf (testPath (), 0 ))
80
+ defer env .Cleanup ()
81
+ blkfileMgrWrapper := newTestBlockfileWrapper (env , "testLedger" )
82
+ defer blkfileMgrWrapper .close ()
83
+ blkfileMgr := blkfileMgrWrapper .blockfileMgr
84
+ blocks := testutil .ConstructTestBlocks (t , 5 )
85
+ blkfileMgrWrapper .addBlocks (blocks )
86
+
87
+ itr , err := blkfileMgr .retrieveBlocks (2 )
88
+ testutil .AssertNoError (t , err , "" )
89
+ itr .Close ()
90
+ }
91
+
92
+ func TestCloseMultipleItrsWaitForFutureBlock (t * testing.T ) {
93
+ env := newTestEnv (t , NewConf (testPath (), 0 ))
94
+ defer env .Cleanup ()
95
+ blkfileMgrWrapper := newTestBlockfileWrapper (env , "testLedger" )
96
+ defer blkfileMgrWrapper .close ()
97
+ blkfileMgr := blkfileMgrWrapper .blockfileMgr
98
+ blocks := testutil .ConstructTestBlocks (t , 10 )
99
+ blkfileMgrWrapper .addBlocks (blocks [:5 ])
100
+
101
+ wg := & sync.WaitGroup {}
102
+ wg .Add (2 )
103
+ itr1 , err := blkfileMgr .retrieveBlocks (7 )
104
+ testutil .AssertNoError (t , err , "" )
105
+ // itr1 does not retrieve any block because it closes before new blocks are added
106
+ go iterateInBackground (t , itr1 , 9 , wg , []uint64 {})
107
+
108
+ itr2 , err := blkfileMgr .retrieveBlocks (8 )
109
+ testutil .AssertNoError (t , err , "" )
110
+ // itr2 retrieves two blocks 8 and 9. Because it started waiting for 8 and quits at 9
111
+ go iterateInBackground (t , itr2 , 9 , wg , []uint64 {8 , 9 })
112
+
113
+ // sleep for the background iterators to get started
114
+ time .Sleep (2 * time .Second )
115
+ itr1 .Close ()
116
+ blkfileMgrWrapper .addBlocks (blocks [5 :])
117
+ wg .Wait ()
118
+ }
119
+
120
+ func iterateInBackground (t * testing.T , itr * blocksItr , quitAfterBlkNum uint64 , wg * sync.WaitGroup , expectedBlockNums []uint64 ) {
121
+ defer wg .Done ()
122
+ retrievedBlkNums := []uint64 {}
123
+ defer func () { testutil .AssertEquals (t , retrievedBlkNums , expectedBlockNums ) }()
124
+
125
+ for {
126
+ blk , err := itr .Next ()
127
+ testutil .AssertNoError (t , err , "" )
128
+ if blk == nil {
129
+ return
130
+ }
131
+ blkNum := blk .(* common.Block ).Header .Number
132
+ retrievedBlkNums = append (retrievedBlkNums , blkNum )
133
+ t .Logf ("blk.Num=%d" , blk .(* common.Block ).Header .Number )
134
+ if blkNum == quitAfterBlkNum {
135
+ return
136
+ }
137
+ }
138
+ }
139
+
77
140
func testIterateAndVerify (t * testing.T , itr * blocksItr , blocks []* common.Block , doneChan chan bool ) {
78
141
blocksIterated := 0
79
142
for {
0 commit comments