forked from Trial97/rpc-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_context_test.go
50 lines (43 loc) · 1000 Bytes
/
rpc_context_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
package main
import (
"context"
"log"
"net"
"rpc-bench/airc"
"testing"
"github.com/keegancsmith/rpc"
)
func startRPCServerWithContext() {
rpc.Register(new(airc.Arith))
var l net.Listener
l, serverAddr = listenTCP()
log.Println("Test RPC server listening on", serverAddr)
go rpc.Accept(l)
}
func BenchmarkRPCCallWithContext(b *testing.B) {
startRPCServerWithContext()
client, err := rpc.Dial("tcp", serverAddr)
if err != nil {
b.Fatal("dialing", err)
}
defer client.Close()
// Synchronous calls
ctx := context.Background()
// ctx2, cancel := context.WithCancel(ctx)
args := &airc.Args{7, 8}
reply := new(airc.Reply)
// go func() {
// time.Sleep(time.Second)
// cancel()
// }()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = client.Call(ctx, "Arith.Add", args, reply)
if err != nil {
b.Errorf("Add: expected no error but got string %q", err.Error())
}
if reply.C != args.A+args.B {
b.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
}
}
}