/
request_stream.go
121 lines (107 loc) · 3.99 KB
/
request_stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package yarpctest
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/peer/hostport"
"go.uber.org/yarpc/transport/grpc"
"go.uber.org/yarpc/x/yarpctest/api"
)
// GRPCStreamRequest creates a new grpc stream request.
func GRPCStreamRequest(options ...api.ClientStreamRequestOption) api.Action {
return api.ActionFunc(func(t testing.TB) {
opts := api.NewClientStreamRequestOpts()
for _, option := range options {
option.ApplyClientStreamRequest(&opts)
}
trans := grpc.NewTransport()
chooser, err := opts.NewChooser(hostport.PeerIdentifier(fmt.Sprintf("127.0.0.1:%d", opts.Port)), trans)
require.NoError(t, err, "failed to create chooser")
out := trans.NewOutbound(chooser)
require.NoError(t, trans.Start())
defer func() { assert.NoError(t, trans.Stop()) }()
require.NoError(t, out.Start())
defer func() { assert.NoError(t, out.Stop()) }()
err = callStream(t, out, opts.GiveRequest, opts.StreamActions)
if len(opts.WantErrMsgs) > 0 {
require.Error(t, err)
for _, wantErrMsg := range opts.WantErrMsgs {
require.Contains(t, err.Error(), wantErrMsg)
}
return
}
require.NoError(t, err)
})
}
func callStream(
t testing.TB,
out transport.StreamOutbound,
req *transport.StreamRequest,
actions []api.ClientStreamAction,
) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
client, err := out.CallStream(ctx, req)
if err != nil {
return err
}
for _, a := range actions {
a.ApplyClientStream(t, client)
}
return nil
}
// ClientStreamActions combines a series of client stream actions into actions
// that will be applied when the StreamRequest is run.
func ClientStreamActions(actions ...api.ClientStreamAction) api.ClientStreamRequestOption {
return api.ClientStreamRequestOptionFunc(func(opts *api.ClientStreamRequestOpts) {
opts.StreamActions = actions
})
}
// WantStreamError asserts that the stream request had an error immediately.
func WantStreamError(wantErrMsgs ...string) api.ClientStreamRequestOption {
return api.ClientStreamRequestOptionFunc(func(opts *api.ClientStreamRequestOpts) {
opts.WantErrMsgs = wantErrMsgs
})
}
// CLIENT-SPECIFIC STREAM ACTIONS (see stream.go for generic stream actions)
// CloseStream is an action to close a client stream.
func CloseStream() api.ClientStreamAction {
return api.ClientStreamActionFunc(func(t testing.TB, c *transport.ClientStream) {
require.NoError(t, c.Close(context.Background()))
})
}
// WantHeaders is an action to fetch the client stream headers.
func WantHeaders(want map[string]string) api.ClientStreamAction {
return api.ClientStreamActionFunc(func(t testing.TB, c *transport.ClientStream) {
got, err := c.Headers()
require.NoError(t, err)
for k, v := range want {
g, ok := got.Get(k)
require.True(t, ok)
assert.Equal(t, v, g)
}
})
}