6
6
from pathlib import Path
7
7
8
8
import pytest
9
- from aiokafka .errors import NoBrokersAvailable
9
+ from aiokafka .errors import InvalidSessionTimeoutError
10
+ from confluent_kafka import TopicPartition
10
11
from confluent_kafka .admin import NewTopic
11
12
12
- from karapace .backup .api import BackupVersion , create_backup
13
13
from karapace .core .config import Config
14
14
from karapace .core .kafka .admin import KafkaAdminClient
15
- from karapace .core .kafka_utils import kafka_producer_from_config
15
+
16
+ from src .karapace .backup .api import _consume_records
17
+ from src .karapace .backup .poll_timeout import PollTimeout
18
+ from src .karapace .core .kafka_utils import kafka_producer_from_config , kafka_consumer_from_config
16
19
from tests .integration .conftest import create_kafka_server
17
20
from tests .integration .utils .config import KafkaDescription
18
21
from tests .integration .utils .kafka_server import KafkaServers
19
22
20
23
SESSION_TIMEOUT_MS = 65000
24
+ INVALID_SESSION_TIMEOUT_MS = 5000
21
25
GROUP_MIN_SESSION_TIMEOUT_MS = 60000
22
26
GROUP_MAX_SESSION_TIMEOUT_MS = 70000
23
27
@@ -44,13 +48,13 @@ def fixture_kafka_server(
44
48
)
45
49
46
50
47
- def test_producer_with_custom_kafka_properties_does_not_fail (
51
+ def test_consumer_with_custom_kafka_properties_does_not_fail (
48
52
kafka_server_session_timeout : KafkaServers ,
49
53
new_topic : NewTopic ,
50
54
tmp_path : Path ,
51
55
) -> None :
52
56
"""
53
- This test checks wether the custom properties are accepted by kafka.
57
+ This test checks weather the custom properties are accepted by kafka.
54
58
We know by the implementation of the consumer startup code that if
55
59
`group.session.min.timeout.ms` > `session.timeout.ms` the consumer
56
60
will raise an exception during the startup.
@@ -64,51 +68,60 @@ def test_producer_with_custom_kafka_properties_does_not_fail(
64
68
admin_client = KafkaAdminClient (bootstrap_servers = kafka_server_session_timeout .bootstrap_servers )
65
69
admin_client .new_topic (new_topic .topic , num_partitions = 1 , replication_factor = 1 )
66
70
67
- with kafka_producer_from_config (config ) as producer :
68
- producer .send (
69
- new_topic .topic ,
70
- key = b"foo" ,
71
- value = b"bar" ,
72
- partition = 0 ,
73
- headers = [
74
- ("some-header" , b"some header value" ),
75
- ("other-header" , b"some other header value" ),
76
- ],
77
- timestamp = 1683474657 ,
78
- )
79
- producer .flush ()
80
-
81
- # without performing the backup the exception isn't raised.
82
- create_backup (
83
- config = config ,
84
- backup_location = tmp_path / "backup" ,
85
- topic_name = new_topic .topic ,
86
- version = BackupVersion .V3 ,
87
- replication_factor = 1 ,
88
- )
71
+ produce_consume_messages (config , new_topic .topic , False )
89
72
90
73
91
- def test_producer_with_custom_kafka_properties_fail (
74
+ def test_consumer_with_custom_kafka_properties_fail (
92
75
kafka_server_session_timeout : KafkaServers ,
93
76
new_topic : NewTopic ,
77
+ tmp_path : Path ,
94
78
) -> None :
95
79
"""
96
- This test checks wether the custom properties are accepted by kafka.
80
+ This test checks weather the custom properties are accepted by kafka.
97
81
We know by the implementation of the consumer startup code that if
98
82
`group.session.min.timeout.ms` > `session.timeout.ms` the consumer
99
83
will raise an exception during the startup.
100
84
This test ensures that the `session.timeout.ms` can be injected in
101
- the kafka config so that the exception isn't raised
85
+ the kafka config so that the exception is raised
102
86
"""
103
87
admin_client = KafkaAdminClient (bootstrap_servers = kafka_server_session_timeout .bootstrap_servers )
104
88
admin_client .new_topic (new_topic .topic , num_partitions = 1 , replication_factor = 1 )
105
89
106
90
config = Config ()
107
- # TODO: This test is broken. Test has used localhost:9092 when this should use
108
91
# the configured broker from kafka_server_session.
109
- # config.bootstrap_uri = kafka_server_session_timeout.bootstrap_servers[0]
110
- config .bootstrap_uri = "localhost:9092"
92
+ config .bootstrap_uri = kafka_server_session_timeout .bootstrap_servers [0 ]
93
+ # configure session timeout less than min session time
94
+ config .session_timeout_ms = INVALID_SESSION_TIMEOUT_MS
95
+
96
+ produce_consume_messages (config , new_topic .topic , True )
97
+
98
+
99
+ def produce_consume_messages (config : Config , new_topic : str , invalid_config : bool ):
100
+ with kafka_producer_from_config (config ) as producer :
101
+ producer .send (
102
+ new_topic ,
103
+ key = b"foo" ,
104
+ value = b"bar" ,
105
+ partition = 0 ,
106
+ headers = [
107
+ ("some-header" , b"some header value" ),
108
+ ("other-header" , b"some other header value" ),
109
+ ],
110
+ timestamp = 1683474657 ,
111
+ )
112
+ if invalid_config :
113
+ with pytest .raises (InvalidSessionTimeoutError ):
114
+ consume_messages (config , new_topic )
115
+ else :
116
+ consume_messages (config , new_topic )
117
+
111
118
112
- with pytest .raises (NoBrokersAvailable ):
113
- with kafka_producer_from_config (config ) as producer :
114
- _ = producer
119
+ def consume_messages (config , new_topic ):
120
+ with kafka_consumer_from_config (config , new_topic ) as consumer :
121
+ (partition ,) = consumer .partitions_for_topic (new_topic )
122
+ for _ in _consume_records (
123
+ consumer = consumer ,
124
+ topic_partition = TopicPartition (new_topic , partition ),
125
+ poll_timeout = PollTimeout .default (),
126
+ ):
127
+ pass
0 commit comments