Skip to content

Commit 494707f

Browse files
committed
fix tests
1 parent 5d9c9af commit 494707f

File tree

8 files changed

+47
-38
lines changed

8 files changed

+47
-38
lines changed

engine/execution/pruner/core_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/cockroachdb/pebble/v2"
10+
"github.com/jordanschalm/lockctx"
1011
"github.com/stretchr/testify/require"
1112

1213
"github.com/onflow/flow-go/model/flow"
@@ -46,35 +47,34 @@ func TestLoopPruneExecutionDataFromRootToLatestSealed(t *testing.T) {
4647
// indexed by height
4748
chunks := make([]*verification.VerifiableChunkData, lastFinalizedHeight+2)
4849
parentID := genesis.ID()
49-
lctxGenesis := lockManager.NewContext()
50-
require.NoError(t, lctxGenesis.AcquireLock(storage.LockInsertBlock))
51-
require.NoError(t, db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
52-
// By convention, root block has no proposer signature - implementation has to handle this edge case
53-
return blockstore.BatchStore(lctxGenesis, rw, &flow.Proposal{Block: *genesis, ProposerSigData: nil})
54-
}))
55-
lctxGenesis.Release()
50+
unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
51+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
52+
// By convention, root block has no proposer signature - implementation has to handle this edge case
53+
return blockstore.BatchStore(lctx, rw, &flow.Proposal{Block: *genesis, ProposerSigData: nil})
54+
})
55+
})
5656

5757
for i := 1; i <= lastFinalizedHeight; i++ {
5858
chunk, block := unittest.VerifiableChunkDataFixture(0, func(headerBody *flow.HeaderBody) {
5959
headerBody.Height = uint64(i)
6060
headerBody.ParentID = parentID
6161
})
6262
chunks[i] = chunk // index by height
63-
lctxBlock := lockManager.NewContext()
64-
require.NoError(t, lctxBlock.AcquireLock(storage.LockInsertBlock))
65-
require.NoError(t, db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
66-
return blockstore.BatchStore(lctxBlock, rw, unittest.ProposalFromBlock(block))
67-
}))
68-
lctxBlock.Release()
69-
lctxFinality := lockManager.NewContext()
70-
require.NoError(t, lctxFinality.AcquireLock(storage.LockFinalizeBlock))
71-
require.NoError(t, db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
72-
return operation.IndexFinalizedBlockByHeight(lctxFinality, rw, chunk.Header.Height, chunk.Header.ID())
73-
}))
74-
lctxFinality.Release()
63+
unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
64+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
65+
return blockstore.BatchStore(lctx, rw, unittest.ProposalFromBlock(block))
66+
})
67+
})
68+
unittest.WithLock(t, lockManager, storage.LockFinalizeBlock, func(lctx lockctx.Context) error {
69+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
70+
return operation.IndexFinalizedBlockByHeight(lctx, rw, chunk.Header.Height, chunk.Header.ID())
71+
})
72+
})
7573
require.NoError(t, results.Store(chunk.Result))
7674
require.NoError(t, results.Index(chunk.Result.BlockID, chunk.Result.ID()))
77-
require.NoError(t, chunkDataPacks.Store([]*flow.ChunkDataPack{chunk.ChunkDataPack}))
75+
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
76+
return chunkDataPacks.Store(lctx, []*flow.ChunkDataPack{chunk.ChunkDataPack})
77+
})
7878
_, storeErr := collections.Store(chunk.ChunkDataPack.Collection)
7979
require.NoError(t, storeErr)
8080
// verify that chunk data pack fixture can be found by the result

module/block_iterator/executor/executor_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/cockroachdb/pebble/v2"
11+
"github.com/jordanschalm/lockctx"
1112
"github.com/stretchr/testify/require"
1213

1314
"github.com/onflow/flow-go/model/flow"
@@ -23,6 +24,7 @@ import (
2324
// verify the executor is able to iterate through all blocks from the iterator.
2425
func TestExecute(t *testing.T) {
2526
unittest.RunWithPebbleDB(t, func(db *pebble.DB) {
27+
lockManager := storage.NewTestingLockManager()
2628
blockCount := 10
2729

2830
// prepare data
@@ -39,9 +41,11 @@ func TestExecute(t *testing.T) {
3941
// store the chunk data packs to be pruned later
4042
for _, cdp := range cdps {
4143
sc := storage.ToStoredChunkDataPack(cdp)
42-
require.NoError(t, pdb.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
43-
return operation.InsertChunkDataPack(rw.Writer(), sc)
44-
}))
44+
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
45+
return pdb.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
46+
return operation.InsertChunkDataPack(lctx, rw, sc)
47+
})
48+
})
4549
}
4650

