Skip to content

Unit Test P2p handler RequestFileDownload #866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2020
Merged
Changes from all 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
86 changes: 86 additions & 0 deletions p2p/handler/p2pServerHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,89 @@ func TestP2PServerHandler_RequestBlockTransactions(t *testing.T) {
})
}
}

type (
mockRequestDownloadFileError struct {
service2.P2PServerServiceInterface
}
mockRequestDownloadFileSuccess struct {
service2.P2PServerServiceInterface
}
)

func (*mockRequestDownloadFileError) RequestDownloadFile(ctx context.Context, fileChunkNames []string) (*model.FileDownloadResponse, error) {
return nil, errors.New("Error RequestDownloadFile")
}

func (*mockRequestDownloadFileSuccess) RequestDownloadFile(ctx context.Context, fileChunkNames []string) (*model.FileDownloadResponse, error) {
return &model.FileDownloadResponse{}, nil
}

func TestP2PServerHandler_RequestFileDownload(t *testing.T) {
type fields struct {
Service service2.P2PServerServiceInterface
}
type args struct {
ctx context.Context
req *model.FileDownloadRequest
}
tests := []struct {
name string
fields fields
args args
want *model.FileDownloadResponse
wantErr bool
}{
{
name: "RequestFileDownload:NotContainAnyFileName",
args: args{
req: &model.FileDownloadRequest{
FileChunkNames: []string{},
},
},
want: nil,
wantErr: true,
},
{
name: "RequestFileDownload:Error",
args: args{
req: &model.FileDownloadRequest{
FileChunkNames: []string{"mockName"},
},
},
fields: fields{
Service: &mockRequestDownloadFileError{},
},
want: nil,
wantErr: true,
},
{
name: "RequestFileDownload:Success",
args: args{
req: &model.FileDownloadRequest{
FileChunkNames: []string{"mockName"},
},
},
fields: fields{
Service: &mockRequestDownloadFileSuccess{},
},
want: &model.FileDownloadResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ss := &P2PServerHandler{
Service: tt.fields.Service,
}
got, err := ss.RequestFileDownload(tt.args.ctx, tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("P2PServerHandler.RequestFileDownload() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("P2PServerHandler.RequestFileDownload() = %v, want %v", got, tt.want)
}
})
}
}