func setUpSignerClient(t *testing.T, grpcServer *grpc.Server) (*client.NotarySigner, *grpc.ClientConn, func()) { socketFile, err := ioutil.TempFile("", "notary-grpc-test") require.NoError(t, err) socketFile.Close() os.Remove(socketFile.Name()) lis, err := net.Listen("unix", socketFile.Name()) require.NoError(t, err, "unable to open socket to listen") go grpcServer.Serve(lis) // client setup clientConn, err := grpc.Dial(socketFile.Name(), grpc.WithInsecure(), grpc.WithDialer(socketDialer)) require.NoError(t, err, "unable to connect to socket as a GRPC client") signerClient := client.NewNotarySigner(clientConn) cleanup := func() { clientConn.Close() grpcServer.Stop() os.Remove(socketFile.Name()) } return signerClient, clientConn, cleanup }
func waitShutdown(rpcSrv *grpc.Server, apiSrv *server.ApiServer) { sigCh := make(chan os.Signal) signal.Notify(sigCh, os.Interrupt, os.Kill) // Block until a signal is received or we got an error select { case signal := <-sigCh: log.Printf("Got signal %s, waiting for shutdown...", signal) rpcSrv.Stop() apiSrv.Shutdown() return } }
// ListenAndServeGRPC creates a listener and serves the specified grpc Server // on it, closing the listener when signalled by the stopper. func ListenAndServeGRPC(stopper *stop.Stopper, server *grpc.Server, addr net.Addr) (net.Listener, error) { ln, err := net.Listen(addr.Network(), addr.String()) if err != nil { return ln, err } stopper.RunWorker(func() { <-stopper.ShouldDrain() server.Stop() }) stopper.RunWorker(func() { FatalIfUnexpected(server.Serve(ln)) }) return ln, nil }
func tearDown(s *grpc.Server, cc *grpc.ClientConn) { cc.Close() s.Stop() }
func TestDisplay(t *testing.T) { var tCtx context.Context tests := []struct { ctx context.Context query Query cfg *Config generator generator err error }{{ ctx: tCtx, query: Query{ Target: "test", }, cfg: &Config{}, err: fmt.Errorf("grpc: no transport security set (use grpc.WithInsecure() explicitly or set credentials)"), }, { ctx: tCtx, query: Query{ Target: "test", }, cfg: nil, err: fmt.Errorf("cfg must not be nil"), }, { ctx: tCtx, query: Query{ Target: "localhost", DialOptions: []grpc.DialOption{ grpc.WithInsecure(), }, }, cfg: &Config{}, err: fmt.Errorf("query target must be specified"), }, { ctx: tCtx, query: Query{ Target: "localhost", DialOptions: []grpc.DialOption{ grpc.WithInsecure(), }, Queries: [][]string{ []string{"foo"}, }, }, cfg: &Config{}, generator: NewFixedGenerator(ocpb.Path{ Element: []string{"foo"}, }, ocpb.Value{ Value: []byte("42"), }), }} for _, tt := range tests { var s *grpc.Server var ft *fakeTarget if tt.query.Target == "localhost" { var err error s, ft, tt.query.Target, err = startLocalServer() if tt.generator != nil { ft.RegisterGenerator(tt.generator) } if err != nil { t.Fatal("failed to start server") } defer s.Stop() } t.Logf("test Display(%+v, %+v, %+v)", tt.ctx, tt.query, tt.cfg) if tt.cfg != nil { tt.cfg.Display = fakeDisplayer(ft, t) } err := Display(context.Background(), tt.query, tt.cfg) if tt.err != nil && err != nil && tt.err.Error() != err.Error() { t.Errorf("failed Display(%+v, %+v, %+v): got %s, want %s", tt.ctx, tt.query, tt.cfg, err, tt.err) } } }