Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions amt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/rand"
"os"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -44,14 +45,37 @@ func TestMain(m *testing.M) {

type mockBlocks struct {
data map[cid.Cid]block.Block
dataMu sync.Mutex
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required for accessing the blockstore in parallel (as is done by ParallelDiff), the race detector throws a fit otherwise.

getCount, putCount int
delay time.Duration
}

func newMockBlocks() *mockBlocks {
return &mockBlocks{make(map[cid.Cid]block.Block), 0, 0}
type mockBlocksOpt func(m *mockBlocks)

func WithGetDelay(d time.Duration) mockBlocksOpt {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used for (poorly) simulating a disk read

return func(m *mockBlocks) {
m.delay = d
}
}

func newMockBlocks(opts ...mockBlocksOpt) *mockBlocks {
mb := &mockBlocks{
data: make(map[cid.Cid]block.Block),
dataMu: sync.Mutex{},
getCount: 0,
putCount: 0,
delay: 0,
}
for _, opt := range opts {
opt(mb)
}
return mb
}

func (mb *mockBlocks) Get(c cid.Cid) (block.Block, error) {
time.Sleep(mb.delay)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skip the sleep if mb.delay == 0

mb.dataMu.Lock()
defer mb.dataMu.Unlock()
d, ok := mb.data[c]
mb.getCount++
if ok {
Expand All @@ -61,6 +85,8 @@ func (mb *mockBlocks) Get(c cid.Cid) (block.Block, error) {
}

func (mb *mockBlocks) Put(b block.Block) error {
mb.dataMu.Lock()
defer mb.dataMu.Unlock()
mb.putCount++
mb.data[b.Cid()] = b
return nil
Expand Down
1 change: 0 additions & 1 deletion diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"golang.org/x/xerrors"

"github.com/ipfs/go-cid"

cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
)
Expand Down
Loading