4751
// it's ok the chunk ids is used as block ids, because the iterator
@@ -72,6 +76,7 @@ func TestExecute(t *testing.T) {
7276
// verify the pruning can be interrupted and resumed
7377
func TestExecuteCanBeResumed(t *testing.T) {
7478
unittest.RunWithPebbleDB(t, func(db *pebble.DB) {
79+
lockManager := storage.NewTestingLockManager()
7580
blockCount := 10
7681

7782
cdps := make([]*flow.ChunkDataPack, 0, blockCount)
@@ -87,9 +92,11 @@ func TestExecuteCanBeResumed(t *testing.T) {
8792
// store the chunk data packs to be pruned later
8893
for _, cdp := range cdps {
8994
sc := storage.ToStoredChunkDataPack(cdp)
90-
require.NoError(t, pdb.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
91-
return operation.InsertChunkDataPack(rw.Writer(), sc)
92-
}))
95+
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
96+
return pdb.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
97+
return operation.InsertChunkDataPack(lctx, rw, sc)
98+
})
99+
})
93100
}
94101

95102
// it's ok the chunk ids is used as block ids, because the iterator

module/pruner/pruners/chunk_data_pack_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/cockroachdb/pebble/v2"
9+
"github.com/jordanschalm/lockctx"
910
"github.com/stretchr/testify/require"
1011

1112
"github.com/onflow/flow-go/module/metrics"
@@ -18,6 +19,7 @@ import (
1819
func TestChunkDataPackPruner(t *testing.T) {
1920

2021
unittest.RunWithPebbleDB(t, func(pebbleDB *pebble.DB) {
22+
lockManager := storage.NewTestingLockManager()
2123
m := metrics.NewNoopCollector()
2224
db := pebbleimpl.ToDB(pebbleDB)
2325
results := store.NewExecutionResults(m, db)
@@ -29,7 +31,9 @@ func TestChunkDataPackPruner(t *testing.T) {
2931
// store the chunks
3032
cdp1, result1 := unittest.ChunkDataPacksFixtureAndResult()
3133
require.NoError(t, results.Store(result1))
32-
require.NoError(t, chunks.Store(cdp1))
34+
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
35+
return chunks.Store(lctx, cdp1)
36+
})
3337

3438
pruner := NewChunkDataPackPruner(chunks, results)
3539

storage/chunk_data_packs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55

66
"github.com/jordanschalm/lockctx"
7+
78
"github.com/onflow/flow-go/model/flow"
89
)
910

storage/operation/chunk_data_packs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/jordanschalm/lockctx"
8+
89
"github.com/onflow/flow-go/model/flow"
910
"github.com/onflow/flow-go/storage"
1011
)

storage/operation/chunk_data_packs_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ func TestChunkDataPack(t *testing.T) {
3131
})
3232

3333
t.Run("Save", func(t *testing.T) {
34-
err := db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
35-
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
34+
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
35+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
3636
return operation.InsertChunkDataPack(lctx, rw, expected)
3737
})
38-
return nil
3938
})
40-
require.NoError(t, err)
4139

4240
var actual storage.StoredChunkDataPack
43-
err = operation.RetrieveChunkDataPack(db.Reader(), expected.ChunkID, &actual)
41+
err := operation.RetrieveChunkDataPack(db.Reader(), expected.ChunkID, &actual)
4442
assert.NoError(t, err)
4543

4644
assert.Equal(t, *expected, actual)

storage/store/chunk_data_packs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/jordanschalm/lockctx"
7+
78
"github.com/onflow/flow-go/model/flow"
89
"github.com/onflow/flow-go/module"
910
"github.com/onflow/flow-go/module/metrics"

storage/store/chunk_data_packs_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ func TestChunkDataPacks_Store(t *testing.T) {
2323
WithChunkDataPacks(t, 100, func(t *testing.T, chunkDataPacks []*flow.ChunkDataPack, chunkDataPackStore *store.ChunkDataPacks, _ *pebble.DB, lockManager storage.LockManager) {
2424
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
2525
require.NoError(t, chunkDataPackStore.Store(lctx, chunkDataPacks))
26-
require.NoError(t, chunkDataPackStore.Store(lctx, chunkDataPacks))
27-
return nil
26+
return chunkDataPackStore.Store(lctx, chunkDataPacks)
2827
})
2928
})
3029
}
@@ -51,8 +50,7 @@ func TestChunkDataPack_Remove(t *testing.T) {
5150
}
5251

5352
unittest.WithLock(t, lockManager, storage.LockInsertChunkDataPack, func(lctx lockctx.Context) error {
54-
require.NoError(t, chunkDataPackStore.Store(lctx, chunkDataPacks))
55-
return nil
53+
return chunkDataPackStore.Store(lctx, chunkDataPacks)
5654
})
5755
require.NoError(t, chunkDataPackStore.Remove(chunkIDs))
5856

@@ -96,8 +94,7 @@ func TestChunkDataPacks_StoreTwice(t *testing.T) {
9694
require.Equal(t, c, c2)
9795
}
9896

99-
require.NoError(t, store1.Store(lctx, chunkDataPacks))
100-
return nil
97+
return store1.Store(lctx, chunkDataPacks)
10198
})
10299
})
103100
}

0 commit comments

Comments
 (0)