-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathpingpong_test.go
330 lines (283 loc) · 12.2 KB
/
pingpong_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pingpong_test
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/hyperledger-labs/fabric-smart-client/integration"
"github.com/hyperledger-labs/fabric-smart-client/integration/fsc/pingpong"
"github.com/hyperledger-labs/fabric-smart-client/integration/fsc/pingpong/mock"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/client"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc"
"github.com/hyperledger-labs/fabric-smart-client/pkg/api"
"github.com/hyperledger-labs/fabric-smart-client/pkg/node"
viewsdk "github.com/hyperledger-labs/fabric-smart-client/platform/view/sdk/dig"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/client/web"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/atomic"
)
var _ = Describe("EndToEnd", func() {
Describe("Node-based Ping pong", func() {
var (
initiator api.FabricSmartClientNode
responder api.FabricSmartClientNode
)
AfterEach(func() {
// Stop the ii
initiator.Stop()
if responder != nil {
responder.Stop()
}
time.Sleep(5 * time.Second)
})
It("successful pingpong based on REST API", func() {
// Init and Start fsc nodes
initiator = newNode("./testdata/fsc/nodes/initiator.0")
responder = newNode("./testdata/fsc/nodes/responder.0")
err := initiator.Start()
Expect(err).NotTo(HaveOccurred())
err = responder.Start()
Expect(err).NotTo(HaveOccurred())
// Register views and view factories
err = initiator.RegisterFactory("init", &pingpong.InitiatorViewFactory{})
Expect(err).NotTo(HaveOccurred())
Expect(responder.RegisterResponder(&pingpong.Responder{}, &pingpong.Initiator{})).NotTo(HaveOccurred())
time.Sleep(3 * time.Second)
webClientConfig, err := client.NewWebClientConfigFromFSC("./testdata/fsc/nodes/initiator.0")
Expect(err).NotTo(HaveOccurred())
initiatorWebClient, err := web.NewClient(webClientConfig)
Expect(err).NotTo(HaveOccurred())
res, err := initiatorWebClient.CallView("init", bytes.NewBuffer([]byte("hi")).Bytes())
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
version, err := initiatorWebClient.ServerVersion()
Expect(err).NotTo(HaveOccurred())
Expect(version).To(BeEquivalentTo("{\"CommitSHA\":\"development build\",\"Version\":\"latest\"}"))
webClientConfig.TLSCertPath = ""
initiatorWebClient, err = web.NewClient(webClientConfig)
Expect(err).NotTo(HaveOccurred())
_, err = initiatorWebClient.CallView("init", bytes.NewBuffer([]byte("hi")).Bytes())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("status code [401], status [401 Unauthorized]"))
version, err = initiatorWebClient.ServerVersion()
Expect(err).NotTo(HaveOccurred())
Expect(version).To(BeEquivalentTo("{\"CommitSHA\":\"development build\",\"Version\":\"latest\"}"))
})
It("successful pingpong based on WebSocket", func() {
// Init and Start fsc nodes
initiator = newNode("./testdata/fsc/nodes/initiator.0")
Expect(initiator).NotTo(BeNil())
err := initiator.Start()
Expect(err).NotTo(HaveOccurred())
// Register views and view factories
err = initiator.RegisterFactory("stream", &pingpong.StreamerViewFactory{})
Expect(err).NotTo(HaveOccurred())
time.Sleep(3 * time.Second)
initiatorWebClient := newWebClient("./testdata/fsc/nodes/initiator.0")
stream, err := initiatorWebClient.StreamCallView("stream", nil)
Expect(err).NotTo(HaveOccurred())
var s string
Expect(stream.Recv(&s)).NotTo(HaveOccurred())
Expect(s).To(BeEquivalentTo("hello"))
Expect(stream.Send("ciao")).NotTo(HaveOccurred())
res, err := stream.Result()
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
})
It("successful pingpong", func() {
// Init and Start fsc nodes
initiator = newNode("./testdata/fsc/nodes/initiator.0")
Expect(initiator).NotTo(BeNil())
responder = newNode("./testdata/fsc/nodes/responder.0")
Expect(responder).NotTo(BeNil())
err := initiator.Start()
Expect(err).NotTo(HaveOccurred())
err = responder.Start()
Expect(err).NotTo(HaveOccurred())
// Register views and view factories
err = initiator.RegisterFactory("init", &pingpong.InitiatorViewFactory{})
Expect(err).NotTo(HaveOccurred())
Expect(responder.RegisterResponder(&pingpong.Responder{}, &pingpong.Initiator{})).NotTo(HaveOccurred())
time.Sleep(3 * time.Second)
// Initiate a view and check the output
res, err := initiator.CallView("init", nil)
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
})
})
Describe("Network-based Ping pong With LibP2P", func() {
s := NewTestSuite(fsc.LibP2P, false, integration.NoReplication)
BeforeEach(s.Setup)
AfterEach(s.TearDown)
It("generate artifacts & successful pingpong", func() { s.TestGenerateAndPingPong("initiator") })
It("load artifact & successful pingpong", func() { s.TestLoadAndPingPong("initiator") })
It("load artifact & successful pingpong with stream", func() { s.TestLoadAndPingPongStream("initiator") })
It("load artifact & successful stream", func() { s.TestLoadAndStream("initiator") })
It("load artifact & successful stream with websocket", func() { s.TestLoadAndStreamWebsocket("initiator") })
It("load artifact & init clients & successful pingpong", s.TestLoadInitPingPong)
})
Describe("Network-based Ping pong With Websockets", func() {
s := NewTestSuite(fsc.WebSocket, false, integration.NoReplication)
BeforeEach(s.Setup)
AfterEach(s.TearDown)
It("generate artifacts & successful pingpong", func() { s.TestGenerateAndPingPong("initiator") })
It("load artifact & successful pingpong", func() { s.TestLoadAndPingPong("initiator") })
It("load artifact & successful pingpong with stream", func() { s.TestLoadAndPingPongStream("initiator") })
It("load artifact & successful stream", func() { s.TestLoadAndStream("initiator") })
It("load artifact & successful stream with websocket", func() { s.TestLoadAndStreamWebsocket("initiator") })
It("load artifact & init clients & successful pingpong", s.TestLoadInitPingPong)
})
Describe("Network-based Ping pong With Websockets and replication", func() {
s := NewTestSuite(fsc.WebSocket, true, &integration.ReplicationOptions{
ReplicationFactors: map[string]int{
"initiator": 3,
},
})
initiatorReplicas := GetFSCReplicaNames("initiator", 3)
BeforeEach(s.Setup)
AfterEach(s.TearDown)
It("generate artifacts & successful pingpong", func() { s.TestGenerateAndPingPong(initiatorReplicas...) })
It("load artifact & successful pingpong", func() { s.TestLoadAndPingPong(initiatorReplicas...) })
It("load artifact & successful pingpong with stream", func() { s.TestLoadAndPingPongStream(initiatorReplicas...) })
It("load artifact & successful stream", func() { s.TestLoadAndStream(initiatorReplicas...) })
It("load artifact & successful stream with websocket", func() { s.TestLoadAndStreamWebsocket(initiatorReplicas...) })
})
Describe("Network-based Mock Ping pong With LibP2P", func() {
s := NewTestSuite(fsc.LibP2P, false, integration.NoReplication)
BeforeEach(s.Setup)
AfterEach(s.TearDown)
It("generate artifacts & successful mock pingpong", s.TestGenerateAndMockPingPong)
})
Describe("Network-based Mock Ping pong With Websockets", func() {
s := NewTestSuite(fsc.WebSocket, false, integration.NoReplication)
BeforeEach(s.Setup)
AfterEach(s.TearDown)
It("generate artifacts & successful mock pingpong", s.TestGenerateAndMockPingPong)
})
})
func newNode(conf string) api.FabricSmartClientNode {
n := node.NewEmpty(conf)
Expect(n).NotTo(BeNil())
n.AddSDK(viewsdk.NewSDK(n))
return n
}
const testdataDir = "./testdata"
type TestSuite struct {
*integration.TestSuite
commType fsc.P2PCommunicationType
nodeOpts *integration.ReplicationOptions
}
func NewTestSuite(commType fsc.P2PCommunicationType, alwaysGenerate bool, nodeOpts *integration.ReplicationOptions) *TestSuite {
init := atomic.NewBool(false)
return &TestSuite{
TestSuite: integration.NewTestSuite(func() (ii *integration.Infrastructure, err error) {
topologies := pingpong.Topology(commType, nodeOpts)
if alwaysGenerate || init.CompareAndSwap(false, true) {
ii, err = integration.Generate(StartPortWithGeneration(), true, topologies...)
} else {
ii, err = integration.Load(0, testdataDir, true, topologies...)
}
ii.DeleteOnStop = false
return
}),
commType: commType,
nodeOpts: nodeOpts,
}
}
func (s *TestSuite) TestGenerateAndPingPong(clients ...string) {
// Initiate a view and check the output
for _, clientName := range clients {
res, err := s.II.Client(clientName).CallView("init", nil)
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}
}
func (s *TestSuite) TestLoadAndPingPong(clients ...string) {
// Initiate a view and check the output
for _, clientName := range clients {
res, err := s.II.Client(clientName).CallView("init", nil)
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}
}
func (s *TestSuite) TestLoadAndPingPongStream(clients ...string) {
// Initiate a view and check the output
for _, clientName := range clients {
channel, err := s.II.Client(clientName).StreamCallView("init", nil)
Expect(err).NotTo(HaveOccurred())
res, err := channel.Result()
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}
}
func (s *TestSuite) TestLoadAndStream(clients ...string) {
for _, clientName := range clients {
channel, err := s.II.Client(clientName).StreamCallView("stream", nil)
Expect(err).NotTo(HaveOccurred())
var str string
Expect(channel.Recv(&str)).NotTo(HaveOccurred())
Expect(str).To(BeEquivalentTo("hello"))
Expect(channel.Send("ciao")).NotTo(HaveOccurred())
res, err := channel.Result()
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}
}
func (s *TestSuite) TestLoadAndStreamWebsocket(clients ...string) {
time.Sleep(7 * time.Second)
for _, clientName := range clients {
// Get a client for the fsc node labelled initiator
initiator := s.II.WebClient(clientName)
// Initiate a view and check the output
channel, err := initiator.StreamCallView("stream", nil)
Expect(err).NotTo(HaveOccurred())
var str string
Expect(channel.Recv(&str)).NotTo(HaveOccurred())
Expect(str).To(BeEquivalentTo("hello"))
Expect(channel.Send("ciao")).NotTo(HaveOccurred())
res, err := channel.Result()
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}
}
func (s *TestSuite) TestLoadInitPingPong() {
// Use another ii to create clients
iiClients, err := integration.Clients(testdataDir, pingpong.Topology(s.commType, s.nodeOpts)...)
Expect(err).NotTo(HaveOccurred())
// Get a client for the fsc node labelled initiator
initiator := iiClients.Client("initiator")
// Initiate a view and check the output
res, err := initiator.CallView("init", nil)
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}
func (s *TestSuite) TestGenerateAndMockPingPong() {
// Init with mock=false, a failure must happen
_, err := s.II.Client("initiator").CallView("mockInit", common.JSONMarshall(&mock.Params{Mock: false}))
Expect(err).To(HaveOccurred())
Expect(strings.Contains(err.Error(), "expected mock pong, got pong")).To(BeTrue())
// Init with mock=true, a success must happen
res, err := s.II.Client("initiator").CallView("mockInit", common.JSONMarshall(&mock.Params{Mock: true}))
Expect(err).NotTo(HaveOccurred())
Expect(common.JSONUnmarshalString(res)).To(BeEquivalentTo("OK"))
}
func newWebClient(confDir string) *web.Client {
c, err := client.NewWebClientConfigFromFSC(confDir)
Expect(err).NotTo(HaveOccurred())
initiator, err := web.NewClient(c)
Expect(err).NotTo(HaveOccurred())
return initiator
}
func GetFSCReplicaNames(nodeName string, replicationFactor int) []string {
result := make([]string, replicationFactor)
for i := 0; i < replicationFactor; i++ {
result[i] = fmt.Sprintf("fsc.%s.%d", nodeName, i)
}
return result
}