func TestCancel(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() log := testLogger{t} p, q := pipetransport.New() if *logMessages { p = logtransport.New(nil, p) } c := rpc.NewConn(p, rpc.ConnLog(log)) notify := make(chan struct{}) hanger := testcapnp.Hanger_ServerToClient(Hanger{notify: notify}) d := rpc.NewConn(q, rpc.MainInterface(hanger.Client), rpc.ConnLog(log)) defer d.Wait() defer c.Close() client := testcapnp.Hanger{Client: c.Bootstrap(ctx)} subctx, subcancel := context.WithCancel(ctx) promise := client.Hang(subctx, nil) <-notify subcancel() _, err := promise.Struct() <-notify // test will deadlock if cancel not delivered if err != context.Canceled { t.Errorf("promise.Get() error: %v; want %v", err, context.Canceled) } }
func TestIssue3(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() p, q := pipetransport.New() if *logMessages { p = logtransport.New(nil, p) } log := testLogger{t} c := rpc.NewConn(p, rpc.ConnLog(log)) echoSrv := testcapnp.Echoer_ServerToClient(new(SideEffectEchoer)) d := rpc.NewConn(q, rpc.MainInterface(echoSrv.Client), rpc.ConnLog(log)) defer d.Wait() defer c.Close() client := testcapnp.Echoer{Client: c.Bootstrap(ctx)} localCap := testcapnp.CallOrder_ServerToClient(new(CallOrder)) echo := client.Echo(ctx, func(p testcapnp.Echoer_echo_Params) error { return p.SetCap(localCap) }) // This should not deadlock. _, err := echo.Struct() if err != nil { t.Error("Echo error:", err) } }
func BenchmarkPingPong(b *testing.B) { p, q := pipetransport.New() if *logMessages { p = logtransport.New(nil, p) } log := testLogger{b} c := rpc.NewConn(p, rpc.ConnLog(log)) d := rpc.NewConn(q, rpc.ConnLog(log), rpc.BootstrapFunc(bootstrapPingPong)) defer d.Wait() defer c.Close() ctx, cancel := context.WithCancel(context.Background()) defer cancel() client := testcapnp.PingPong{Client: c.Bootstrap(ctx)} b.ResetTimer() for i := 0; i < b.N; i++ { promise := client.EchoNum(ctx, func(p testcapnp.PingPong_echoNum_Params) error { p.SetN(42) return nil }) result, err := promise.Struct() if err != nil { b.Errorf("EchoNum(42) failed on iteration %d: %v", i, err) break } if result.N() != 42 { b.Errorf("EchoNum(42) = %d; want 42", result.N()) break } } }
func TestEmbargo(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() p, q := pipetransport.New() if *logMessages { p = logtransport.New(nil, p) } log := testLogger{t} c := rpc.NewConn(p, rpc.ConnLog(log)) echoSrv := testcapnp.Echoer_ServerToClient(new(Echoer)) d := rpc.NewConn(q, rpc.MainInterface(echoSrv.Client), rpc.ConnLog(log)) defer d.Wait() defer c.Close() client := testcapnp.Echoer{Client: c.Bootstrap(ctx)} localCap := testcapnp.CallOrder_ServerToClient(new(CallOrder)) earlyCall := callseq(ctx, client.Client, 0) echo := client.Echo(ctx, func(p testcapnp.Echoer_echo_Params) error { return p.SetCap(localCap) }) pipeline := echo.Cap() call0 := callseq(ctx, pipeline.Client, 0) call1 := callseq(ctx, pipeline.Client, 1) _, err := earlyCall.Struct() if err != nil { t.Errorf("earlyCall error: %v", err) } call2 := callseq(ctx, pipeline.Client, 2) _, err = echo.Struct() if err != nil { t.Errorf("echo.Get() error: %v", err) } call3 := callseq(ctx, pipeline.Client, 3) call4 := callseq(ctx, pipeline.Client, 4) call5 := callseq(ctx, pipeline.Client, 5) check := func(promise testcapnp.CallOrder_getCallSequence_Results_Promise, n uint32) { r, err := promise.Struct() if err != nil { t.Errorf("call%d error: %v", n, err) } if r.N() != n { t.Errorf("call%d = %d; want %d", n, r.N(), n) } } check(call0, 0) check(call1, 1) check(call2, 2) check(call3, 3) check(call4, 4) check(call5, 5) }
func newUnpairedConn(t *testing.T, options ...rpc.ConnOption) (*rpc.Conn, rpc.Transport) { p, q := pipetransport.New() if *logMessages { p = logtransport.New(nil, p) } newopts := make([]rpc.ConnOption, len(options), len(options)+1) copy(newopts, options) newopts = append(newopts, rpc.ConnLog(testLogger{t})) c := rpc.NewConn(p, newopts...) return c, q }
func TestPromisedCapability(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() p, q := pipetransport.New() if *logMessages { p = logtransport.New(nil, p) } log := testLogger{t} c := rpc.NewConn(p, rpc.ConnLog(log)) delay := make(chan struct{}) echoSrv := testcapnp.Echoer_ServerToClient(&DelayEchoer{delay: delay}) d := rpc.NewConn(q, rpc.MainInterface(echoSrv.Client), rpc.ConnLog(log)) defer d.Wait() defer c.Close() client := testcapnp.Echoer{Client: c.Bootstrap(ctx)} echo := client.Echo(ctx, func(p testcapnp.Echoer_echo_Params) error { return p.SetCap(testcapnp.CallOrder{Client: client.Client}) }) pipeline := echo.Cap() call0 := callseq(ctx, pipeline.Client, 0) call1 := callseq(ctx, pipeline.Client, 1) close(delay) check := func(promise testcapnp.CallOrder_getCallSequence_Results_Promise, n uint32) { r, err := promise.Struct() if err != nil { t.Errorf("call%d error: %v", n, err) } if r.N() != n { t.Errorf("call%d = %d; want %d", n, r.N(), n) } } check(call0, 0) check(call1, 1) }