Skip to content

Commit

Permalink
[FAB-9651] Closed channel should return error
Browse files Browse the repository at this point in the history
Change-Id: I6391cfe2b5f45be54e4bd1b408c5eb77b3807f02
Signed-off-by: Saad Karim <[email protected]>
  • Loading branch information
Saad Karim committed Apr 24, 2018
1 parent db0c395 commit 5f37d12
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
go func() {
var in2 *pb.ChaincodeMessage
in2, err = stream.Recv()
errc <- err
msgAvail <- in2
}()
}
Expand All @@ -303,7 +304,7 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
continue
}
//no, bail
err = errors.Wrap(sendErr, fmt.Sprintf("error sending %s", in.Type.String()))
err = errors.Wrap(sendErr, "error sending")
return
case in = <-msgAvail:
if err == io.EOF {
Expand Down
6 changes: 5 additions & 1 deletion core/chaincode/shim/inprocstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

pb "github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
)

//SendPanicFailure
Expand Down Expand Up @@ -55,7 +56,10 @@ func (s *inProcStream) Send(msg *pb.ChaincodeMessage) (err error) {
}

func (s *inProcStream) Recv() (*pb.ChaincodeMessage, error) {
msg := <-s.recv
msg, ok := <-s.recv
if !ok {
return nil, errors.New("channel is closed")
}
return msg, nil
}

Expand Down
29 changes: 29 additions & 0 deletions core/chaincode/shim/inprocstream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package shim

import (
"testing"

pb "github.com/hyperledger/fabric/protos/peer"
"github.com/stretchr/testify/assert"
)

func TestRecvChannelClosedError(t *testing.T) {
ch := make(chan *pb.ChaincodeMessage)

stream := newInProcStream(ch, ch)

// Close the channel
close(ch)

// Trying to call a closed receive channel should return an error
_, err := stream.Recv()
if assert.Error(t, err, "Should return an error") {
assert.Contains(t, err.Error(), "channel is closed")
}
}
6 changes: 5 additions & 1 deletion core/container/inproccontroller/inprocstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package inproccontroller

import (
"errors"
"fmt"

pb "github.com/hyperledger/fabric/protos/peer"
Expand Down Expand Up @@ -53,6 +54,9 @@ func (s *inProcStream) Send(msg *pb.ChaincodeMessage) (err error) {
}

func (s *inProcStream) Recv() (*pb.ChaincodeMessage, error) {
msg := <-s.recv
msg, ok := <-s.recv
if !ok {
return nil, errors.New("channel is closed")
}
return msg, nil
}
15 changes: 15 additions & 0 deletions core/container/inproccontroller/inprocstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ func TestSend(t *testing.T) {
err := stream.Send(msg)
assert.NotNil(t, err, "should have errored on panic")
}

func TestRecvChannelClosedError(t *testing.T) {
ch := make(chan *pb.ChaincodeMessage)

stream := newInProcStream(ch, ch)

// Close the channel
close(ch)

// Trying to call a closed receive channel should return an error
_, err := stream.Recv()
if assert.Error(t, err, "Should return an error") {
assert.Contains(t, err.Error(), "channel is closed")
}
}

0 comments on commit 5f37d12

Please sign in to comment.