-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
add unit test for common/deliverclient/blocksprovider/delivery_requester #4798
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
198 changes: 198 additions & 0 deletions
198
common/deliverclient/blocksprovider/delivery_requester_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package blocksprovider_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric-protos-go/common" | ||
"github.com/hyperledger/fabric/common/deliverclient/blocksprovider" | ||
"github.com/hyperledger/fabric/common/deliverclient/blocksprovider/fake" | ||
"github.com/hyperledger/fabric/common/deliverclient/orderers" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/connectivity" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
func TestDeliveryRequester_Connect_Success(t *testing.T) { | ||
fakeSigner := &fake.Signer{} | ||
fakeSigner.SignReturns([]byte("good-sig"), nil) | ||
|
||
fakeDialer := &fake.Dialer{} | ||
cc := &grpc.ClientConn{} | ||
fakeDialer.DialReturns(cc, nil) | ||
|
||
fakeDeliverClient := &fake.DeliverClient{} | ||
fakeDeliverClient.SendReturns(nil) | ||
|
||
fakeDeliverStreamer := &fake.DeliverStreamer{} | ||
fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) | ||
|
||
dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) | ||
assert.NotNil(t, dr) | ||
|
||
seekInfoEnv := &common.Envelope{} | ||
endpoint := &orderers.Endpoint{ | ||
Address: "orderer-address-1", | ||
RootCerts: [][]byte{[]byte("root-cert")}, | ||
Refreshed: make(chan struct{}), | ||
} | ||
|
||
deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, deliverClient) | ||
assert.NotNil(t, cancelFunc) | ||
} | ||
|
||
func TestDeliveryRequester_Connect_DialerError(t *testing.T) { | ||
fakeSigner := &fake.Signer{} | ||
fakeSigner.SignReturns([]byte("good-sig"), nil) | ||
|
||
fakeDialer := &fake.Dialer{} | ||
dialError := errors.New("dialer-error") | ||
fakeDialer.DialReturns(nil, dialError) | ||
|
||
fakeDeliverClient := &fake.DeliverClient{} | ||
fakeDeliverClient.SendReturns(nil) | ||
|
||
fakeDeliverStreamer := &fake.DeliverStreamer{} | ||
fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) | ||
|
||
dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) | ||
assert.NotNil(t, dr) | ||
|
||
seekInfoEnv := &common.Envelope{} | ||
endpoint := &orderers.Endpoint{ | ||
Address: "orderer-address-1", | ||
RootCerts: [][]byte{[]byte("root-cert")}, | ||
Refreshed: make(chan struct{}), | ||
} | ||
|
||
deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) | ||
assert.Error(t, err) | ||
assert.Nil(t, deliverClient) | ||
assert.Nil(t, cancelFunc) | ||
} | ||
|
||
func TestDeliveryRequester_Connect_DeliverStreamerError(t *testing.T) { | ||
fakeSigner := &fake.Signer{} | ||
fakeSigner.SignReturns([]byte("good-sig"), nil) | ||
|
||
fakeDialer := &fake.Dialer{} | ||
fakeDialer.DialStub = func(string, [][]byte) (*grpc.ClientConn, error) { | ||
cc, err := grpc.Dial("localhost:6005", grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
require.NoError(t, err) | ||
require.NotEqual(t, connectivity.Shutdown, cc.GetState()) | ||
|
||
return cc, nil | ||
} | ||
|
||
fakeDeliverClient := &fake.DeliverClient{} | ||
fakeDeliverClient.SendReturns(nil) | ||
|
||
fakeDeliverStreamer := &fake.DeliverStreamer{} | ||
deliverStreamerError := errors.New("deliver-streamer-error") | ||
fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, deliverStreamerError) | ||
|
||
dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) | ||
assert.NotNil(t, dr) | ||
|
||
seekInfoEnv := &common.Envelope{} | ||
endpoint := &orderers.Endpoint{ | ||
Address: "orderer-address-1", | ||
RootCerts: [][]byte{[]byte("root-cert")}, | ||
Refreshed: make(chan struct{}), | ||
} | ||
|
||
deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) | ||
assert.Error(t, err) | ||
assert.Nil(t, deliverClient) | ||
assert.Nil(t, cancelFunc) | ||
} | ||
|
||
func TestDeliveryRequester_Connect_DeliverClientError(t *testing.T) { | ||
fakeSigner := &fake.Signer{} | ||
fakeSigner.SignReturns([]byte("good-sig"), nil) | ||
|
||
fakeDialer := &fake.Dialer{} | ||
fakeDialer.DialStub = func(string, [][]byte) (*grpc.ClientConn, error) { | ||
cc, err := grpc.Dial("localhost:6005", grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
require.NoError(t, err) | ||
require.NotEqual(t, connectivity.Shutdown, cc.GetState()) | ||
|
||
return cc, nil | ||
} | ||
|
||
fakeDeliverClient := &fake.DeliverClient{} | ||
deliverClientError := errors.New("deliver-client-error") | ||
fakeDeliverClient.SendReturns(deliverClientError) | ||
|
||
fakeDeliverStreamer := &fake.DeliverStreamer{} | ||
fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) | ||
|
||
dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) | ||
assert.NotNil(t, dr) | ||
|
||
seekInfoEnv := &common.Envelope{} | ||
endpoint := &orderers.Endpoint{ | ||
Address: "orderer-address-1", | ||
RootCerts: [][]byte{[]byte("root-cert")}, | ||
Refreshed: make(chan struct{}), | ||
} | ||
|
||
deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) | ||
assert.Error(t, err) | ||
assert.Nil(t, deliverClient) | ||
assert.Nil(t, cancelFunc) | ||
} | ||
|
||
func TestDeliveryRequester_SeekInfoBlocksFrom(t *testing.T) { | ||
fakeSigner := &fake.Signer{} | ||
fakeSigner.SignReturns([]byte("good-sig"), nil) | ||
|
||
fakeDialer := &fake.Dialer{} | ||
cc := &grpc.ClientConn{} | ||
fakeDialer.DialReturns(cc, nil) | ||
|
||
fakeDeliverClient := &fake.DeliverClient{} | ||
fakeDeliverClient.SendReturns(nil) | ||
|
||
fakeDeliverStreamer := &fake.DeliverStreamer{} | ||
fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) | ||
|
||
dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) | ||
assert.NotNil(t, dr) | ||
|
||
envelope, err := dr.SeekInfoBlocksFrom(1000) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, envelope) | ||
} | ||
|
||
func TestDeliveryRequester_SeekInfoHeadersFrom(t *testing.T) { | ||
fakeSigner := &fake.Signer{} | ||
fakeSigner.SignReturns([]byte("good-sig"), nil) | ||
|
||
fakeDialer := &fake.Dialer{} | ||
cc := &grpc.ClientConn{} | ||
fakeDialer.DialReturns(cc, nil) | ||
|
||
fakeDeliverClient := &fake.DeliverClient{} | ||
fakeDeliverClient.SendReturns(nil) | ||
|
||
fakeDeliverStreamer := &fake.DeliverStreamer{} | ||
fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) | ||
|
||
dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) | ||
assert.NotNil(t, dr) | ||
|
||
envelope, err := dr.SeekInfoHeadersFrom(1000) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, envelope) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@solo-daemon
First of all thanks for this contribution!
This style of "fake mocks" based testing is good. The deliverer and bft_deliverer are tested this way. I will accept all the tests written here.
However, the issue calls for a more explicit style of testing - against a real gRPC service (a mockup for testing, not a real orderer).
See for example how it's done here:
orderer/common/cluster/deliver_test.go
where there is adeliverServer
that implements the "Deliver" API. You can copy the deliverServer from there, and run the DeliveryRequester against it.