Skip to content

Commit

Permalink
wsclient: Add new test for context cancellation in client.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richa Gangwar committed May 18, 2023
1 parent d68e815 commit 8cfa5a0
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions ecs-agent/wsclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestClientProxy(t *testing.T) {
defer os.Unsetenv("HTTP_PROXY")

types := []interface{}{ecsacs.AckRequest{}}
cs := getTestClientServer("http://www.amazon.com", types)
cs := getTestClientServer("http://www.amazon.com", types, 1)
err := cs.Connect()
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), proxy_url), "proxy not found: %s", err.Error())
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestConcurrentWritesDontPanic(t *testing.T) {
req := ecsacs.AckRequest{Cluster: aws.String("test"), ContainerInstance: aws.String("test"), MessageId: aws.String("test")}

types := []interface{}{ecsacs.AckRequest{}}
cs := getTestClientServer(mockServer.URL, types)
cs := getTestClientServer(mockServer.URL, types, 1)
require.NoError(t, cs.Connect())

executeTenRequests := func() {
Expand All @@ -104,7 +104,7 @@ func TestConcurrentWritesDontPanic(t *testing.T) {
waitForRequests.Wait()
}

func getTestClientServer(url string, msgType []interface{}) *ClientServerImpl {
func getTestClientServer(url string, msgType []interface{}, rwTimeout time.Duration) *ClientServerImpl {
testCreds := credentials.NewStaticCredentials("test-id", "test-secret", "test-token")

return &ClientServerImpl{
Expand All @@ -117,7 +117,7 @@ func getTestClientServer(url string, msgType []interface{}) *ClientServerImpl {
},
CredentialProvider: testCreds,
TypeDecoder: BuildTypeDecoder(msgType),
RWTimeout: time.Second,
RWTimeout: rwTimeout * time.Second,
RequestHandlers: make(map[string]RequestHandler),
}
}
Expand All @@ -135,7 +135,7 @@ func TestProxyVariableCustomValue(t *testing.T) {
testString := "Custom no proxy string"
os.Setenv("NO_PROXY", testString)
types := []interface{}{ecsacs.AckRequest{}}
require.NoError(t, getTestClientServer(mockServer.URL, types).Connect())
require.NoError(t, getTestClientServer(mockServer.URL, types, 1).Connect())

assert.Equal(t, os.Getenv("NO_PROXY"), testString, "NO_PROXY should match user-supplied variable")
}
Expand All @@ -152,7 +152,7 @@ func TestProxyVariableDefaultValue(t *testing.T) {

os.Unsetenv("NO_PROXY")
types := []interface{}{ecsacs.AckRequest{}}
getTestClientServer(mockServer.URL, types).Connect()
getTestClientServer(mockServer.URL, types, 1).Connect()

expectedEnvVar := "169.254.169.254,169.254.170.2," + dockerEndpoint

Expand All @@ -171,7 +171,7 @@ func TestHandleMessagePermissibleCloseCode(t *testing.T) {
mockServer.StartTLS()

types := []interface{}{ecsacs.AckRequest{}}
cs := getTestClientServer(mockServer.URL, types)
cs := getTestClientServer(mockServer.URL, types, 1)
require.NoError(t, cs.Connect())
assert.True(t, cs.IsReady(), "expected websocket connection to be ready")

Expand All @@ -194,7 +194,7 @@ func TestHandleMessageUnexpectedCloseCode(t *testing.T) {
mockServer.StartTLS()

types := []interface{}{ecsacs.AckRequest{}}
cs := getTestClientServer(mockServer.URL, types)
cs := getTestClientServer(mockServer.URL, types, 1)
require.NoError(t, cs.Connect())
assert.True(t, cs.IsReady(), "expected websocket connection to be ready")

Expand All @@ -218,7 +218,7 @@ func TestHandleNonHTTPSEndpoint(t *testing.T) {
defer mockServer.Close()

types := []interface{}{ecsacs.AckRequest{}}
cs := getTestClientServer(mockServer.URL, types)
cs := getTestClientServer(mockServer.URL, types, 1)
require.NoError(t, cs.Connect())
assert.True(t, cs.IsReady(), "expected websocket connection to be ready")

Expand All @@ -243,7 +243,7 @@ func TestHandleIncorrectURLScheme(t *testing.T) {
mockServerURL.Scheme = "notaparticularlyrealscheme"

types := []interface{}{ecsacs.AckRequest{}}
cs := getTestClientServer(mockServerURL.String(), types)
cs := getTestClientServer(mockServerURL.String(), types, 1)
err := cs.Connect()

assert.Error(t, err, "Expected error for incorrect URL scheme")
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestAddRequestPayloadHandler(t *testing.T) {

types := []interface{}{ecsacs.PayloadMessage{}}
messageError := make(chan error)
cs := getTestClientServer(mockServer.URL, types)
cs := getTestClientServer(mockServer.URL, types, 1)
cs.conn = conn

defer cs.Close()
Expand Down Expand Up @@ -364,7 +364,7 @@ func TestMakeUnrecognizedRequest(t *testing.T) {
mockServer.StartTLS()

types := []interface{}{ecsacs.PayloadMessage{}}
cs := getTestClientServer(mockServer.URL, types)
cs := getTestClientServer(mockServer.URL, types, 1)
cs.conn = conn

defer cs.Close()
Expand All @@ -388,7 +388,7 @@ func TestWriteCloseMessage(t *testing.T) {
mockServer.StartTLS()

types := []interface{}{ecsacs.PayloadMessage{}}
cs := getTestClientServer(mockServer.URL, types)
cs := getTestClientServer(mockServer.URL, types, 1)
cs.Connect()

defer cs.Close()
Expand All @@ -397,3 +397,28 @@ func TestWriteCloseMessage(t *testing.T) {
assert.NoError(t, err)
assert.Error(t, <-errChan)
}

// TestCtxCancel tests if the passed context, on receiving the cancel
// on the created ctx.Done channel, performs the expected behavior of
// closing the connection and returns the ctx error.
func TestCtxCancel(t *testing.T) {
closeWS := make(chan []byte)
defer close(closeWS)

ctx, cancel := context.WithCancel(context.Background())
messageError := make(chan error)
mockServer, _, _, _, _ := utils.GetMockServer(closeWS)
mockServer.StartTLS()

types := []interface{}{ecsacs.AckRequest{}}
cs := getTestClientServer(mockServer.URL, types, 2)
require.NoError(t, cs.Connect())
assert.True(t, cs.IsReady(), "expected websocket connection to be ready")

go func() {
messageError <- cs.ConsumeMessages(ctx)
}()
// Cancel the context.
cancel()
assert.EqualError(t, <-messageError, "context canceled")
}

0 comments on commit 8cfa5a0

Please sign in to comment.