|
17 | 17 | package rpc
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "bytes" |
| 21 | + "context" |
20 | 22 | "encoding/json"
|
21 | 23 | "fmt"
|
| 24 | + "io" |
| 25 | + "math/big" |
22 | 26 | "net"
|
23 | 27 | "strings"
|
24 | 28 | "testing"
|
25 | 29 | "time"
|
| 30 | + |
| 31 | + "github.com/XinFinOrg/XDPoSChain/common" |
| 32 | + "github.com/XinFinOrg/XDPoSChain/core/types" |
26 | 33 | )
|
27 | 34 |
|
28 | 35 | func TestNewID(t *testing.T) {
|
@@ -220,3 +227,56 @@ func readAndValidateMessage(in *json.Decoder) (*subConfirmation, *subscriptionRe
|
220 | 227 | return nil, nil, fmt.Errorf("unrecognized message: %v", msg)
|
221 | 228 | }
|
222 | 229 | }
|
| 230 | + |
| 231 | +type mockConn struct { |
| 232 | + enc *json.Encoder |
| 233 | +} |
| 234 | + |
| 235 | +// writeJSON writes a message to the connection. |
| 236 | +func (c *mockConn) writeJSON(ctx context.Context, msg interface{}, isError bool) error { |
| 237 | + return c.enc.Encode(msg) |
| 238 | +} |
| 239 | + |
| 240 | +// Closed returns a channel which is closed when the connection is closed. |
| 241 | +func (c *mockConn) closed() <-chan interface{} { return nil } |
| 242 | + |
| 243 | +// RemoteAddr returns the peer address of the connection. |
| 244 | +func (c *mockConn) remoteAddr() string { return "" } |
| 245 | + |
| 246 | +// BenchmarkNotify benchmarks the performance of notifying a subscription. |
| 247 | +func BenchmarkNotify(b *testing.B) { |
| 248 | + id := ID("test") |
| 249 | + notifier := &Notifier{ |
| 250 | + h: &handler{conn: &mockConn{json.NewEncoder(io.Discard)}}, |
| 251 | + sub: &Subscription{ID: id}, |
| 252 | + activated: true, |
| 253 | + } |
| 254 | + msg := &types.Header{ |
| 255 | + ParentHash: common.HexToHash("0x01"), |
| 256 | + Number: big.NewInt(100), |
| 257 | + } |
| 258 | + b.ResetTimer() |
| 259 | + for i := 0; i < b.N; i++ { |
| 260 | + notifier.Notify(id, msg) |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +func TestNotify(t *testing.T) { |
| 265 | + out := new(bytes.Buffer) |
| 266 | + id := ID("test") |
| 267 | + notifier := &Notifier{ |
| 268 | + h: &handler{conn: &mockConn{json.NewEncoder(out)}}, |
| 269 | + sub: &Subscription{ID: id}, |
| 270 | + activated: true, |
| 271 | + } |
| 272 | + msg := &types.Header{ |
| 273 | + ParentHash: common.HexToHash("0x01"), |
| 274 | + Number: big.NewInt(100), |
| 275 | + } |
| 276 | + notifier.Notify(id, msg) |
| 277 | + have := strings.TrimSpace(out.String()) |
| 278 | + want := `{"jsonrpc":"2.0","method":"_subscription","params":{"subscription":"test","result":{"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000001","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":null,"number":"0x64","gasLimit":"0x0","gasUsed":"0x0","timestamp":null,"extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","validators":null,"validator":null,"penalties":null,"baseFeePerGas":null,"hash":"0x40f9f4f62eba4f75e54e5168d0fe28302f06f2780cfaf45356a52131f2addf77"}}}` |
| 279 | + if have != want { |
| 280 | + t.Errorf("have:\n%v\nwant:\n%v\n", have, want) |
| 281 | + } |
| 282 | +} |
0 commit comments