Skip to content

Commit

Permalink
Test module: allow the MockDAGService to be write-only.
Browse files Browse the repository at this point in the history
The adder needs a write-only DAGService.
  • Loading branch information
hsanjuan committed Jun 16, 2022
1 parent e0ecb76 commit f82f60e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions test/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package test
import (
"context"
"encoding/hex"
"errors"
"io"
"math/rand"
"os"
"path/filepath"
"sync"
"testing"

"github.com/ipfs-cluster/ipfs-cluster/api"
files "github.com/ipfs/go-ipfs-files"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs-cluster/ipfs-cluster/api"

cid "github.com/ipfs/go-cid"
)
Expand Down Expand Up @@ -287,15 +288,24 @@ func (sth *ShardingTestHelper) makeRandFile(t *testing.T, kbs int) os.FileInfo {
type MockDAGService struct {
mu sync.Mutex
Nodes map[cid.Cid]format.Node

writeOnly bool
}

// NewMockDAGService returns an in-memory DAG Service.
func NewMockDAGService() *MockDAGService {
return &MockDAGService{Nodes: make(map[cid.Cid]format.Node)}
func NewMockDAGService(writeOnly bool) *MockDAGService {
return &MockDAGService{
Nodes: make(map[cid.Cid]format.Node),
writeOnly: writeOnly,
}
}

// Get reads a node.
func (d *MockDAGService) Get(ctx context.Context, cid cid.Cid) (format.Node, error) {
if d.writeOnly {
return nil, errors.New("dagservice: block not found")
}

d.mu.Lock()
defer d.mu.Unlock()
if n, ok := d.Nodes[cid]; ok {
Expand All @@ -306,6 +316,13 @@ func (d *MockDAGService) Get(ctx context.Context, cid cid.Cid) (format.Node, err

// GetMany reads many nodes.
func (d *MockDAGService) GetMany(ctx context.Context, cids []cid.Cid) <-chan *format.NodeOption {
if d.writeOnly {
out := make(chan *format.NodeOption, 1)
out <- &format.NodeOption{Err: errors.New("failed to fetch all nodes")}
close(out)
return out
}

d.mu.Lock()
defer d.mu.Unlock()
out := make(chan *format.NodeOption, len(cids))
Expand Down

0 comments on commit f82f60e

Please sign in to comment.