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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
package awsendpointdiscoverytest
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting/unit"
)
func TestEndpointDiscovery(t *testing.T) {
svc := New(unit.Session, &aws.Config{
EnableEndpointDiscovery: aws.Bool(true),
})
svc.Handlers.Clear()
svc.Handlers.Send.PushBack(mockSendDescEndpoint)
var descCount int32
svc.Handlers.Complete.PushBack(func(r *request.Request) {
if r.Operation.Name != opDescribeEndpoints {
return
}
atomic.AddInt32(&descCount, 1)
})
for i := 0; i < 2; i++ {
req, _ := svc.TestDiscoveryIdentifiersRequiredRequest(
&TestDiscoveryIdentifiersRequiredInput{
Sdk: aws.String("sdk"),
},
)
req.Handlers.Send.PushBack(func(r *request.Request) {
if e, a := "http://foo", r.HTTPRequest.URL.String(); e != a {
t.Errorf("expected %q, but received %q", e, a)
}
})
if err := req.Send(); err != nil {
t.Fatal(err)
}
}
if e, a := int32(1), atomic.LoadInt32(&descCount); e != a {
t.Errorf("expect desc endpoint called %d, got %d", e, a)
}
}
func TestAsyncEndpointDiscovery(t *testing.T) {
t.Parallel()
svc := New(unit.Session, &aws.Config{
EnableEndpointDiscovery: aws.Bool(true),
})
svc.Handlers.Clear()
var firstAsyncReq sync.WaitGroup
firstAsyncReq.Add(1)
svc.Handlers.Build.PushBack(func(r *request.Request) {
if r.Operation.Name == opDescribeEndpoints {
firstAsyncReq.Wait()
}
})
svc.Handlers.Send.PushBack(mockSendDescEndpoint)
req, _ := svc.TestDiscoveryOptionalRequest(&TestDiscoveryOptionalInput{
Sdk: aws.String("sdk"),
})
const clientHost = "awsendpointdiscoverytestservice.mock-region.amazonaws.com"
req.Handlers.Send.PushBack(func(r *request.Request) {
if e, a := clientHost, r.HTTPRequest.URL.Host; e != a {
t.Errorf("expected %q, but received %q", e, a)
}
})
req.Handlers.Complete.PushBack(func(r *request.Request) {
firstAsyncReq.Done()
})
if err := req.Send(); err != nil {
t.Fatal(err)
}
var cacheUpdated bool
for s := time.Now().Add(10 * time.Second); s.After(time.Now()); {
// Wait for the cache to be updated before making second request.
if svc.endpointCache.Has(req.Operation.Name) {
cacheUpdated = true
break
}
time.Sleep(10 * time.Millisecond)
}
if !cacheUpdated {
t.Fatalf("expect endpoint cache to be updated, was not")
}
req, _ = svc.TestDiscoveryOptionalRequest(&TestDiscoveryOptionalInput{
Sdk: aws.String("sdk"),
})
req.Handlers.Send.PushBack(func(r *request.Request) {
if e, a := "http://foo", r.HTTPRequest.URL.String(); e != a {
t.Errorf("expected %q, but received %q", e, a)
}
})
if err := req.Send(); err != nil {
t.Fatal(err)
}
}
func removeHandlers(h request.Handlers, removeSendHandlers bool) request.Handlers {
if removeSendHandlers {
h.Send.Clear()
}
h.Unmarshal.Clear()
h.UnmarshalStream.Clear()
h.UnmarshalMeta.Clear()
h.UnmarshalError.Clear()
h.Validate.Clear()
h.Complete.Clear()
h.ValidateResponse.Clear()
return h
}
func mockSendDescEndpoint(r *request.Request) {
if r.Operation.Name != opDescribeEndpoints {
return
}
out, _ := r.Data.(*DescribeEndpointsOutput)
out.Endpoints = []*Endpoint{
{
Address: aws.String("http://foo"),
CachePeriodInMinutes: aws.Int64(5),
},
}
r.Data = out
}
|