github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlark_test.go (about)

     1  package larking
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"testing"
     8  
     9  	"github.com/emcfarlane/larking/starlib"
    10  	"github.com/emcfarlane/larking/testpb"
    11  	"github.com/google/go-cmp/cmp"
    12  	"go.starlark.net/starlark"
    13  	"golang.org/x/sync/errgroup"
    14  	"google.golang.org/grpc"
    15  	"google.golang.org/grpc/credentials/insecure"
    16  	"google.golang.org/grpc/reflection"
    17  	"google.golang.org/protobuf/proto"
    18  	"google.golang.org/protobuf/testing/protocmp"
    19  )
    20  
    21  func TestStarlark(t *testing.T) {
    22  	opts := cmp.Options{protocmp.Transform()}
    23  
    24  	ms := &testpb.UnimplementedMessagingServer{}
    25  	fs := &testpb.UnimplementedFilesServer{}
    26  	gs := grpc.NewServer(
    27  		grpc.UnaryInterceptor(
    28  			func(
    29  				_ context.Context,
    30  				req interface{},
    31  				info *grpc.UnaryServerInfo,
    32  				_ grpc.UnaryHandler,
    33  			) (interface{}, error) {
    34  				// TODO: fix overrides...
    35  				wantMethod := "/larking.testpb.Messaging/GetMessageOne"
    36  				if info.FullMethod != wantMethod {
    37  					return nil, fmt.Errorf("grpc expected %s, got %s", wantMethod, info.FullMethod)
    38  				}
    39  
    40  				msg := req.(proto.Message)
    41  				wantIn := &testpb.GetMessageRequestOne{Name: "starlark"}
    42  				diff := cmp.Diff(msg, wantIn, opts...)
    43  				if diff != "" {
    44  					return nil, fmt.Errorf(diff)
    45  				}
    46  				return &testpb.Message{
    47  					MessageId: "starlark",
    48  					Text:      "hello",
    49  					UserId:    "user",
    50  				}, nil
    51  			},
    52  		),
    53  	)
    54  	testpb.RegisterMessagingServer(gs, ms)
    55  	testpb.RegisterFilesServer(gs, fs)
    56  	reflection.Register(gs)
    57  
    58  	lis, err := net.Listen("tcp", "localhost:0")
    59  	if err != nil {
    60  		t.Fatalf("failed to listen: %v", err)
    61  	}
    62  	t.Cleanup(func() { lis.Close() })
    63  
    64  	var g errgroup.Group
    65  	//defer func() {
    66  	t.Cleanup(func() {
    67  		if err := g.Wait(); err != nil {
    68  			t.Fatal(err)
    69  		}
    70  	})
    71  
    72  	g.Go(func() error {
    73  		return gs.Serve(lis)
    74  	})
    75  	t.Cleanup(gs.Stop)
    76  
    77  	// Create client.
    78  	conn, err := grpc.Dial(
    79  		lis.Addr().String(),
    80  		grpc.WithTransportCredentials(insecure.NewCredentials()),
    81  	)
    82  	if err != nil {
    83  		t.Fatalf("cannot connect to server: %v", err)
    84  	}
    85  	t.Cleanup(func() { conn.Close() })
    86  	//defer conn.Close()
    87  
    88  	mux, err := NewMux()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	if err := mux.RegisterConn(context.Background(), conn); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	globals := starlark.StringDict{
    97  		"mux": mux,
    98  	}
    99  	t.Log("running")
   100  	starlib.RunTests(t, "testdata/*.star", globals)
   101  }