tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/talk/peer_test.go (about) 1 package talk 2 3 import ( 4 "context" 5 "io" 6 "testing" 7 8 "tractor.dev/toolkit-go/duplex/codec" 9 "tractor.dev/toolkit-go/duplex/mux" 10 "tractor.dev/toolkit-go/duplex/rpc" 11 ) 12 13 func TestPeerBidirectional(t *testing.T) { 14 ar, bw := io.Pipe() 15 br, aw := io.Pipe() 16 sessA, _ := mux.DialIO(aw, ar) 17 sessB, _ := mux.DialIO(bw, br) 18 19 peerA := NewPeer(sessA, codec.JSONCodec{}) 20 peerB := NewPeer(sessB, codec.JSONCodec{}) 21 defer peerA.Close() 22 defer peerB.Close() 23 24 peerA.Handle("hello", rpc.HandlerFunc(func(r rpc.Responder, c *rpc.Call) { 25 r.Return("A") 26 })) 27 peerB.Handle("hello", rpc.HandlerFunc(func(r rpc.Responder, c *rpc.Call) { 28 r.Return("B") 29 })) 30 31 go peerA.Respond() 32 go peerB.Respond() 33 34 var retB string 35 _, err := peerA.Call(context.Background(), "hello", nil, &retB) 36 if err != nil { 37 t.Fatal(err) 38 } 39 if retB != "B" { 40 t.Fatal("unexpected return:", retB) 41 } 42 43 var retA string 44 _, err = peerB.Call(context.Background(), "hello", nil, &retA) 45 if err != nil { 46 t.Fatal(err) 47 } 48 if retA != "A" { 49 t.Fatal("unexpected return:", retA) 50 } 51 }