github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/runner/compat.go (about) 1 // Copyright 2021 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package runner 6 7 import ( 8 "context" 9 "io" 10 11 "go.chromium.org/tast/core/errors" 12 "go.chromium.org/tast/core/internal/protocol" 13 "go.chromium.org/tast/core/internal/rpc" 14 ) 15 16 type compatServer struct { 17 cw *io.PipeWriter 18 cr *io.PipeReader 19 conn *rpc.GenericClient 20 } 21 22 func (s *compatServer) Close() { 23 s.conn.Close() 24 s.cw.Close() 25 s.cr.Close() 26 } 27 28 func (s *compatServer) Client() protocol.TestServiceClient { 29 return protocol.NewTestServiceClient(s.conn.Conn()) 30 } 31 32 // startCompatServer starts an in-process gRPC server to be used to implement 33 // JSON-based protocol handlers. 34 func startCompatServer(ctx context.Context, scfg *StaticConfig, req *protocol.HandshakeRequest) (s *compatServer, retErr error) { 35 sr, cw := io.Pipe() 36 cr, sw := io.Pipe() 37 defer func() { 38 if retErr != nil { 39 cw.Close() 40 cr.Close() 41 } 42 }() 43 go runRPCServer(scfg, sr, sw) 44 45 conn, err := rpc.NewClient(ctx, cr, cw, req) 46 if err != nil { 47 return nil, errors.Wrap(err, "failed to connect to in-process gRPC server") 48 } 49 50 return &compatServer{ 51 cw: cw, 52 cr: cr, 53 conn: conn, 54 }, nil 55 }