tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/rpc/rpctest/rpctest.go (about)

     1  package rpctest
     2  
     3  import (
     4  	"io"
     5  
     6  	"tractor.dev/toolkit-go/duplex/codec"
     7  	"tractor.dev/toolkit-go/duplex/mux"
     8  	"tractor.dev/toolkit-go/duplex/rpc"
     9  )
    10  
    11  // NewPair creates a Client and Server connected by in-memory pipes.
    12  // The server Respond method is called in a goroutine. Only the client
    13  // should need to be cleaned up with call to Close.
    14  func NewPair(handler rpc.Handler, codec codec.Codec) (*rpc.Client, *rpc.Server) {
    15  	ar, bw := io.Pipe()
    16  	br, aw := io.Pipe()
    17  	sessA, _ := mux.DialIO(aw, ar)
    18  	sessB, _ := mux.DialIO(bw, br)
    19  
    20  	srv := &rpc.Server{
    21  		Codec:   codec,
    22  		Handler: handler,
    23  	}
    24  	go srv.Respond(sessA, nil)
    25  
    26  	return rpc.NewClient(sessB, codec), srv
    27  }