| OLD | NEW |
| 1 # Copyright (c) 2013 Amazon.com, Inc. or its affiliates. | 1 # Copyright (c) 2013 Amazon.com, Inc. or its affiliates. |
| 2 # All rights reserved. | 2 # All rights reserved. |
| 3 # | 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- | 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit | 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: | 10 # lowing conditions: |
| 11 # | 11 # |
| 12 # The above copyright notice and this permission notice shall be included | 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. | 13 # in all copies or substantial portions of the Software. |
| 14 # | 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. | 21 # IN THE SOFTWARE. |
| 22 | 22 |
| 23 import time |
| 24 |
| 23 import boto | 25 import boto |
| 24 import time | 26 from boto.kinesis.exceptions import ResourceNotFoundException |
| 25 | 27 |
| 26 from unittest import TestCase | 28 from unittest import TestCase |
| 27 | 29 |
| 28 | 30 |
| 29 class TimeoutError(Exception): | 31 class TimeoutError(Exception): |
| 30 pass | 32 pass |
| 31 | 33 |
| 32 | 34 |
| 33 class TestKinesis(TestCase): | 35 class TestKinesis(TestCase): |
| 34 def setUp(self): | 36 def setUp(self): |
| 35 self.kinesis = boto.connect_kinesis() | 37 self.kinesis = boto.connect_kinesis() |
| 36 | 38 |
| 37 def tearDown(self): | |
| 38 # Delete the stream even if there is a failure | |
| 39 self.kinesis.delete_stream('test') | |
| 40 | |
| 41 def test_kinesis(self): | 39 def test_kinesis(self): |
| 42 kinesis = self.kinesis | 40 kinesis = self.kinesis |
| 43 | 41 |
| 44 # Create a new stream | 42 # Create a new stream |
| 45 kinesis.create_stream('test', 1) | 43 kinesis.create_stream('test', 1) |
| 44 self.addCleanup(self.kinesis.delete_stream, 'test') |
| 46 | 45 |
| 47 # Wait for the stream to be ready | 46 # Wait for the stream to be ready |
| 48 tries = 0 | 47 tries = 0 |
| 49 while tries < 10: | 48 while tries < 10: |
| 50 tries += 1 | 49 tries += 1 |
| 51 time.sleep(15) | 50 time.sleep(15) |
| 52 response = kinesis.describe_stream('test') | 51 response = kinesis.describe_stream('test') |
| 53 | 52 |
| 54 if response['StreamDescription']['StreamStatus'] == 'ACTIVE': | 53 if response['StreamDescription']['StreamStatus'] == 'ACTIVE': |
| 55 shard_id = response['StreamDescription']['Shards'][0]['ShardId'] | 54 shard_id = response['StreamDescription']['Shards'][0]['ShardId'] |
| 56 break | 55 break |
| 57 else: | 56 else: |
| 58 raise TimeoutError('Stream is still not active, aborting...') | 57 raise TimeoutError('Stream is still not active, aborting...') |
| 59 | 58 |
| 60 # Get ready to process some data from the stream | 59 # Get ready to process some data from the stream |
| 61 response = kinesis.get_shard_iterator('test', shard_id, 'TRIM_HORIZON') | 60 response = kinesis.get_shard_iterator('test', shard_id, 'TRIM_HORIZON') |
| 62 shard_iterator = response['ShardIterator'] | 61 shard_iterator = response['ShardIterator'] |
| 63 | 62 |
| 64 # Write some data to the stream | 63 # Write some data to the stream |
| 65 data = 'Some data ...' | 64 data = 'Some data ...' |
| 66 response = kinesis.put_record('test', data, data) | 65 response = kinesis.put_record('test', data, data) |
| 67 | 66 |
| 68 # Wait for the data to show up | 67 # Wait for the data to show up |
| 69 tries = 0 | 68 tries = 0 |
| 70 while tries < 100: | 69 while tries < 100: |
| 71 tries += 1 | 70 tries += 1 |
| 72 time.sleep(1) | 71 time.sleep(1) |
| 73 | 72 |
| 74 response = kinesis.get_records(shard_iterator) | 73 response = kinesis.get_records(shard_iterator) |
| 75 shard_iterator = response['NextShardIterator'] | 74 shard_iterator = response['NextShardIterator'] |
| 76 | 75 |
| 77 if len(response['Records']): | 76 if len(response['Records']): |
| 78 break | 77 break |
| 79 else: | 78 else: |
| 80 raise TimeoutError('No records found, aborting...') | 79 raise TimeoutError('No records found, aborting...') |
| 81 | 80 |
| 82 # Read the data, which should be the same as what we wrote | 81 # Read the data, which should be the same as what we wrote |
| 83 self.assertEqual(1, len(response['Records'])) | 82 self.assertEqual(1, len(response['Records'])) |
| 84 self.assertEqual(data, response['Records'][0]['Data']) | 83 self.assertEqual(data, response['Records'][0]['Data']) |
| 84 |
| 85 def test_describe_non_existent_stream(self): |
| 86 with self.assertRaises(ResourceNotFoundException) as cm: |
| 87 self.kinesis.describe_stream('this-stream-shouldnt-exist') |
| 88 |
| 89 # Assert things about the data we passed along. |
| 90 self.assertEqual(cm.exception.error_code, None) |
| 91 self.assertTrue('not found' in cm.exception.message) |
| OLD | NEW |