Skip to content

Commit 5262607

Browse files
committed
Update rpc tests. Remove peer mock.
1 parent 7319448 commit 5262607

21 files changed

+126
-151
lines changed

cmd/multi/daemon.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ func daemonAction(c *cli.Context) error {
5959
return err
6060
}
6161

62-
client, err := peer.New(c.Context, dstore, config)
62+
node, err := peer.New(c.Context, dstore, config)
6363
if err != nil {
6464
return err
6565
}
6666

6767
// ensure any changes made offline will be published
68-
if err := client.Authors().Publish(c.Context); err != nil {
68+
if err := node.Authors().Publish(c.Context); err != nil {
6969
return err
7070
}
7171

72-
go web.ListenAndServe(client)
73-
go rpc.ListenAndServe(client)
72+
go web.ListenAndServe(node)
73+
go rpc.ListenAndServe(node)
7474

7575
fmt.Printf(daemonBanner)
76-
fmt.Printf("Peer ID: %s\n", client.PeerID().Pretty())
76+
fmt.Printf("Peer ID: %s\n", node.PeerID().Pretty())
7777
fmt.Printf("Web URL: %s\n", web.BindAddr)
7878

7979
quit := make(chan os.Signal, 1)

peer/authors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/multiverse-vcs/go-multiverse/p2p"
1111
)
1212

13-
type authors Client
13+
type authors Node
1414

1515
// Publish advertises the local author.
1616
func (a *authors) Publish(ctx context.Context) error {

peer/mock.go

-36
This file was deleted.

peer/peer.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"github.com/multiverse-vcs/go-multiverse/p2p"
2222
)
2323

