go.uber.org/yarpc@v1.72.1/encoding/thrift/thriftrw-plugin-yarpc/fx_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main_test
    22  
    23  import (
    24  	"context"
    25  	"fmt"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  	"go.uber.org/fx"
    32  	"go.uber.org/fx/fxtest"
    33  	"go.uber.org/thriftrw/ptr"
    34  	"go.uber.org/yarpc"
    35  	"go.uber.org/yarpc/api/transport"
    36  	"go.uber.org/yarpc/api/x/restriction"
    37  	"go.uber.org/yarpc/encoding/raw"
    38  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/atomic"
    39  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/atomic/readonlystoreclient"
    40  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/atomic/readonlystorefx"
    41  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/atomic/readonlystoreserver"
    42  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/atomic/storefx"
    43  	"go.uber.org/yarpc/transport/http"
    44  )
    45  
    46  func TestFxClient(t *testing.T) {
    47  	const storeServiceName = "store"
    48  
    49  	d := yarpc.NewDispatcher(yarpc.Config{
    50  		Name: "myservice",
    51  		Outbounds: yarpc.Outbounds{
    52  			storeServiceName: {Unary: http.NewTransport().NewSingleOutbound("http://127.0.0.1/yarpc")},
    53  		},
    54  	})
    55  
    56  	t.Run("success", func(t *testing.T) {
    57  		assert.NotPanics(t, func() {
    58  			p := storefx.Params{
    59  				Provider: d,
    60  			}
    61  			f := storefx.Client(storeServiceName).(func(storefx.Params) storefx.Result)
    62  			f(p)
    63  		}, "failed to build client")
    64  	})
    65  
    66  	t.Run("invalid config", func(t *testing.T) {
    67  		assert.PanicsWithValue(t, `no configured outbound transport for outbound key "not-store"`, func() {
    68  			f := storefx.Client("not-store").(func(storefx.Params) storefx.Result)
    69  			f(storefx.Params{
    70  				Provider: d,
    71  			})
    72  		}, "expected panics")
    73  	})
    74  
    75  	t.Run("restriction success", func(t *testing.T) {
    76  		r, err := restriction.NewChecker(restriction.Tuple{
    77  			Transport: "http", Encoding: "thrift",
    78  		})
    79  		require.NoError(t, err, "could not create restriction checker")
    80  
    81  		assert.NotPanics(t, func() {
    82  			p := storefx.Params{
    83  				Provider:    d,
    84  				Restriction: r,
    85  			}
    86  			f := storefx.Client(storeServiceName).(func(storefx.Params) storefx.Result)
    87  			f(p)
    88  		}, "failed to build client")
    89  	})
    90  
    91  	t.Run("restriction error", func(t *testing.T) {
    92  		r, err := restriction.NewChecker(restriction.Tuple{
    93  			Transport: "grpc", Encoding: "protobuf",
    94  		})
    95  		require.NoError(t, err, "could not create restriction checker")
    96  
    97  		assert.PanicsWithValue(t, `"http/thrift" is not a whitelisted combination, available: "grpc/protobuf"`, func() {
    98  			p := storefx.Params{
    99  				Provider:    d,
   100  				Restriction: r,
   101  			}
   102  			f := storefx.Client(storeServiceName).(func(storefx.Params) storefx.Result)
   103  			f(p)
   104  		}, "failed to build client")
   105  	})
   106  }
   107  
   108  func extractProcedures(procs *[]transport.Procedure) fx.Option {
   109  	type params struct {
   110  		fx.In
   111  
   112  		// We need to handle both cases: A single transport.Procedure provided
   113  		// to the "yarpcfx" group and a []transport.Procedure provided to the
   114  		// "yarpcfx" group.
   115  		SingleProcedures []transport.Procedure   `group:"yarpcfx"`
   116  		ProcedureLists   [][]transport.Procedure `group:"yarpcfx"`
   117  	}
   118  
   119  	return fx.Invoke(func(p params) {
   120  		*procs = append(*procs, p.SingleProcedures...)
   121  		for _, procList := range p.ProcedureLists {
   122  			*procs = append(*procs, procList...)
   123  		}
   124  	})
   125  }
   126  
   127  func echoRaw(ctx context.Context, req []byte) ([]byte, error) { return req, nil }
   128  
   129  func TestFxServer(t *testing.T) {
   130  	type rawProcedures struct {
   131  		fx.Out
   132  
   133  		Procedures []transport.Procedure `group:"yarpcfx"`
   134  	}
   135  
   136  	handler := readOnlyStoreHandler{
   137  		"foo":    1,
   138  		"bar":    2,
   139  		"answer": 42,
   140  	}
   141  
   142  	var procedures []transport.Procedure
   143  	serverApp := fxtest.New(t,
   144  		fx.Provide(
   145  			func() readonlystoreserver.Interface { return handler },
   146  			readonlystorefx.Server(),
   147  			func() rawProcedures {
   148  				return rawProcedures{Procedures: raw.Procedure("echoRaw", echoRaw)}
   149  			},
   150  		),
   151  		extractProcedures(&procedures),
   152  	)
   153  	defer serverApp.RequireStart().RequireStop()
   154  
   155  	inbound := http.NewTransport().NewInbound("127.0.0.1:0")
   156  	serverD := yarpc.NewDispatcher(yarpc.Config{
   157  		Name:     "myserver",
   158  		Inbounds: yarpc.Inbounds{inbound},
   159  	})
   160  	serverD.Register(procedures)
   161  	require.NoError(t, serverD.Start(), "failed to start server")
   162  	defer func() {
   163  		assert.NoError(t, serverD.Stop(), "failed to stop server")
   164  	}()
   165  
   166  	clientD := yarpc.NewDispatcher(yarpc.Config{
   167  		Name: "myclient",
   168  		Outbounds: yarpc.Outbounds{
   169  			"myserver": {
   170  				Unary: http.NewTransport().NewSingleOutbound(
   171  					fmt.Sprintf("http://%s/", inbound.Addr()),
   172  				),
   173  			},
   174  		},
   175  	})
   176  	require.NoError(t, clientD.Start(), "failed to start client")
   177  	defer func() {
   178  		assert.NoError(t, clientD.Stop(), "failed to stop client")
   179  	}()
   180  
   181  	client := readonlystoreclient.New(clientD.ClientConfig("myserver"))
   182  
   183  	ctx := context.Background()
   184  
   185  	t.Run("Integer", func(t *testing.T) {
   186  		ctx, cancel := context.WithTimeout(ctx, time.Second)
   187  		defer cancel()
   188  
   189  		res, err := client.Integer(ctx, ptr.String("answer"))
   190  		assert.NoError(t, err, "request failed")
   191  		assert.Equal(t, int64(42), res, "result did not match")
   192  	})
   193  
   194  	t.Run("Integer error", func(t *testing.T) {
   195  		ctx, cancel := context.WithTimeout(ctx, time.Second)
   196  		defer cancel()
   197  
   198  		_, err := client.Integer(ctx, ptr.String("baz")) // baz does not exist
   199  		assert.Error(t, err, "request failed")
   200  
   201  		exc, ok := err.(*atomic.KeyDoesNotExist)
   202  		require.True(t, ok, "error '%+v' must be a *KeyDoesNotExist, not %T", err, err)
   203  		assert.Equal(t, "baz", *exc.Key, "exception key did not match")
   204  	})
   205  
   206  	rawClient := raw.New(clientD.ClientConfig("myserver"))
   207  
   208  	t.Run("raw", func(t *testing.T) {
   209  		ctx, cancel := context.WithTimeout(ctx, time.Second)
   210  		defer cancel()
   211  
   212  		res, err := rawClient.Call(ctx, "echoRaw", []byte("hello"))
   213  		require.NoError(t, err, "request failed")
   214  		assert.Equal(t, "hello", string(res), "response body did not match")
   215  	})
   216  }
   217  
   218  type readOnlyStoreHandler map[string]int64
   219  
   220  func (readOnlyStoreHandler) Healthy(context.Context) (bool, error) {
   221  	return true, nil
   222  }
   223  
   224  func (h readOnlyStoreHandler) Integer(ctx context.Context, k *string) (int64, error) {
   225  	v, ok := h[*k]
   226  	if !ok {
   227  		return 0, &atomic.KeyDoesNotExist{Key: k}
   228  	}
   229  	return v, nil
   230  }