1// Licensed to Elasticsearch B.V. under one or more contributor 2// license agreements. See the NOTICE file distributed with 3// this work for additional information regarding copyright 4// ownership. Elasticsearch B.V. licenses this file to you under 5// the Apache License, Version 2.0 (the "License"); you may 6// not use this file except in compliance with the License. 7// You may obtain a copy of the License at 8// 9// http://www.apache.org/licenses/LICENSE-2.0 10// 11// Unless required by applicable law or agreed to in writing, 12// software distributed under the License is distributed on an 13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14// KIND, either express or implied. See the License for the 15// specific language governing permissions and limitations 16// under the License. 17 18package transporttest 19 20import ( 21 "context" 22 "io" 23 "io/ioutil" 24 25 "go.elastic.co/apm/transport" 26) 27 28// Discard is a transport.Transport which discards 29// all streams, and returns no errors. 30var Discard transport.Transport = ErrorTransport{} 31 32// ErrorTransport is a transport that returns the stored error 33// for each method call. 34type ErrorTransport struct { 35 Error error 36} 37 38// SendStream discards the stream and returns t.Error. 39func (t ErrorTransport) SendStream(ctx context.Context, r io.Reader) error { 40 errc := make(chan error, 1) 41 go func() { 42 _, err := io.Copy(ioutil.Discard, r) 43 errc <- err 44 }() 45 select { 46 case err := <-errc: 47 if err != nil { 48 return err 49 } 50 return t.Error 51 case <-ctx.Done(): 52 return ctx.Err() 53 } 54} 55