|
1 | 1 | from string import ascii_lowercase |
| 2 | +import time |
2 | 3 |
|
3 | | -from fluvio import ConsumerConfig, Fluvio, Offset |
| 4 | +from fluvio import ConsumerConfig, Fluvio, Offset, OffsetManagementStrategy |
4 | 5 | from fluvio import ConsumerConfigExtBuilder |
5 | 6 | from test_base import CommonFluvioSetup |
6 | 7 |
|
@@ -59,6 +60,140 @@ def test_consume_at_offset_begin(self): |
59 | 60 | self.assertEqual(records[0], "record-z") |
60 | 61 | self.assertEqual(records[1], "record-y") |
61 | 62 |
|
| 63 | + def test_consume_offset_managed_auto_manual(self): |
| 64 | + """ |
| 65 | + Manual offset management test |
| 66 | + """ |
| 67 | + fluvio = Fluvio.connect() |
| 68 | + |
| 69 | + builder = ConsumerConfigExtBuilder(self.topic) |
| 70 | + builder.offset_start(Offset.beginning()) |
| 71 | + builder.offset_strategy(OffsetManagementStrategy.MANUAL) |
| 72 | + builder.offset_consumer(self.consumer_id) |
| 73 | + |
| 74 | + config = builder.build() |
| 75 | + stream = fluvio.consumer_with_config(config) |
| 76 | + |
| 77 | + num_items = 2 |
| 78 | + records = [bytearray(next(stream).value()).decode() for _ in range(num_items)] |
| 79 | + |
| 80 | + time.sleep(1) |
| 81 | + |
| 82 | + stream.offset_commit() |
| 83 | + stream.offset_flush() |
| 84 | + |
| 85 | + self.assertEqual(len(records), 2) |
| 86 | + self.assertEqual(records[0], "record-z") |
| 87 | + self.assertEqual(records[1], "record-y") |
| 88 | + |
| 89 | + consumers = fluvio.consumer_offsets() |
| 90 | + self.assertEqual(len(consumers), 1) |
| 91 | + self.assertEqual(consumers[0].consumer_id, self.consumer_id) |
| 92 | + self.assertEqual(consumers[0].topic, self.topic) |
| 93 | + self.assertEqual(consumers[0].partition, 0) |
| 94 | + self.assertEqual(consumers[0].offset, 1) |
| 95 | + |
| 96 | + def test_consume_offset_managed_auto_commit_and_flush(self): |
| 97 | + """ |
| 98 | + Automatic offset management test |
| 99 | + """ |
| 100 | + fluvio = Fluvio.connect() |
| 101 | + |
| 102 | + builder = ConsumerConfigExtBuilder(self.topic) |
| 103 | + builder.offset_start(Offset.beginning()) |
| 104 | + builder.offset_strategy(OffsetManagementStrategy.AUTO) |
| 105 | + builder.offset_consumer(self.consumer_id) |
| 106 | + |
| 107 | + config = builder.build() |
| 108 | + stream = fluvio.consumer_with_config(config) |
| 109 | + |
| 110 | + num_items = 2 |
| 111 | + records = [bytearray(next(stream).value()).decode() for _ in range(num_items)] |
| 112 | + |
| 113 | + stream.offset_commit() |
| 114 | + stream.offset_flush() |
| 115 | + |
| 116 | + self.assertEqual(len(records), 2) |
| 117 | + self.assertEqual(records[0], "record-z") |
| 118 | + self.assertEqual(records[1], "record-y") |
| 119 | + |
| 120 | + consumers = fluvio.consumer_offsets() |
| 121 | + self.assertEqual(len(consumers), 1) |
| 122 | + self.assertEqual(consumers[0].consumer_id, self.consumer_id) |
| 123 | + self.assertEqual(consumers[0].topic, self.topic) |
| 124 | + self.assertEqual(consumers[0].partition, 0) |
| 125 | + self.assertEqual(consumers[0].offset, 1) |
| 126 | + |
| 127 | + def test_consume_offset_managed_auto_flush(self): |
| 128 | + """ |
| 129 | + Consume with an offset_consume id defined |
| 130 | +
|
| 131 | + This will case the cluster to track that offsets |
| 132 | + that consumer has consumed, stoping and resuming |
| 133 | + from last offset |
| 134 | +
|
| 135 | + The equivalent of this test with the CLI is: |
| 136 | + `fluvio consume test-topic --offset-beginning --offset-consumer --end 1 foo` |
| 137 | + record-z |
| 138 | +
|
| 139 | +
|
| 140 | + `fluvio consume test-topic --offset-beginning --offset-consumer --end 2 foo` |
| 141 | + record-y |
| 142 | +
|
| 143 | + """ |
| 144 | + fluvio = Fluvio.connect() |
| 145 | + |
| 146 | + # configure consumer |
| 147 | + builder = ConsumerConfigExtBuilder(self.topic) |
| 148 | + builder.disable_continuous() |
| 149 | + builder.offset_consumer(self.consumer_id) |
| 150 | + builder.offset_start(Offset.beginning()) |
| 151 | + builder.offset_strategy(OffsetManagementStrategy.AUTO) |
| 152 | + |
| 153 | + config = builder.build() |
| 154 | + stream = fluvio.consumer_with_config(config) |
| 155 | + |
| 156 | + num_items = 2 |
| 157 | + records = [] |
| 158 | + for _ in range(num_items): |
| 159 | + records.append(bytearray(next(stream).value()).decode()) |
| 160 | + self.assertEqual(len(records), 2) |
| 161 | + self.assertEqual(records[0], "record-z") |
| 162 | + # ensure offset is flushed |
| 163 | + # with Auto, this normally happens with advancement of the stream and/or |
| 164 | + # closure of the client |
| 165 | + # but that isn't at a guaranteed point for python in the middle |
| 166 | + # of this test, so do this explictly |
| 167 | + stream.offset_flush() |
| 168 | + # connect again with a new client, but the same id |
| 169 | + fluvio = Fluvio.connect() |
| 170 | + |
| 171 | + # configure consumer |
| 172 | + # even though offset_start says to start at the beginning |
| 173 | + # it is expected start from the last offset consumed |
| 174 | + # because the offset_consumer is set |
| 175 | + builder = ConsumerConfigExtBuilder(self.topic) |
| 176 | + builder.disable_continuous() |
| 177 | + builder.offset_consumer(self.consumer_id) |
| 178 | + builder.offset_start(Offset.beginning()) |
| 179 | + builder.offset_strategy(OffsetManagementStrategy.AUTO) |
| 180 | + |
| 181 | + stream = fluvio.consumer_with_config(config) |
| 182 | + |
| 183 | + num_items = 1 |
| 184 | + records = [] |
| 185 | + for _ in range(1): |
| 186 | + records.append(bytearray(next(stream).value()).decode()) |
| 187 | + self.assertEqual(len(records), 1) |
| 188 | + self.assertEqual(records[0], "record-y") |
| 189 | + |
| 190 | + consumers = fluvio.consumer_offsets() |
| 191 | + self.assertEqual(len(consumers), 1) |
| 192 | + self.assertEqual(consumers[0].consumer_id, self.consumer_id) |
| 193 | + self.assertEqual(consumers[0].topic, self.topic) |
| 194 | + self.assertEqual(consumers[0].partition, 0) |
| 195 | + self.assertEqual(consumers[0].offset, 1) |
| 196 | + |
62 | 197 | def test_consume_at_offset_from_begin(self): |
63 | 198 | fluvio = Fluvio.connect() |
64 | 199 |
|
@@ -146,7 +281,7 @@ def test_consume_deprecated(self): |
146 | 281 |
|
147 | 282 | config = ConsumerConfig() |
148 | 283 | stream = consumer.stream_with_config(Offset.beginning(), config) |
149 | | - for i in range(2): |
| 284 | + for _ in range(2): |
150 | 285 | records.append(bytearray(next(stream).value()).decode()) |
151 | 286 |
|
152 | 287 | self.assertEqual(len(records), 2) |
|
0 commit comments