go.uber.org/yarpc@v1.72.1/encoding/thrift/thriftrw-plugin-yarpc/extends_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
    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  
    32  	"go.uber.org/yarpc"
    33  	"go.uber.org/yarpc/api/transport"
    34  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/extends/barclient"
    35  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/extends/barserver"
    36  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/extends/fooclient"
    37  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/extends/fooserver"
    38  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/extends/nameclient"
    39  	"go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/extends/nameserver"
    40  	"go.uber.org/yarpc/internal/yarpctest"
    41  	"go.uber.org/yarpc/transport/http"
    42  )
    43  
    44  func setupTest(t *testing.T, p []transport.Procedure) (*yarpc.Dispatcher, func()) {
    45  	httpInbound := http.NewTransport().NewInbound("127.0.0.1:0")
    46  
    47  	server := yarpc.NewDispatcher(yarpc.Config{
    48  		Name:     "server",
    49  		Inbounds: yarpc.Inbounds{httpInbound},
    50  	})
    51  	server.Register(p)
    52  	require.NoError(t, server.Start())
    53  
    54  	outbound := http.NewTransport().NewSingleOutbound(
    55  		fmt.Sprintf("http://%v", yarpctest.ZeroAddrToHostPort(httpInbound.Addr())))
    56  
    57  	client := yarpc.NewDispatcher(yarpc.Config{
    58  		Name: "client",
    59  		Outbounds: yarpc.Outbounds{
    60  			"server": {
    61  				Unary:  outbound,
    62  				Oneway: outbound,
    63  			},
    64  		},
    65  	})
    66  	require.NoError(t, client.Start())
    67  
    68  	return client, func() {
    69  		assert.NoError(t, client.Stop())
    70  		assert.NoError(t, server.Stop())
    71  	}
    72  }
    73  
    74  func TestExtendsProcedure(t *testing.T) {
    75  	t.Run("base service: Name::name", func(t *testing.T) {
    76  		d, cleanup := setupTest(t, nameserver.New(&nameHandler{}))
    77  		defer cleanup()
    78  
    79  		cli := nameclient.New(d.ClientConfig("server"))
    80  		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    81  		defer cancel()
    82  
    83  		res, err := cli.Name(ctx)
    84  		require.NoError(t, err)
    85  		assert.Equal(t, "Name::name", res)
    86  	})
    87  	t.Run("foo service: Foo::name", func(t *testing.T) {
    88  		d, cleanup := setupTest(t, fooserver.New(&fooHandler{}))
    89  		defer cleanup()
    90  
    91  		cli := fooclient.New(d.ClientConfig("server"))
    92  		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    93  		defer cancel()
    94  
    95  		res, err := cli.Name(ctx)
    96  		require.NoError(t, err)
    97  		assert.Equal(t, "Foo::name", res)
    98  	})
    99  	t.Run("bar service: Bar::name", func(t *testing.T) {
   100  		d, cleanup := setupTest(t, barserver.New(&barHandler{}))
   101  		defer cleanup()
   102  
   103  		cli := barclient.New(d.ClientConfig("server"))
   104  		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
   105  		defer cancel()
   106  
   107  		res, err := cli.Name(ctx)
   108  		require.NoError(t, err)
   109  		assert.Equal(t, "Bar::name", res)
   110  	})
   111  }
   112  
   113  type nameHandler struct{}
   114  
   115  func (*nameHandler) Name(ctx context.Context) (string, error) {
   116  	return yarpc.CallFromContext(ctx).Procedure(), nil
   117  }
   118  
   119  type fooHandler struct{}
   120  
   121  func (*fooHandler) Name(ctx context.Context) (string, error) {
   122  	return yarpc.CallFromContext(ctx).Procedure(), nil
   123  }
   124  
   125  type barHandler struct{}
   126  
   127  func (*barHandler) Name(ctx context.Context) (string, error) {
   128  	return yarpc.CallFromContext(ctx).Procedure(), nil
   129  }