go.uber.org/yarpc@v1.72.1/x/yarpctest/types/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 types 22 23 import ( 24 "context" 25 "io" 26 "io/ioutil" 27 "testing" 28 29 "github.com/stretchr/testify/require" 30 "go.uber.org/yarpc/api/transport" 31 "go.uber.org/yarpc/x/yarpctest/api" 32 ) 33 34 // SendStreamMsg is an action to send a message to a stream. It can be 35 // applied to either a server or client stream. 36 type SendStreamMsg struct { 37 api.SafeTestingTBOnStart 38 api.NoopStop 39 40 BodyFunc func() io.ReadCloser 41 WantErrMsgs []string 42 } 43 44 // ApplyClientStream implements ClientStreamAction 45 func (s *SendStreamMsg) ApplyClientStream(t testing.TB, c *transport.ClientStream) { 46 s.applyStream(t, c) 47 } 48 49 // ApplyServerStream implements ServerStreamAction 50 func (s *SendStreamMsg) ApplyServerStream(c *transport.ServerStream) error { 51 s.applyStream(s.GetTestingTB(), c) 52 return nil 53 } 54 55 func (s *SendStreamMsg) applyStream(t testing.TB, c transport.Stream) { 56 err := c.SendMessage( 57 context.Background(), 58 &transport.StreamMessage{ 59 Body: s.BodyFunc(), 60 }, 61 ) 62 if len(s.WantErrMsgs) > 0 { 63 require.Error(t, err) 64 for _, wantErrMsg := range s.WantErrMsgs { 65 require.Contains(t, err.Error(), wantErrMsg) 66 } 67 return 68 } 69 require.NoError(t, err) 70 } 71 72 // RecvStreamMsg is an action to receive a message from a stream. It can 73 // be applied to either a server or client stream. 74 type RecvStreamMsg struct { 75 api.SafeTestingTBOnStart 76 api.NoopStop 77 78 WantBody []byte 79 WantDecodeErrMsgs []string 80 WantErrMsgs []string 81 82 WantErr error 83 } 84 85 // ApplyClientStream implements ClientStreamAction 86 func (s *RecvStreamMsg) ApplyClientStream(t testing.TB, c *transport.ClientStream) { 87 s.applyStream(t, c) 88 } 89 90 // ApplyServerStream implements ServerStreamAction 91 func (s *RecvStreamMsg) ApplyServerStream(c *transport.ServerStream) error { 92 s.applyStream(s.GetTestingTB(), c) 93 return nil 94 } 95 96 func (s *RecvStreamMsg) applyStream(t testing.TB, c transport.Stream) { 97 msg, err := c.ReceiveMessage(context.Background()) 98 if len(s.WantErrMsgs) > 0 { 99 require.Error(t, err) 100 for _, wantErrMsg := range s.WantErrMsgs { 101 require.Contains(t, err.Error(), wantErrMsg) 102 } 103 return 104 } 105 if s.WantErr != nil { 106 require.Error(t, err) 107 require.Equal(t, s.WantErr, err) 108 return 109 } 110 require.NoError(t, err) 111 112 actualMsg, err := ioutil.ReadAll(msg.Body) 113 if len(s.WantDecodeErrMsgs) > 0 { 114 require.Error(t, err) 115 for _, wantErrMsg := range s.WantDecodeErrMsgs { 116 require.Contains(t, err.Error(), wantErrMsg) 117 } 118 return 119 } 120 require.NoError(t, err) 121 require.Equal(t, s.WantBody, actualMsg, "mismatch on stream messages") 122 }