2018-08-23 16:20:11 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-08-23 16:20:11 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
|
|
|
"storj.io/storj/pkg/dht/mocks"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-23 16:20:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestQuery(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
mockDHT := mock_dht.NewMockDHT(ctrl)
|
|
|
|
mockRT := mock_dht.NewMockRoutingTable(ctrl)
|
|
|
|
s := &Server{dht: mockDHT}
|
2018-11-29 18:39:27 +00:00
|
|
|
sender := &pb.Node{Id: teststorj.NodeIDFromString("A")}
|
|
|
|
target := &pb.Node{Id: teststorj.NodeIDFromString("B")}
|
|
|
|
node := &pb.Node{Id: teststorj.NodeIDFromString("C")}
|
2018-08-23 16:20:11 +01:00
|
|
|
cases := []struct {
|
|
|
|
caseName string
|
|
|
|
rt dht.RoutingTable
|
|
|
|
getRTErr error
|
2018-09-18 05:39:06 +01:00
|
|
|
pingNode pb.Node
|
2018-08-23 16:20:11 +01:00
|
|
|
pingErr error
|
|
|
|
successErr error
|
|
|
|
failErr error
|
2018-09-18 05:39:06 +01:00
|
|
|
findNear []*pb.Node
|
2018-08-23 16:20:11 +01:00
|
|
|
limit int
|
|
|
|
nearErr error
|
2018-10-08 16:09:37 +01:00
|
|
|
res *pb.QueryResponse
|
2018-08-23 16:20:11 +01:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{caseName: "ping success, return sender",
|
|
|
|
rt: mockRT,
|
|
|
|
getRTErr: nil,
|
|
|
|
pingNode: *sender,
|
|
|
|
pingErr: nil,
|
|
|
|
successErr: nil,
|
|
|
|
failErr: nil,
|
2018-09-18 05:39:06 +01:00
|
|
|
findNear: []*pb.Node{target},
|
2018-08-23 16:20:11 +01:00
|
|
|
limit: 2,
|
|
|
|
nearErr: nil,
|
2018-10-08 16:09:37 +01:00
|
|
|
res: &pb.QueryResponse{Sender: sender, Response: []*pb.Node{target}},
|
2018-08-23 16:20:11 +01:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{caseName: "ping success, return nearest",
|
|
|
|
rt: mockRT,
|
|
|
|
getRTErr: nil,
|
|
|
|
pingNode: *sender,
|
|
|
|
pingErr: nil,
|
|
|
|
successErr: nil,
|
|
|
|
failErr: nil,
|
2018-09-18 05:39:06 +01:00
|
|
|
findNear: []*pb.Node{sender, node},
|
2018-08-23 16:20:11 +01:00
|
|
|
limit: 2,
|
|
|
|
nearErr: nil,
|
2018-10-08 16:09:37 +01:00
|
|
|
res: &pb.QueryResponse{Sender: sender, Response: []*pb.Node{sender, node}},
|
2018-08-23 16:20:11 +01:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, v := range cases {
|
2018-11-29 18:39:27 +00:00
|
|
|
req := pb.QueryRequest{Pingback: true, Sender: sender, Target: &pb.Node{Id: teststorj.NodeIDFromString("B")}, Limit: int64(2)}
|
2018-08-23 16:20:11 +01:00
|
|
|
mockDHT.EXPECT().GetRoutingTable(gomock.Any()).Return(v.rt, v.getRTErr)
|
|
|
|
mockDHT.EXPECT().Ping(gomock.Any(), gomock.Any()).Return(v.pingNode, v.pingErr)
|
|
|
|
if v.pingErr != nil {
|
|
|
|
mockRT.EXPECT().ConnectionFailed(gomock.Any()).Return(v.failErr)
|
|
|
|
} else {
|
|
|
|
mockRT.EXPECT().ConnectionSuccess(gomock.Any()).Return(v.successErr)
|
|
|
|
if v.successErr == nil {
|
|
|
|
mockRT.EXPECT().FindNear(gomock.Any(), v.limit).Return(v.findNear, v.nearErr)
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
res, err := s.Query(context.Background(), &req)
|
2018-08-23 16:20:11 +01:00
|
|
|
if !assert.Equal(t, v.res, res) {
|
|
|
|
fmt.Printf("case %s (%v) failed\n", v.caseName, i)
|
|
|
|
}
|
|
|
|
if v.err == nil && !assert.NoError(t, err) {
|
|
|
|
fmt.Printf("query errored at case %v\n", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|