24-
// Client manages peer services.
25-
type Client struct {
24+
// Node manages peer services.
25+
type Node struct {
2626
ipld.DAGService
2727
host host.Host
2828
config *Config
@@ -35,7 +35,7 @@ type Client struct {
3535
}
3636

3737
// New returns a peer with a p2p host and persistent storage.
38-
func New(ctx context.Context, dstore datastore.Batching, config *Config) (*Client, error) {
38+
func New(ctx context.Context, dstore datastore.Batching, config *Config) (*Node, error) {
3939
priv, err := p2p.DecodeKey(config.PrivateKey)
4040
if err != nil {
4141
return nil, err
@@ -72,7 +72,7 @@ func New(ctx context.Context, dstore datastore.Batching, config *Config) (*Clien
7272
return nil, err
7373
}
7474

75-
return &Client{
75+
return &Node{
7676
DAGService: dag,
7777
host: host,
7878
config: config,
@@ -86,21 +86,21 @@ func New(ctx context.Context, dstore datastore.Batching, config *Config) (*Clien
8686
}
8787

8888
// Authors returns the authors api.
89-
func (c *Client) Authors() *authors {
90-
return (*authors)(c)
89+
func (n *Node) Authors() *authors {
90+
return (*authors)(n)
9191
}
9292

9393
// Config returns the peer config.
94-
func (c *Client) Config() *Config {
95-
return c.config
94+
func (n *Node) Config() *Config {
95+
return n.config
9696
}
9797

98-
// PeerID returns the peer id of the client.
99-
func (c *Client) PeerID() peer.ID {
100-
return c.host.ID()
98+
// PeerID returns the node peer id.
99+
func (n *Node) PeerID() peer.ID {
100+
return n.host.ID()
101101
}
102102

103103
// ResolvePath resolves the node from the given path.
104-
func (c *Client) ResolvePath(ctx context.Context, p path.Path) (ipld.Node, error) {
105-
return c.resolv.ResolvePath(ctx, p)
104+
func (n *Node) ResolvePath(ctx context.Context, p path.Path) (ipld.Node, error) {
105+
return n.resolv.ResolvePath(ctx, p)
106106
}

rpc/branch.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ type BranchReply struct {
2727
// ListBranches returns the repo branches.
2828
func (s *Service) ListBranches(args *BranchArgs, reply *BranchReply) error {
2929
ctx := context.Background()
30-
cfg := s.client.Config()
30+
cfg := s.node.Config()
3131

3232
id, ok := cfg.Author.Repositories[args.Name]
3333
if !ok {
3434
return errors.New("repository does not exist")
3535
}
3636

37-
repo, err := data.GetRepository(ctx, s.client, id)
37+
repo, err := data.GetRepository(ctx, s.node, id)
3838
if err != nil {
3939
return err
4040
}
@@ -46,14 +46,14 @@ func (s *Service) ListBranches(args *BranchArgs, reply *BranchReply) error {
4646
// CreateBranch creates a new branch.
4747
func (s *Service) CreateBranch(args *BranchArgs, reply *BranchReply) error {
4848
ctx := context.Background()
49-
cfg := s.client.Config()
49+
cfg := s.node.Config()
5050

5151
id, ok := cfg.Author.Repositories[args.Name]
5252
if !ok {
5353
return errors.New("repository does not exist")
5454
}
5555

56-
repo, err := data.GetRepository(ctx, s.client, id)
56+
repo, err := data.GetRepository(ctx, s.node, id)
5757
if err != nil {
5858
return err
5959
}
@@ -69,7 +69,7 @@ func (s *Service) CreateBranch(args *BranchArgs, reply *BranchReply) error {
6969
repo.Branches[args.Branch] = args.Head
7070
reply.Branches = repo.Branches
7171

72-
id, err = data.AddRepository(ctx, s.client, repo)
72+
id, err = data.AddRepository(ctx, s.node, repo)
7373
if err != nil {
7474
return err
7575
}
@@ -81,20 +81,20 @@ func (s *Service) CreateBranch(args *BranchArgs, reply *BranchReply) error {
8181
return err
8282
}
8383

84-
return s.client.Authors().Publish(ctx)
84+
return s.node.Authors().Publish(ctx)
8585
}
8686

8787
// DeleteBranch deletes an existing branch.
8888
func (s *Service) DeleteBranch(args *BranchArgs, reply *BranchReply) error {
8989
ctx := context.Background()
90-
cfg := s.client.Config()
90+
cfg := s.node.Config()
9191

9292
id, ok := cfg.Author.Repositories[args.Name]
9393
if !ok {
9494
return errors.New("repository does not exist")
9595
}
9696

97-
repo, err := data.GetRepository(ctx, s.client, id)
97+
repo, err := data.GetRepository(ctx, s.node, id)
9898
if err != nil {
9999
return err
100100
}
@@ -110,7 +110,7 @@ func (s *Service) DeleteBranch(args *BranchArgs, reply *BranchReply) error {
110110
delete(repo.Branches, args.Branch)
111111
reply.Branches = repo.Branches
112112

113-
id, err = data.AddRepository(ctx, s.client, repo)
113+
id, err = data.AddRepository(ctx, s.node, repo)
114114
if err != nil {
115115
return err
116116
}
@@ -122,5 +122,5 @@ func (s *Service) DeleteBranch(args *BranchArgs, reply *BranchReply) error {
122122
return err
123123
}
124124

125-
return s.client.Authors().Publish(ctx)
125+
return s.node.Authors().Publish(ctx)
126126
}

rpc/branch_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import (
66
"testing"
77

88
"github.com/multiverse-vcs/go-multiverse/data"
9-
"github.com/multiverse-vcs/go-multiverse/peer"
109
)
1110

1211
func TestListBranches(t *testing.T) {
1312
ctx := context.Background()
1413

15-
mock, err := peer.Mock()
14+
node, err := makeNode(ctx)
1615
if err != nil {
17-
t.Fatal("failed to create peer")
16+
t.Fatal("failed to create peer node")
1817
}
1918

2019
json, err := ioutil.ReadFile("testdata/repository.json")
@@ -27,19 +26,19 @@ func TestListBranches(t *testing.T) {
2726
t.Fatal("failed to parse repository json")
2827
}
2928

30-
id, err := data.AddRepository(ctx, mock, repo)
29+
id, err := data.AddRepository(ctx, node, repo)
3130
if err != nil {
3231
t.Fatal("failed to create repository")
3332
}
34-
mock.Config().Author.Repositories[repo.Name] = id
33+
node.Config().Author.Repositories["test"] = id
3534

36-
client, err := connect(mock)
35+
client, err := makeClient(node)
3736
if err != nil {
3837
t.Fatal("failed to connect to rpc server")
3938
}
4039

4140
args := BranchArgs{
42-
Name: repo.Name,
41+
Name: "test",
4342
}
4443

4544
var reply BranchReply

rpc/checkout.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ type CheckoutReply struct{}
3232
// Checkout copies the tree of an existing commit to the root.
3333
func (s *Service) Checkout(args *CheckoutArgs, reply *CheckoutReply) error {
3434
ctx := context.Background()
35-
cfg := s.client.Config()
35+
cfg := s.node.Config()
3636

37-
equal, err := core.Equal(ctx, s.client, args.Root, args.Ignore, args.Index)
37+
equal, err := core.Equal(ctx, s.node, args.Root, args.Ignore, args.Index)
3838
if err != nil {
3939
return err
4040
}
@@ -48,7 +48,7 @@ func (s *Service) Checkout(args *CheckoutArgs, reply *CheckoutReply) error {
4848
return errors.New("repository does not exist")
4949
}
5050

51-
repo, err := data.GetRepository(ctx, s.client, id)
51+
repo, err := data.GetRepository(ctx, s.node, id)
5252
if err != nil {
5353
return err
5454
}
@@ -58,7 +58,7 @@ func (s *Service) Checkout(args *CheckoutArgs, reply *CheckoutReply) error {
5858
return errors.New("branch does not exist")
5959
}
6060

61-
child, err := core.IsAncestor(ctx, s.client, head, args.ID)
61+
child, err := core.IsAncestor(ctx, s.node, head, args.ID)
6262
if err != nil {
6363
return err
6464
}
@@ -67,15 +67,15 @@ func (s *Service) Checkout(args *CheckoutArgs, reply *CheckoutReply) error {
6767
return errors.New("commit is not in branch")
6868
}
6969

70-
commit, err := data.GetCommit(ctx, s.client, args.ID)
70+
commit, err := data.GetCommit(ctx, s.node, args.ID)
7171
if err != nil {
7272
return err
7373
}
7474

75-
tree, err := s.client.Get(ctx, commit.Tree)
75+
tree, err := s.node.Get(ctx, commit.Tree)
7676
if err != nil {
7777
return err
7878
}
7979

80-
return unixfs.Write(ctx, s.client, args.Root, tree)
80+
return unixfs.Write(ctx, s.node, args.Root, tree)
8181
}

rpc/clone.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ func (s *Service) Clone(args *CloneArgs, reply *CloneReply) error {
3939
ctx := context.Background()
4040

4141
if args.Dir == "" {
42-
return errors.New("dir is required")
42+
return errors.New("dir cannot be empty")
4343
}
4444

45-
repo, err := data.GetRepository(ctx, s.client, args.ID)
45+
repo, err := data.GetRepository(ctx, s.node, args.ID)
4646
if err != nil {
4747
return err
4848
}
@@ -52,16 +52,16 @@ func (s *Service) Clone(args *CloneArgs, reply *CloneReply) error {
5252
return errors.New("branch does not exist")
5353
}
5454

55-
if err := merkledag.FetchGraphWithDepthLimit(ctx, id, args.Limit, s.client); err != nil {
55+
if err := merkledag.FetchGraphWithDepthLimit(ctx, id, args.Limit, s.node); err != nil {
5656
return err
5757
}
5858

59-
commit, err := data.GetCommit(ctx, s.client, id)
59+
commit, err := data.GetCommit(ctx, s.node, id)
6060
if err != nil {
6161
return err
6262
}
6363

64-
tree, err := s.client.Get(ctx, commit.Tree)
64+
tree, err := s.node.Get(ctx, commit.Tree)
6565
if err != nil {
6666
return err
6767
}
@@ -74,5 +74,5 @@ func (s *Service) Clone(args *CloneArgs, reply *CloneReply) error {
7474
reply.ID = id
7575
reply.Root = path
7676

77-
return unixfs.Write(ctx, s.client, path, tree)
77+
return unixfs.Write(ctx, s.node, path, tree)
7878
}

rpc/commit.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type CommitReply struct {
3434
// Commit records changes to the repo
3535
func (s *Service) Commit(args *CommitArgs, reply *CommitReply) error {
3636
ctx := context.Background()
37-
cfg := s.client.Config()
37+
cfg := s.node.Config()
3838

3939
if args.Branch == "" {
4040
return errors.New("branch cannot be empty")
@@ -45,7 +45,7 @@ func (s *Service) Commit(args *CommitArgs, reply *CommitReply) error {
4545
return errors.New("repository does not exist")
4646
}
4747

48-
repo, err := data.GetRepository(ctx, s.client, id)
48+
repo, err := data.GetRepository(ctx, s.node, id)
4949
if err != nil {
5050
return err
5151
}
@@ -55,7 +55,7 @@ func (s *Service) Commit(args *CommitArgs, reply *CommitReply) error {
5555
return errors.New("branch is ahead of parent")
5656
}
5757

58-
tree, err := unixfs.Add(ctx, s.client, args.Root, args.Ignore)
58+
tree, err := unixfs.Add(ctx, s.node, args.Root, args.Ignore)
5959
if err != nil {
6060
return err
6161
}
@@ -65,15 +65,15 @@ func (s *Service) Commit(args *CommitArgs, reply *CommitReply) error {
6565
commit.Parents = append(commit.Parents, args.Parent)
6666
}
6767

68-
head, err = data.AddCommit(ctx, s.client, commit)
68+
head, err = data.AddCommit(ctx, s.node, commit)
6969
if err != nil {
7070
return err
7171
}
7272

7373
repo.Branches[args.Branch] = head
7474
reply.Index = head
7575

76-
id, err = data.AddRepository(ctx, s.client, repo)
76+
id, err = data.AddRepository(ctx, s.node, repo)
7777
if err != nil {
7878
return err
7979
}
@@ -85,5 +85,5 @@ func (s *Service) Commit(args *CommitArgs, reply *CommitReply) error {
8585
return err
8686
}
8787

88-
return s.client.Authors().Publish(ctx)
88+
return s.node.Authors().Publish(ctx)
8989
}

0 commit comments

Comments
 (0)