Skip to content

Commit 73b86ec

Browse files
Add test for rate limit exceeded
1 parent ea8afec commit 73b86ec

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import logging
2+
import unittest
3+
from cassandra import OperationType, RateLimitReached
4+
from cassandra.cluster import Cluster
5+
from cassandra.policies import ConstantReconnectionPolicy, RoundRobinPolicy, TokenAwarePolicy
6+
7+
from tests.integration import PROTOCOL_VERSION, use_cluster
8+
9+
LOGGER = logging.getLogger(__name__)
10+
11+
def setup_module():
12+
use_cluster('rate_limit', [3], start=True)
13+
14+
class TestRateLimitExceededException(unittest.TestCase):
15+
@classmethod
16+
def setup_class(cls):
17+
cls.cluster = Cluster(contact_points=["127.0.0.1"], protocol_version=PROTOCOL_VERSION,
18+
load_balancing_policy=TokenAwarePolicy(RoundRobinPolicy()),
19+
reconnection_policy=ConstantReconnectionPolicy(1))
20+
cls.session = cls.cluster.connect()
21+
22+
@classmethod
23+
def teardown_class(cls):
24+
cls.cluster.shutdown()
25+
26+
def test_rate_limit_exceeded(self):
27+
self.session.execute(
28+
"""
29+
DROP KEYSPACE IF EXISTS ratetests
30+
"""
31+
)
32+
self.session.execute(
33+
"""
34+
CREATE KEYSPACE IF NOT EXISTS ratetests
35+
WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}
36+
""")
37+
38+
self.session.execute("USE ratetests")
39+
self.session.execute(
40+
"""
41+
CREATE TABLE tbl (pk int PRIMARY KEY, v int)
42+
WITH per_partition_rate_limit = {'max_writes_per_second': 1}
43+
""")
44+
45+
prepared = self.session.prepare(
46+
"""
47+
INSERT INTO tbl (pk, v) VALUES (?, ?)
48+
""")
49+
50+
# The rate limit is 1 write/s, so repeat the same query
51+
# until an error occurs, it should happen quickly
52+
def execute_write():
53+
for _ in range(1000):
54+
self.session.execute(prepared.bind((123, 456)))
55+
56+
with self.assertRaises(RateLimitReached) as context:
57+
execute_write()
58+
59+
self.assertEqual(context.exception.op_type, OperationType.Write)

tests/unit/test_protocol_features.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
try:
2+
import unittest2 as unittest
3+
except ImportError:
4+
import unittest # noqa
5+
6+
import logging
7+
8+
from cassandra.protocol_features import ProtocolFeatures
9+
10+
LOGGER = logging.getLogger(__name__)
11+
12+
13+
class TestProtocolFeatures(unittest.TestCase):
14+
def test_parsing_rate_limit_error(self):
15+
"""
16+
Testing the parsing of the options command
17+
"""
18+
class OptionsHolder(object):
19+
options = {
20+
'SCYLLA_RATE_LIMIT_ERROR': ["ERROR_CODE=123"]
21+
}
22+
23+
protocol_features = ProtocolFeatures.parse_from_supported(OptionsHolder().options)
24+
25+
self.assertEqual(protocol_features.rate_limit_error, 123)
26+
self.assertEqual(protocol_features.shard_id, 0)
27+
self.assertEqual(protocol_features.sharding_info, None)

0 commit comments

Comments
 (0)