...

Source file src/github.com/grpc-ecosystem/grpc-gateway/examples/internal/integration/main_test.go

Documentation: github.com/grpc-ecosystem/grpc-gateway/examples/internal/integration

     1  package integration_test
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"net/http"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/golang/glog"
    13  	"github.com/grpc-ecosystem/grpc-gateway/examples/internal/gateway"
    14  	server "github.com/grpc-ecosystem/grpc-gateway/examples/internal/server"
    15  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
    16  )
    17  
    18  var (
    19  	endpoint   = flag.String("endpoint", "localhost:9090", "endpoint of the gRPC service")
    20  	network    = flag.String("network", "tcp", `one of "tcp" or "unix". Must be consistent to -endpoint`)
    21  	swaggerDir = flag.String("swagger_dir", "examples/internal/proto/examplepb", "path to the directory which contains swagger definitions")
    22  )
    23  
    24  func runGateway(ctx context.Context, addr string, opts ...gwruntime.ServeMuxOption) error {
    25  	return gateway.Run(ctx, gateway.Options{
    26  		Addr: addr,
    27  		GRPCServer: gateway.Endpoint{
    28  			Network: *network,
    29  			Addr:    *endpoint,
    30  		},
    31  		SwaggerDir: *swaggerDir,
    32  		Mux:        opts,
    33  	})
    34  }
    35  
    36  func waitForGateway(ctx context.Context, port uint16) error {
    37  	ch := time.After(10 * time.Second)
    38  
    39  	var err error
    40  	for {
    41  		if r, err := http.Get(fmt.Sprintf("http://localhost:%d/healthz", port)); err == nil {
    42  			if r.StatusCode == http.StatusOK {
    43  				return nil
    44  			}
    45  			err = fmt.Errorf("server localhost:%d returned an unexpected status %d", port, r.StatusCode)
    46  		}
    47  
    48  		glog.Infof("Waiting for localhost:%d to get ready", port)
    49  		select {
    50  		case <-ctx.Done():
    51  			return err
    52  		case <-ch:
    53  			return err
    54  		case <-time.After(10 * time.Millisecond):
    55  		}
    56  	}
    57  }
    58  
    59  func runServers(ctx context.Context) <-chan error {
    60  	ch := make(chan error, 3)
    61  	go func() {
    62  		if err := server.Run(ctx, *network, *endpoint); err != nil {
    63  			ch <- fmt.Errorf("cannot run grpc service: %v", err)
    64  		}
    65  	}()
    66  	go func() {
    67  		if err := runGateway(ctx, ":8088"); err != nil {
    68  			ch <- fmt.Errorf("cannot run gateway service: %v", err)
    69  		}
    70  	}()
    71  	go func() {
    72  		if err := server.RunInProcessGateway(ctx, ":8089"); err != nil {
    73  			ch <- fmt.Errorf("cannot run in process gateway service: %v", err)
    74  		}
    75  	}()
    76  	return ch
    77  }
    78  
    79  func TestMain(m *testing.M) {
    80  	flag.Parse()
    81  	defer glog.Flush()
    82  
    83  	ctx, cancel := context.WithCancel(context.Background())
    84  	defer cancel()
    85  	errCh := runServers(ctx)
    86  
    87  	ch := make(chan int, 1)
    88  	go func() {
    89  		if err := waitForGateway(ctx, 8088); err != nil {
    90  			glog.Errorf("waitForGateway(ctx, 8088) failed with %v; want success", err)
    91  		}
    92  		ch <- m.Run()
    93  	}()
    94  
    95  	select {
    96  	case err := <-errCh:
    97  		fmt.Fprintln(os.Stderr, err)
    98  		os.Exit(1)
    99  	case status := <-ch:
   100  		cancel()
   101  		os.Exit(status)
   102  	}
   103  }
   104  

View as plain text