diff --git a/examples/examples_nodejs_test.go b/examples/examples_nodejs_test.go index a112f558..faa29c11 100644 --- a/examples/examples_nodejs_test.go +++ b/examples/examples_nodejs_test.go @@ -15,20 +15,13 @@ package examples import ( - "context" "fmt" - "log" - "os" "path/filepath" "testing" "time" - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/config" - "github.com/aws/aws-sdk-go-v2/service/dynamodb" "github.com/gorilla/websocket" "github.com/pulumi/pulumi/pkg/v3/testing/integration" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -117,8 +110,7 @@ func TestAPIWebsocketLambdaDynamoDB(t *testing.T) { t.Helper() t.Logf("Outputs: %v", stack.Outputs) url := stack.Outputs["url"].(string) - table := stack.Outputs["table"].(string) - websocketValidation(t, url, table) + websocketValidation(t, url) }, }) @@ -156,36 +148,6 @@ func retryFunc(t *testing.T, fn func() bool) error { } } -// isConnected validates that the $connect lambda was triggered successfully by -// ensuring there is a record in the dynamodb table -// It shouldn't be necessary to retry this, but my testing was very inconsistent, sometimes the `Dial` -// triggered the connect and sometimes it didn't (according to the docs it shouldn't be possible to establish -// a connection without triggering $connect) -func isConnected(ctx context.Context, t *testing.T, table string, c *websocket.Conn, client *dynamodb.Client, canRetry bool) error { - err := retryFunc(t, func() bool { - // When you establish the connection the `onconnect` lambda is triggered which writes - // an item to the table - res, err := client.Scan(ctx, &dynamodb.ScanInput{ - ProjectionExpression: aws.String("connectionId"), - TableName: &table, - }) - require.NoError(t, err) - if assert.Equal(t, 1, len(res.Items)) { - return true - } - return false - }) - if err != nil { - if canRetry { - err = c.WriteMessage(websocket.TextMessage, []byte(`{"action":"$connect","data":"hello world"}`)) - require.NoError(t, err) - return isConnected(ctx, t, table, c, client, false) - } - return err - } - return nil -} - // websocketValidation validates that the websocket lambda apigateway test is setup and you can: // 1. Open a connection // 2. The $connect route triggers the lambda function which writes to the dynamodb table @@ -193,18 +155,15 @@ func isConnected(ctx context.Context, t *testing.T, table string, c *websocket.C // // $disconnect is best effort so is not guaranteed to be sent to Lambda, otherwise // we would also assert that the item is removed -func websocketValidation(t *testing.T, url, table string) { +func websocketValidation(t *testing.T, url string) { t.Helper() - ctx := context.Background() t.Logf("URL: %s", url) - cfg, err := config.LoadDefaultConfig(ctx) - client := dynamodb.NewFromConfig(cfg) var c *websocket.Conn // Sometimes it can take a while for the connection to be successfully established // bad handshake errors can occur, but go away after some retrying - err = retryFunc(t, func() bool { + err := retryFunc(t, func() bool { conn, _, err := websocket.DefaultDialer.Dial(url, nil) if err == nil { c = conn @@ -215,13 +174,4 @@ func websocketValidation(t *testing.T, url, table string) { require.NoErrorf(t, err, "Failed to connect to websocket") defer c.Close() - - err = isConnected(ctx, t, table, c, client, true) - require.NoErrorf(t, err, "never found connection in dynamodb table") - - // writing a message with `sendmessage` action will trigger the `sendmessage` lambda - // the lambda writes a message back on the connection - log.New(os.Stderr, "", 0).Println("Sending message...") - err = c.WriteMessage(websocket.TextMessage, []byte(`{"action":"sendmessage","data":"hello world"}`)) - require.NoError(t, err) } diff --git a/examples/go.mod b/examples/go.mod index 97263cb7..c97b0029 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -5,9 +5,6 @@ go 1.21 toolchain go1.22.5 require ( - github.com/aws/aws-sdk-go-v2 v1.30.4 - github.com/aws/aws-sdk-go-v2/config v1.15.15 - github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.5 github.com/gorilla/websocket v1.5.3 github.com/pulumi/pulumi/pkg/v3 v3.99.0 github.com/stretchr/testify v1.8.4 @@ -44,13 +41,14 @@ require ( github.com/armon/go-radix v1.0.0 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aws/aws-sdk-go v1.44.298 // indirect + github.com/aws/aws-sdk-go-v2 v1.30.4 // indirect + github.com/aws/aws-sdk-go-v2/config v1.15.15 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.12.10 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.9 // indirect github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.3.16 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect - github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.17 // indirect github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.9 // indirect github.com/aws/aws-sdk-go-v2/service/kms v1.18.1 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.11.13 // indirect diff --git a/examples/go.sum b/examples/go.sum index fe81f1bd..28bc9426 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -274,8 +274,6 @@ github.com/aws/aws-sdk-go-v2/internal/ini v1.3.16 h1:f0ySVcmQhwmzn7zQozd8wBM3yuG github.com/aws/aws-sdk-go-v2/internal/ini v1.3.16/go.mod h1:CYmI+7x03jjJih8kBEEFKRQc40UjUokT0k7GbvrhhTc= github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.6 h1:3L8pcjvgaSOs0zzZcMKzxDSkYKEpwJ2dNVDdxm68jAY= github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.6/go.mod h1:O7Oc4peGZDEKlddivslfYFvAbgzvl/GH3J8j3JIGBXc= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.5 h1:Cm77yt+/CV7A6DglkENsWA3H1hq8+4ItJnFKrhxHkvg= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.5/go.mod h1:s2fYaueBuCnwv1XQn6T8TfShxJWusv5tWPMcL+GY6+g= github.com/aws/aws-sdk-go-v2/service/iam v1.19.0 h1:9vCynoqC+dgxZKrsjvAniyIopsv3RZFsZ6wkQ+yxtj8= github.com/aws/aws-sdk-go-v2/service/iam v1.19.0/go.mod h1:OyAuvpFeSVNppcSsp1hFOVQcaTRc1LE24YIR7pMbbAA= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.3/go.mod h1:gkb2qADY+OHaGLKNTYxMaQNacfeyQpZ4csDTQMeFmcw= @@ -283,8 +281,6 @@ github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 h1:KypMCbL github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4/go.mod h1:Vz1JQXliGcQktFTN/LN6uGppAIRoLBR2bMvIMP0gOjc= github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.10 h1:7LJcuRalaLw+GYQTMGmVUl4opg2HrDZkvn/L3KvIQfw= github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.10/go.mod h1:Qks+dxK3O+Z2deAhNo6cJ8ls1bam3tUGUAcgxQP1c70= -github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.17 h1:HDJGz1jlV7RokVgTPfx1UHBHANC0N5Uk++xgyYgz5E0= -github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.17/go.mod h1:5szDu6TWdRDytfDxUQVv2OYfpTQMKApVFyqpm+TcA98= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.9 h1:sHfDuhbOuuWSIAEDd3pma6p0JgUcR2iePxtCE8gfCxQ= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.9/go.mod h1:yQowTpvdZkFVuHrLBXmczat4W+WJKg/PafBZnGBLga0= github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.9 h1:sJdKvydGYDML9LTFcp6qq6Z5fIjN0Rdq2Gvw1hUg8tc=