go.uber.org/yarpc@v1.72.1/x/yarpctest/request_stream.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 yarpctest
    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/yarpc/api/transport"
    32  	"go.uber.org/yarpc/peer/hostport"
    33  	"go.uber.org/yarpc/transport/grpc"
    34  	"go.uber.org/yarpc/x/yarpctest/api"
    35  )
    36  
    37  // GRPCStreamRequest creates a new grpc stream request.
    38  func GRPCStreamRequest(options ...api.ClientStreamRequestOption) api.Action {
    39  	return api.ActionFunc(func(t testing.TB) {
    40  		opts := api.NewClientStreamRequestOpts()
    41  		for _, option := range options {
    42  			option.ApplyClientStreamRequest(&opts)
    43  		}
    44  
    45  		trans := grpc.NewTransport()
    46  		chooser, err := opts.NewChooser(hostport.PeerIdentifier(fmt.Sprintf("127.0.0.1:%d", opts.Port)), trans)
    47  		require.NoError(t, err, "failed to create chooser")
    48  		out := trans.NewOutbound(chooser)
    49  
    50  		require.NoError(t, trans.Start())
    51  		defer func() { assert.NoError(t, trans.Stop()) }()
    52  
    53  		require.NoError(t, out.Start())
    54  		defer func() { assert.NoError(t, out.Stop()) }()
    55  
    56  		err = callStream(t, out, opts.GiveRequest, opts.StreamActions)
    57  		if len(opts.WantErrMsgs) > 0 {
    58  			require.Error(t, err)
    59  			for _, wantErrMsg := range opts.WantErrMsgs {
    60  				require.Contains(t, err.Error(), wantErrMsg)
    61  			}
    62  			return
    63  		}
    64  		require.NoError(t, err)
    65  	})
    66  }
    67  
    68  func callStream(
    69  	t testing.TB,
    70  	out transport.StreamOutbound,
    71  	req *transport.StreamRequest,
    72  	actions []api.ClientStreamAction,
    73  ) error {
    74  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    75  	defer cancel()
    76  	client, err := out.CallStream(ctx, req)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	for _, a := range actions {
    81  		a.ApplyClientStream(t, client)
    82  	}
    83  	return nil
    84  }
    85  
    86  // ClientStreamActions combines a series of client stream actions into actions
    87  // that will be applied when the StreamRequest is run.
    88  func ClientStreamActions(actions ...api.ClientStreamAction) api.ClientStreamRequestOption {
    89  	return api.ClientStreamRequestOptionFunc(func(opts *api.ClientStreamRequestOpts) {
    90  		opts.StreamActions = actions
    91  	})
    92  }
    93  
    94  // WantStreamError asserts that the stream request had an error immediately.
    95  func WantStreamError(wantErrMsgs ...string) api.ClientStreamRequestOption {
    96  	return api.ClientStreamRequestOptionFunc(func(opts *api.ClientStreamRequestOpts) {
    97  		opts.WantErrMsgs = wantErrMsgs
    98  	})
    99  }
   100  
   101  // CLIENT-SPECIFIC STREAM ACTIONS (see stream.go for generic stream actions)
   102  
   103  // CloseStream is an action to close a client stream.
   104  func CloseStream() api.ClientStreamAction {
   105  	return api.ClientStreamActionFunc(func(t testing.TB, c *transport.ClientStream) {
   106  		require.NoError(t, c.Close(context.Background()))
   107  	})
   108  }
   109  
   110  // WantHeaders is an action to fetch the client stream headers.
   111  func WantHeaders(want map[string]string) api.ClientStreamAction {
   112  	return api.ClientStreamActionFunc(func(t testing.TB, c *transport.ClientStream) {
   113  		got, err := c.Headers()
   114  		require.NoError(t, err)
   115  		for k, v := range want {
   116  			g, ok := got.Get(k)
   117  			require.True(t, ok)
   118  			assert.Equal(t, v, g)
   119  		}
   120  	})
   121  }