Skip to content

Commit 1569334

Browse files
authored
Add ProducerBatch.__lt__ for heapq (#2698)
1 parent 5904e5d commit 1569334

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

kafka/producer/producer_batch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,6 @@ def __str__(self):
180180
return 'ProducerBatch(topic_partition=%s, record_count=%d)' % (
181181
self.topic_partition, self.records.next_offset())
182182

183-
184-
183+
# for heapq
184+
def __lt__(self, other):
185+
return self.created < other.created

test/test_producer_batch.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,18 @@ def test_complete_exceptionally_with_null_record_errors(batch):
134134

135135
with pytest.raises(AssertionError):
136136
_test_complete_exceptionally(batch, record_count, top_level_exception, None)
137+
138+
139+
def test_producer_batch_lt(tp, memory_records_builder):
140+
b1 = ProducerBatch(tp, memory_records_builder, now=1)
141+
b2 = ProducerBatch(tp, memory_records_builder, now=2)
142+
143+
assert b1 < b2
144+
assert not b1 < b1
145+
146+
import heapq
147+
q = []
148+
heapq.heappush(q, b2)
149+
heapq.heappush(q, b1)
150+
assert q[0] == b1
151+
assert q[1] == b2

0 commit comments

Comments
 (0)