From fa3fd729d13ccbfe3934d500436139ac41d5d7fd Mon Sep 17 00:00:00 2001 From: stephane soulier Date: Fri, 8 Mar 2019 16:27:24 +0100 Subject: [PATCH 1/2] fix bug in Stream init (wrong number of shards) --- moto/kinesis/models.py | 14 ++++---------- tests/test_kinesis/test_kinesis.py | 4 ++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/moto/kinesis/models.py b/moto/kinesis/models.py index d9a47ea87..21773c5a1 100644 --- a/moto/kinesis/models.py +++ b/moto/kinesis/models.py @@ -121,17 +121,11 @@ class Stream(BaseModel): self.shards = {} self.tags = {} - if six.PY3: - izip_longest = itertools.zip_longest - else: - izip_longest = itertools.izip_longest + step = 2**128 // shard_count + for index, start, end in itertools.chain( + map(lambda i: (i, i*step, (i+1) * step), range(shard_count - 1)), + [(shard_count - 1, (shard_count -1) * step, 2**128)]): - for index, start, end in izip_longest(range(shard_count), - range(0, 2**128, 2 ** - 128 // shard_count), - range(2**128 // shard_count, 2 ** - 128, 2**128 // shard_count), - fillvalue=2**128): shard = Shard(index, start, end) self.shards[shard.shard_id] = shard diff --git a/tests/test_kinesis/test_kinesis.py b/tests/test_kinesis/test_kinesis.py index c70236978..aba8e8916 100644 --- a/tests/test_kinesis/test_kinesis.py +++ b/tests/test_kinesis/test_kinesis.py @@ -14,7 +14,7 @@ from moto import mock_kinesis, mock_kinesis_deprecated def test_create_cluster(): conn = boto.kinesis.connect_to_region("us-west-2") - conn.create_stream("my_stream", 2) + conn.create_stream("my_stream", 3) stream_response = conn.describe_stream("my_stream") @@ -26,7 +26,7 @@ def test_create_cluster(): stream["StreamStatus"].should.equal("ACTIVE") shards = stream['Shards'] - shards.should.have.length_of(2) + shards.should.have.length_of(3) @mock_kinesis_deprecated From 92ca7aee12303496c610de677fe550b77c5245e3 Mon Sep 17 00:00:00 2001 From: stephane soulier Date: Fri, 8 Mar 2019 17:03:56 +0100 Subject: [PATCH 2/2] comply with coding style --- moto/kinesis/models.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/moto/kinesis/models.py b/moto/kinesis/models.py index 21773c5a1..57bc0a103 100644 --- a/moto/kinesis/models.py +++ b/moto/kinesis/models.py @@ -122,9 +122,10 @@ class Stream(BaseModel): self.tags = {} step = 2**128 // shard_count - for index, start, end in itertools.chain( - map(lambda i: (i, i*step, (i+1) * step), range(shard_count - 1)), - [(shard_count - 1, (shard_count -1) * step, 2**128)]): + hash_ranges = itertools.chain(map(lambda i: (i, i * step, (i + 1) * step), + range(shard_count - 1)), + [(shard_count - 1, (shard_count - 1) * step, 2**128)]) + for index, start, end in hash_ranges: shard = Shard(index, start, end) self.shards[shard.shard_id] = shard