Skip to content
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

check the error is returned correctly #723

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions integration/fsc/pingpong/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ SPDX-License-Identifier: Apache-2.0

package pingpong

import "github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
import (
"encoding/json"

"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
)

// InitiatorViewFactory is the factory of Initiator views
type InitiatorViewFactory struct{}

// NewView returns a new instance of the Initiator view
func (i *InitiatorViewFactory) NewView(in []byte) (view.View, error) {
return &Initiator{}, nil
m := Message{}
if len(in) != 0 {
if err := json.Unmarshal(in, &m); err != nil {
return nil, err
}
}
return &Initiator{Message: m}, nil
}
14 changes: 12 additions & 2 deletions integration/fsc/pingpong/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import (
"github.com/pkg/errors"
)

type Initiator struct{}
type Message struct {
Payload []byte
}

type Initiator struct {
Message
}

func (p *Initiator) Call(context view.Context) (interface{}, error) {
// Retrieve responder identity
Expand All @@ -25,7 +31,11 @@ func (p *Initiator) Call(context view.Context) (interface{}, error) {
session, err := context.GetSession(context.Initiator(), responder)
assert.NoError(err) // Send a ping

err = session.Send([]byte("ping"))
if len(p.Payload) == 0 {
p.Payload = []byte("ping")
}

err = session.Send(p.Payload)
assert.NoError(err) // Wait for the pong
ch := session.Receive()
select {
Expand Down
20 changes: 20 additions & 0 deletions integration/fsc/pingpong/pingpong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ var _ = Describe("EndToEnd", func() {
AfterEach(s.TearDown)
It("generate artifacts & successful mock pingpong", s.TestGenerateAndMockPingPong)
})

Describe("Network-based PongWithErr With Websockets", func() {
s := NewTestSuite(fsc.WebSocket, true, integration.NoReplication)
BeforeEach(s.Setup)
AfterEach(s.TearDown)
It("successed", func() {
s.TestGenerateAndPongErr("pongWithSendError", "initiator")
s.TestGenerateAndPongErr("pongWithError", "initiator")
s.TestGenerateAndPongErr("pongWithPanic", "initiator")
})
})
})

func newNode(conf string) api.FabricSmartClientNode {
Expand Down Expand Up @@ -313,6 +324,15 @@ func (s *TestSuite) TestGenerateAndMockPingPong() {
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}

func (s *TestSuite) TestGenerateAndPongErr(payload string, clients ...string) {
// Initiate a view and check the output
for _, clientName := range clients {
_, err := s.II.Client(clientName).CallView("init", common.JSONMarshall(&pingpong.Message{Payload: []byte(payload)}))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("expected ping, got %s", payload)))
}
}

func newWebClient(confDir string) *web.Client {
c, err := client.NewWebClientConfigFromFSC(confDir)
Expect(err).NotTo(HaveOccurred())
Expand Down
19 changes: 15 additions & 4 deletions integration/fsc/pingpong/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ func (p *Responder) Call(context view.Context) (interface{}, error) {
m := string(payload)
switch {
case m != "ping":
// reply with an error
err := session.SendError([]byte(fmt.Sprintf("expected ping, got %s", m)))
assert.NoError(err)
return nil, errors.Errorf("expected ping, got %s", m)
// reply with an error,
// alternatively, you can just return with an error or panic.
// The executor of the view will take care of sending the error back
switch m {
case "pongWithSendError":
err := session.SendError([]byte(fmt.Sprintf("expected ping, got %s", m)))
assert.NoError(err)
return nil, nil
case "pongWithError":
return nil, errors.Errorf("expected ping, got %s", m)
case "pongWithPanic":
panic(fmt.Sprintf("expected ping, got %s", m))
default:
panic(fmt.Sprintf("unexpected message: %s", m))
}
default:
// reply with pong
err := session.Send([]byte("pong"))
Expand Down
Loading