github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/parallel_support/rpc_server.go (about) 1 /* 2 3 The remote package provides the pieces to allow Ginkgo test suites to report to remote listeners. 4 This is used, primarily, to enable streaming parallel test output but has, in principal, broader applications (e.g. streaming test output to a browser). 5 6 */ 7 8 package parallel_support 9 10 import ( 11 "io" 12 "net" 13 "net/http" 14 "net/rpc" 15 16 "github.com/onsi/ginkgo/reporters" 17 ) 18 19 /* 20 RPCServer spins up on an automatically selected port and listens for communication from the forwarding reporter. 21 It then forwards that communication to attached reporters. 22 */ 23 type RPCServer struct { 24 listener net.Listener 25 handler *ServerHandler 26 } 27 28 //Create a new server, automatically selecting a port 29 func newRPCServer(parallelTotal int, reporter reporters.Reporter) (*RPCServer, error) { 30 listener, err := net.Listen("tcp", "127.0.0.1:0") 31 if err != nil { 32 return nil, err 33 } 34 return &RPCServer{ 35 listener: listener, 36 handler: newServerHandler(parallelTotal, reporter), 37 }, nil 38 } 39 40 //Start the server. You don't need to `go s.Start()`, just `s.Start()` 41 func (server *RPCServer) Start() { 42 rpcServer := rpc.NewServer() 43 rpcServer.RegisterName("Server", server.handler) //register the handler's methods as the server 44 45 httpServer := &http.Server{} 46 httpServer.Handler = rpcServer 47 48 go httpServer.Serve(server.listener) 49 } 50 51 //Stop the server 52 func (server *RPCServer) Close() { 53 server.listener.Close() 54 } 55 56 //The address the server can be reached it. Pass this into the `ForwardingReporter`. 57 func (server *RPCServer) Address() string { 58 return server.listener.Addr().String() 59 } 60 61 func (server *RPCServer) GetSuiteDone() chan interface{} { 62 return server.handler.done 63 } 64 65 func (server *RPCServer) GetOutputDestination() io.Writer { 66 return server.handler.outputDestination 67 } 68 69 func (server *RPCServer) SetOutputDestination(w io.Writer) { 70 server.handler.outputDestination = w 71 } 72 73 func (server *RPCServer) RegisterAlive(node int, alive func() bool) { 74 server.handler.registerAlive(node, alive) 75 }