Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add tests for AWS.ResourceWaiter

  • Loading branch information...
commit e11769aec4da2ae26a5eaf321694c80983048477 1 parent 87be5dc
@lsegal lsegal authored
Showing with 102 additions and 0 deletions.
  1. +102 −0 test/resource_waiter.spec.coffee
View
102 test/resource_waiter.spec.coffee
@@ -0,0 +1,102 @@
+# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"). You
+# may not use this file except in compliance with the License. A copy of
+# the License is located at
+#
+# http://aws.amazon.com/apache2.0/
+#
+# or in the "license" file accompanying this file. This file is
+# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
+# ANY KIND, either express or implied. See the License for the specific
+# language governing permissions and limitations under the License.
+
+helpers = require('./helpers')
+AWS = helpers.AWS
+
+describe 'AWS.ResourceWaiter', ->
+ describe 'wait', ->
+ it 'waits until a given state is met', ->
+ err = null; data = null; resp = null
+ db = new AWS.DynamoDB
+ helpers.mockResponses db, [
+ {data: {Table: TableStatus: 'LOADING'}},
+ {data: {Table: TableStatus: 'LOADING'}},
+ {data: {Table: TableStatus: 'ACTIVE'}}
+ ]
+
+ waiter = new AWS.ResourceWaiter(db, 'tableExists')
+ waiter.wait (e, d) -> resp = this; err = e; data = d
+ expect(err).toEqual(null)
+ expect(data.Table.TableStatus).toEqual('ACTIVE')
+ expect(resp.waiterRetryCount).toEqual(2)
+
+ it 'can override the final state', ->
+ err = null; data = null; resp = null
+ db = new AWS.DynamoDB
+ helpers.mockResponses db, [
+ {data: {Table: TableStatus: 'LOADING'}},
+ {data: {Table: TableStatus: 'LOADING'}},
+ {data: {Table: TableStatus: 'ACTIVE'}},
+ {data: {Table: TableStatus: 'FOO'}}
+ ]
+
+ waiter = new AWS.ResourceWaiter(db, {tableExists: 'FOO'})
+ waiter.wait (e, d) -> resp = this; err = e; data = d
+ expect(err).toEqual(null)
+ expect(data.Table.TableStatus).toEqual('FOO')
+ expect(resp.waiterRetryCount).toEqual(3)
+
+ it 'throws an error if terminal state is not configured', ->
+ try
+ new AWS.ResourceWaiter(new AWS.DynamoDB, 'invalidState')
+ catch e
+ err = e
+ expect(err.message).toEqual('State invalidState not found.')
+
+ it 'gives up after a maximum number of retries', ->
+ err = null; data = null; resp = null
+ db = new AWS.DynamoDB
+ resps = ({data: {Table: {TableStatus: 'LOADING'}}} for _ in [1..26])
+ resps.push({data: {Table: {TableStatus: 'ACTIVE'}}})
+ helpers.mockResponses db, resps
+
+ waiter = new AWS.ResourceWaiter(db, 'tableExists')
+ waiter.wait (e, d) -> resp = this; err = e; data = d
+ expect(data).toEqual(null)
+ expect(err.code).toEqual('ResourceNotReady')
+ expect(resp.waiterRetryCount).toEqual(25) # 25 max retries
+ expect(resp.request.retryDelay(resp)).toEqual(20000) # 20s delay
+
+ it 'accepts error state as a terminal state', ->
+ err = null; data = null; resp = null
+ s3 = new AWS.S3
+ helpers.mockResponses s3, [
+ {httpResponse: {statusCode: 200}},
+ {httpResponse: {statusCode: 200}},
+ {httpResponse: {statusCode: 404}}
+ ]
+
+ waiter = new AWS.ResourceWaiter(s3, 'bucketNotExists')
+ waiter.wait Bucket: 'bucket', (e, d) -> resp = this; err = e; data = d
+ expect(err).toEqual(null)
+ expect(resp.httpResponse.statusCode).toEqual(404)
+ expect(resp.waiterRetryCount).toEqual(2)
+
+ it 'fails fast if failure value is found', ->
+ err = null; data = null; resp = null
+ ec2 = new AWS.EC2
+ helpers.mockResponses ec2, [
+ {data: {Reservations: [{Instances: [{State: {Name: 'pending'}}]}]}},
+ {data: {Reservations: [{Instances: [{State: {Name: 'pending'}}]}]}},
+ {data: {Reservations: [{Instances: [{State: {Name: 'pending'}}]}]}},
+ {data: {Reservations: [{Instances: [{State: {Name: 'terminated'}}]}]}},
+ {data: {Reservations: [{Instances: [{State: {Name: 'pending'}}]}]}},
+ {data: {Reservations: [{Instances: [{State: {Name: 'running'}}]}]}}
+ ]
+
+ waiter = new AWS.ResourceWaiter(ec2, 'instanceRunning')
+ waiter.wait InstanceIds: ['id-123456'], (e, d) -> resp = this; err = e; data = d
+ expect(data).toEqual(null)
+ expect(err.code).toEqual('ResourceNotReady')
+ expect(resp.waiterRetryCount).toEqual(3)
Please sign in to comment.
Something went wrong with that request. Please try again.