Skip to content

Commit ef59484

Browse files
committed
check the error is returned correctly
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent d16c293 commit ef59484

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

integration/fsc/pingpong/factory.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ SPDX-License-Identifier: Apache-2.0
66

77
package pingpong
88

9-
import "github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
9+
import (
10+
"encoding/json"
11+
12+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
13+
)
1014

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

1418
// NewView returns a new instance of the Initiator view
1519
func (i *InitiatorViewFactory) NewView(in []byte) (view.View, error) {
16-
return &Initiator{}, nil
20+
m := Message{}
21+
if len(in) != 0 {
22+
if err := json.Unmarshal(in, &m); err != nil {
23+
return nil, err
24+
}
25+
}
26+
return &Initiator{Message: m}, nil
1727
}

integration/fsc/pingpong/initiator.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import (
1515
"github.com/pkg/errors"
1616
)
1717

18-
type Initiator struct{}
18+
type Message struct {
19+
Payload []byte
20+
}
21+
22+
type Initiator struct {
23+
Message
24+
}
1925

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

28-
err = session.Send([]byte("ping"))
34+
if len(p.Payload) == 0 {
35+
p.Payload = []byte("ping")
36+
}
37+
38+
err = session.Send(p.Payload)
2939
assert.NoError(err) // Wait for the pong
3040
ch := session.Receive()
3141
select {

integration/fsc/pingpong/pingpong_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ var _ = Describe("EndToEnd", func() {
189189
AfterEach(s.TearDown)
190190
It("generate artifacts & successful mock pingpong", s.TestGenerateAndMockPingPong)
191191
})
192+
193+
Describe("Network-based PongWithErr With Websockets", func() {
194+
s := NewTestSuite(fsc.WebSocket, true, integration.NoReplication)
195+
BeforeEach(s.Setup)
196+
AfterEach(s.TearDown)
197+
It("successed", func() {
198+
s.TestGenerateAndPongErr("pongWithSendError", "initiator")
199+
s.TestGenerateAndPongErr("pongWithError", "initiator")
200+
s.TestGenerateAndPongErr("pongWithPanic", "initiator")
201+
})
202+
})
192203
})
193204

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

327+
func (s *TestSuite) TestGenerateAndPongErr(payload string, clients ...string) {
328+
// Initiate a view and check the output
329+
for _, clientName := range clients {
330+
_, err := s.II.Client(clientName).CallView("init", common.JSONMarshall(&pingpong.Message{Payload: []byte(payload)}))
331+
Expect(err).To(HaveOccurred())
332+
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("expected ping, got %s", payload)))
333+
}
334+
}
335+
316336
func newWebClient(confDir string) *web.Client {
317337
c, err := client.NewWebClientConfigFromFSC(confDir)
318338
Expect(err).NotTo(HaveOccurred())

integration/fsc/pingpong/responder.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,21 @@ func (p *Responder) Call(context view.Context) (interface{}, error) {
3434
m := string(payload)
3535
switch {
3636
case m != "ping":
37-
// reply with an error
38-
err := session.SendError([]byte(fmt.Sprintf("expected ping, got %s", m)))
39-
assert.NoError(err)
40-
return nil, errors.Errorf("expected ping, got %s", m)
37+
// reply with an error,
38+
// alternatively, you can just return with an error or panic.
39+
// The executor of the view will take care of sending the error back
40+
switch m {
41+
case "pongWithSendError":
42+
err := session.SendError([]byte(fmt.Sprintf("expected ping, got %s", m)))
43+
assert.NoError(err)
44+
return nil, nil
45+
case "pongWithError":
46+
return nil, errors.Errorf("expected ping, got %s", m)
47+
case "pongWithPanic":
48+
panic(fmt.Sprintf("expected ping, got %s", m))
49+
default:
50+
panic(fmt.Sprintf("unexpected message: %s", m))
51+
}
4152
default:
4253
// reply with pong
4354
err := session.Send([]byte("pong"))

0 commit comments

Comments
 (0)