go.uber.org/yarpc@v1.72.1/x/yarpctest/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 "bytes" 25 "io" 26 "io/ioutil" 27 28 "go.uber.org/yarpc/x/yarpctest/types" 29 ) 30 31 // SendStreamMsg sends a message to a stream. 32 func SendStreamMsg(sendMsg string) *types.SendStreamMsg { 33 return &types.SendStreamMsg{ 34 BodyFunc: func() io.ReadCloser { 35 return ioutil.NopCloser(bytes.NewBufferString(sendMsg)) 36 }, 37 } 38 } 39 40 // SendStreamMsgAndExpectError sends a message on a stream and asserts on the 41 // error returned. 42 func SendStreamMsgAndExpectError(sendMsg string, wantErrMsgs ...string) *types.SendStreamMsg { 43 return &types.SendStreamMsg{ 44 BodyFunc: func() io.ReadCloser { 45 return ioutil.NopCloser(bytes.NewBufferString(sendMsg)) 46 }, 47 WantErrMsgs: wantErrMsgs, 48 } 49 } 50 51 // SendStreamDecodeErrorAndExpectError induces a decode error on the stream 52 // message and asserts on the result. 53 func SendStreamDecodeErrorAndExpectError(decodeErr error, wantErrMsgs ...string) *types.SendStreamMsg { 54 return &types.SendStreamMsg{ 55 BodyFunc: func() io.ReadCloser { 56 return ioutil.NopCloser(readErr{decodeErr}) 57 }, 58 WantErrMsgs: wantErrMsgs, 59 } 60 } 61 62 type readErr struct { 63 err error 64 } 65 66 func (r readErr) Read(p []byte) (n int, err error) { 67 return 0, r.err 68 } 69 70 // RecvStreamMsg waits to receive a message on a client stream. 71 func RecvStreamMsg(wantMsg string) *types.RecvStreamMsg { 72 return &types.RecvStreamMsg{WantBody: bytes.NewBufferString(wantMsg).Bytes()} 73 } 74 75 // RecvStreamErr waits to receive a message on a client stream. It expects 76 // error messages. 77 func RecvStreamErr(wantErrMsgs ...string) *types.RecvStreamMsg { 78 return &types.RecvStreamMsg{WantErrMsgs: wantErrMsgs} 79 } 80 81 // RecvStreamErrInstance waits to receive a message on a client stream. It expects 82 // an error. 83 func RecvStreamErrInstance(wantErr error) *types.RecvStreamMsg { 84 return &types.RecvStreamMsg{WantErr: wantErr} 85 }