MCPcopy Create free account
hub / github.com/CPChain/chain / TestSubscriptionMultipleNamespaces

Function TestSubscriptionMultipleNamespaces

api/rpc/subscription_test.go:228–330  ·  view source on GitHub ↗

TestSubscriptionMultipleNamespaces ensures that subscriptions can exists for multiple different namespaces.

(t *testing.T)

Source from the content-addressed store, hash-verified

226// TestSubscriptionMultipleNamespaces ensures that subscriptions can exists
227// for multiple different namespaces.
228func TestSubscriptionMultipleNamespaces(t *testing.T) {
229 var (
230 namespaces = []string{"eth", "shh", "bzz"}
231 server = NewServer()
232 service = NotificationTestService{}
233 clientConn, serverConn = net.Pipe()
234
235 out = json.NewEncoder(clientConn)
236 in = json.NewDecoder(clientConn)
237 successes = make(chan jsonSuccessResponse)
238 failures = make(chan jsonErrResponse)
239 notifications = make(chan jsonNotification)
240
241 errors = make(chan error, 10)
242 )
243
244 // setup and start server
245 for _, namespace := range namespaces {
246 if err := server.RegisterName(namespace, &service); err != nil {
247 t.Fatalf("unable to register test service %v", err)
248 }
249 }
250
251 go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation|OptionSubscriptions)
252 defer server.Stop()
253
254 // wait for message and write them to the given channels
255 go waitForMessages(t, in, successes, failures, notifications, errors)
256
257 // create subscriptions one by one
258 n := 3
259 for i, namespace := range namespaces {
260 request := map[string]interface{}{
261 "id": i,
262 "method": fmt.Sprintf("%s_subscribe", namespace),
263 "version": "2.0",
264 "params": []interface{}{"someSubscription", n, i},
265 }
266
267 if err := out.Encode(&request); err != nil {
268 t.Fatalf("Could not create subscription: %v", err)
269 }
270 }
271
272 // create all subscriptions in 1 batch
273 var requests []interface{}
274 for i, namespace := range namespaces {
275 requests = append(requests, map[string]interface{}{
276 "id": i,
277 "method": fmt.Sprintf("%s_subscribe", namespace),
278 "version": "2.0",
279 "params": []interface{}{"someSubscription", n, i},
280 })
281 }
282
283 if err := out.Encode(&requests); err != nil {
284 t.Fatalf("Could not create subscription in batch form: %v", err)
285 }

Callers

nothing calls this directly

Calls 7

NewServerFunction · 0.85
NewJSONCodecFunction · 0.85
waitForMessagesFunction · 0.85
RegisterNameMethod · 0.80
ServeCodecMethod · 0.80
StopMethod · 0.65
FatalMethod · 0.65

Tested by

no test coverage detected