1// Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT.
2
3// +build go1.10
4
5package s3
6
7import (
8	"bytes"
9	"io/ioutil"
10	"net/http"
11	"reflect"
12	"testing"
13	"time"
14
15	"github.com/aws/aws-sdk-go/aws"
16	"github.com/aws/aws-sdk-go/aws/awserr"
17	"github.com/aws/aws-sdk-go/aws/corehandlers"
18	"github.com/aws/aws-sdk-go/aws/request"
19	"github.com/aws/aws-sdk-go/awstesting/unit"
20	"github.com/aws/aws-sdk-go/private/protocol"
21	"github.com/aws/aws-sdk-go/private/protocol/eventstream"
22	"github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamapi"
23	"github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamtest"
24	"github.com/aws/aws-sdk-go/private/protocol/restxml"
25)
26
27var _ time.Time
28var _ awserr.Error
29
30func TestSelectObjectContent_Read(t *testing.T) {
31	expectEvents, eventMsgs := mockSelectObjectContentReadEvents()
32	sess, cleanupFn, err := eventstreamtest.SetupEventStreamSession(t,
33		eventstreamtest.ServeEventStream{
34			T:      t,
35			Events: eventMsgs,
36		},
37		true,
38	)
39	if err != nil {
40		t.Fatalf("expect no error, %v", err)
41	}
42	defer cleanupFn()
43
44	svc := New(sess)
45	resp, err := svc.SelectObjectContent(nil)
46	if err != nil {
47		t.Fatalf("expect no error got, %v", err)
48	}
49	defer resp.GetStream().Close()
50
51	var i int
52	for event := range resp.GetStream().Events() {
53		if event == nil {
54			t.Errorf("%d, expect event, got nil", i)
55		}
56		if e, a := expectEvents[i], event; !reflect.DeepEqual(e, a) {
57			t.Errorf("%d, expect %T %v, got %T %v", i, e, e, a, a)
58		}
59		i++
60	}
61
62	if err := resp.GetStream().Err(); err != nil {
63		t.Errorf("expect no error, %v", err)
64	}
65}
66
67func TestSelectObjectContent_ReadClose(t *testing.T) {
68	_, eventMsgs := mockSelectObjectContentReadEvents()
69	sess, cleanupFn, err := eventstreamtest.SetupEventStreamSession(t,
70		eventstreamtest.ServeEventStream{
71			T:      t,
72			Events: eventMsgs,
73		},
74		true,
75	)
76	if err != nil {
77		t.Fatalf("expect no error, %v", err)
78	}
79	defer cleanupFn()
80
81	svc := New(sess)
82	resp, err := svc.SelectObjectContent(nil)
83	if err != nil {
84		t.Fatalf("expect no error got, %v", err)
85	}
86
87	// Assert calling Err before close does not close the stream.
88	resp.GetStream().Err()
89	select {
90	case _, ok := <-resp.GetStream().Events():
91		if !ok {
92			t.Fatalf("expect stream not to be closed, but was")
93		}
94	default:
95	}
96
97	resp.GetStream().Close()
98	<-resp.GetStream().Events()
99
100	if err := resp.GetStream().Err(); err != nil {
101		t.Errorf("expect no error, %v", err)
102	}
103}
104
105func BenchmarkSelectObjectContent_Read(b *testing.B) {
106	_, eventMsgs := mockSelectObjectContentReadEvents()
107	var buf bytes.Buffer
108	encoder := eventstream.NewEncoder(&buf)
109	for _, msg := range eventMsgs {
110		if err := encoder.Encode(msg); err != nil {
111			b.Fatalf("failed to encode message, %v", err)
112		}
113	}
114	stream := &loopReader{source: bytes.NewReader(buf.Bytes())}
115
116	sess := unit.Session
117	svc := New(sess, &aws.Config{
118		Endpoint:               aws.String("https://example.com"),
119		DisableParamValidation: aws.Bool(true),
120	})
121	svc.Handlers.Send.Swap(corehandlers.SendHandler.Name,
122		request.NamedHandler{Name: "mockSend",
123			Fn: func(r *request.Request) {
124				r.HTTPResponse = &http.Response{
125					Status:     "200 OK",
126					StatusCode: 200,
127					Header:     http.Header{},
128					Body:       ioutil.NopCloser(stream),
129				}
130			},
131		},
132	)
133
134	resp, err := svc.SelectObjectContent(nil)
135	if err != nil {
136		b.Fatalf("failed to create request, %v", err)
137	}
138	defer resp.GetStream().Close()
139	b.ResetTimer()
140
141	for i := 0; i < b.N; i++ {
142		if err = resp.GetStream().Err(); err != nil {
143			b.Fatalf("expect no error, got %v", err)
144		}
145		event := <-resp.GetStream().Events()
146		if event == nil {
147			b.Fatalf("expect event, got nil, %v, %d", resp.GetStream().Err(), i)
148		}
149	}
150}
151
152func mockSelectObjectContentReadEvents() (
153	[]SelectObjectContentEventStreamEvent,
154	[]eventstream.Message,
155) {
156	expectEvents := []SelectObjectContentEventStreamEvent{
157		&ContinuationEvent{},
158		&EndEvent{},
159		&ProgressEvent{
160			Details: &Progress{
161				BytesProcessed: aws.Int64(1234),
162				BytesReturned:  aws.Int64(1234),
163				BytesScanned:   aws.Int64(1234),
164			},
165		},
166		&RecordsEvent{
167			Payload: []byte("blob value goes here"),
168		},
169		&StatsEvent{
170			Details: &Stats{
171				BytesProcessed: aws.Int64(1234),
172				BytesReturned:  aws.Int64(1234),
173				BytesScanned:   aws.Int64(1234),
174			},
175		},
176	}
177
178	var marshalers request.HandlerList
179	marshalers.PushBackNamed(restxml.BuildHandler)
180	payloadMarshaler := protocol.HandlerPayloadMarshal{
181		Marshalers: marshalers,
182	}
183	_ = payloadMarshaler
184
185	eventMsgs := []eventstream.Message{
186		{
187			Headers: eventstream.Headers{
188				eventstreamtest.EventMessageTypeHeader,
189				{
190					Name:  eventstreamapi.EventTypeHeader,
191					Value: eventstream.StringValue("Cont"),
192				},
193			},
194		},
195		{
196			Headers: eventstream.Headers{
197				eventstreamtest.EventMessageTypeHeader,
198				{
199					Name:  eventstreamapi.EventTypeHeader,
200					Value: eventstream.StringValue("End"),
201				},
202			},
203		},
204		{
205			Headers: eventstream.Headers{
206				eventstreamtest.EventMessageTypeHeader,
207				{
208					Name:  eventstreamapi.EventTypeHeader,
209					Value: eventstream.StringValue("Progress"),
210				},
211			},
212			Payload: eventstreamtest.MarshalEventPayload(payloadMarshaler, expectEvents[2]),
213		},
214		{
215			Headers: eventstream.Headers{
216				eventstreamtest.EventMessageTypeHeader,
217				{
218					Name:  eventstreamapi.EventTypeHeader,
219					Value: eventstream.StringValue("Records"),
220				},
221			},
222			Payload: expectEvents[3].(*RecordsEvent).Payload,
223		},
224		{
225			Headers: eventstream.Headers{
226				eventstreamtest.EventMessageTypeHeader,
227				{
228					Name:  eventstreamapi.EventTypeHeader,
229					Value: eventstream.StringValue("Stats"),
230				},
231			},
232			Payload: eventstreamtest.MarshalEventPayload(payloadMarshaler, expectEvents[4]),
233		},
234	}
235
236	return expectEvents, eventMsgs
237}
238
239type loopReader struct {
240	source *bytes.Reader
241}
242
243func (c *loopReader) Read(p []byte) (int, error) {
244	if c.source.Len() == 0 {
245		c.source.Seek(0, 0)
246	}
247
248	return c.source.Read(p)
249}
250