|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | + |
| 21 | +from unittest import TestCase, main |
| 22 | +import time |
| 23 | + |
| 24 | +from pulsar import Client, MessageId |
| 25 | + |
| 26 | +class ReaderTest(TestCase): |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + self._client: Client = Client('pulsar://localhost:6650') |
| 30 | + |
| 31 | + def tearDown(self) -> None: |
| 32 | + self._client.close() |
| 33 | + |
| 34 | + def test_has_message_available_after_seek(self): |
| 35 | + topic = f'test_has_message_available_after_seek-{time.time()}' |
| 36 | + producer = self._client.create_producer(topic) |
| 37 | + reader = self._client.create_reader(topic, start_message_id=MessageId.earliest) |
| 38 | + |
| 39 | + producer.send('msg-0'.encode()) |
| 40 | + self.assertTrue(reader.has_message_available()) |
| 41 | + |
| 42 | + reader.seek(MessageId.latest) |
| 43 | + self.assertFalse(reader.has_message_available()) |
| 44 | + |
| 45 | + producer.send('msg-1'.encode()) |
| 46 | + self.assertTrue(reader.has_message_available()) |
| 47 | + |
| 48 | + def test_seek_latest_message_id(self): |
| 49 | + topic = f'test_seek_latest_message_id-{time.time()}' |
| 50 | + producer = self._client.create_producer(topic) |
| 51 | + msg_id = producer.send('msg'.encode()) |
| 52 | + |
| 53 | + reader = self._client.create_reader(topic, |
| 54 | + start_message_id=MessageId.latest) |
| 55 | + self.assertFalse(reader.has_message_available()) |
| 56 | + reader.close() |
| 57 | + |
| 58 | + reader = self._client.create_reader(topic, |
| 59 | + start_message_id=MessageId.latest, |
| 60 | + start_message_id_inclusive=True) |
| 61 | + self.assertTrue(reader.has_message_available()) |
| 62 | + msg = reader.read_next(3000) |
| 63 | + self.assertEqual(msg.message_id(), msg_id) |
| 64 | + reader.close() |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + main() |
0 commit comments