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

Function TestNotifications

api/rpc/subscription_test.go:108–165  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

106}
107
108func TestNotifications(t *testing.T) {
109 server := NewServer()
110 service := &NotificationTestService{}
111
112 if err := server.RegisterName("eth", service); err != nil {
113 t.Fatalf("unable to register test service %v", err)
114 }
115
116 clientConn, serverConn := net.Pipe()
117
118 go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation|OptionSubscriptions)
119
120 out := json.NewEncoder(clientConn)
121 in := json.NewDecoder(clientConn)
122
123 n := 5
124 val := 12345
125 request := map[string]interface{}{
126 "id": 1,
127 "method": "eth_subscribe",
128 "version": "2.0",
129 "params": []interface{}{"someSubscription", n, val},
130 }
131
132 // create subscription
133 if err := out.Encode(request); err != nil {
134 t.Fatal(err)
135 }
136
137 var subid string
138 response := jsonSuccessResponse{Result: subid}
139 if err := in.Decode(&response); err != nil {
140 t.Fatal(err)
141 }
142
143 var ok bool
144 if _, ok = response.Result.(string); !ok {
145 t.Fatalf("expected subscription id, got %T", response.Result)
146 }
147
148 for i := 0; i < n; i++ {
149 var notification jsonNotification
150 if err := in.Decode(&notification); err != nil {
151 t.Fatalf("%v", err)
152 }
153
154 if int(notification.Params.Result.(float64)) != val+i {
155 t.Fatalf("expected %d, got %d", val+i, notification.Params.Result)
156 }
157 }
158
159 clientConn.Close() // causes notification unsubscribe callback to be called
160 time.Sleep(1 * time.Second)
161
162 if !service.wasUnsubCallbackCalled() {
163 t.Error("unsubscribe callback not called after closing connection")
164 }
165}

Callers

nothing calls this directly

Calls 9

RegisterNameMethod · 0.95
ServeCodecMethod · 0.95
NewServerFunction · 0.85
NewJSONCodecFunction · 0.85
SleepMethod · 0.80
FatalMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected