@@ -82,7 +82,8 @@ TEST_F(ArrowFlightCancelTest, EarlyCloseReleasesBackpressuredBuffer) {
8282 EXPECT_TRUE (_dep->ready ());
8383 EXPECT_TRUE (_state.get_query_ctx ()->is_cancelled ());
8484 std::shared_ptr<Block> block;
85- EXPECT_FALSE (_buffer->get_arrow_batch (&block).ok ());
85+ bool eos = false ;
86+ EXPECT_FALSE (_buffer->get_arrow_batch (&block, &eos).ok ());
8687 EXPECT_TRUE (reader->Close ().ok ());
8788}
8889
@@ -108,7 +109,9 @@ TEST_F(ArrowFlightCancelTest, NormalEofIsNotCancellation) {
108109 EXPECT_EQ (batch, nullptr );
109110 EXPECT_TRUE (reader->Close ().ok ());
110111 std::shared_ptr<Block> block;
111- EXPECT_TRUE (_buffer->get_arrow_batch (&block).ok ());
112+ bool eos = false ;
113+ EXPECT_TRUE (_buffer->get_arrow_batch (&block, &eos).ok ());
114+ EXPECT_TRUE (eos);
112115 EXPECT_FALSE (_state.get_query_ctx ()->is_cancelled ());
113116}
114117
@@ -117,7 +120,7 @@ TEST_F(ArrowFlightCancelTest, CancellationInterruptsEmptyBufferWait) {
117120 std::promise<void > checked;
118121 std::atomic<int > checks = 0 ;
119122 auto result = ArrowFlightBatchLocalReader::Create (_statement, [&] {
120- if (checks.fetch_add (1 ) == 1 ) {
123+ if (checks.fetch_add (1 ) == 3 ) {
121124 checked.set_value ();
122125 }
123126 return cancelled.load ();
@@ -141,6 +144,57 @@ TEST_F(ArrowFlightCancelTest, CancellationInterruptsEmptyBufferWait) {
141144 EXPECT_TRUE (_state.get_query_ctx ()->is_cancelled ());
142145}
143146
147+ TEST_F (ArrowFlightCancelTest, EmptyBufferReadReturnsForRetry) {
148+ auto fetch = std::async (std::launch::async, [&] {
149+ auto block = std::make_shared<Block>();
150+ bool eos = true ;
151+ auto status = _buffer->get_arrow_batch (&block, &eos);
152+ EXPECT_EQ (block, nullptr );
153+ EXPECT_FALSE (eos);
154+ return status;
155+ });
156+ const auto ready = fetch.wait_for (std::chrono::seconds (2 ));
157+ // Bound the test even if an empty buffer incorrectly waits until query completion.
158+ if (ready != std::future_status::ready) {
159+ _buffer->cancel (Status::Cancelled (" test cleanup" ));
160+ }
161+ EXPECT_EQ (ready, std::future_status::ready);
162+ EXPECT_TRUE (fetch.get ().ok ());
163+ }
164+
165+ TEST_F (ArrowFlightCancelTest, EmptyWaitsDoNotFinishReader) {
166+ std::promise<void > retried;
167+ std::atomic<int > checks = 0 ;
168+ auto result = ArrowFlightBatchLocalReader::Create (_statement, [&] {
169+ if (checks.fetch_add (1 ) == 3 ) {
170+ retried.set_value ();
171+ }
172+ return false ;
173+ });
174+ ASSERT_TRUE (result.ok ()) << result.status ();
175+ auto reader = *result;
176+ std::shared_ptr<arrow::RecordBatch> batch;
177+ auto fetch = std::async (std::launch::async, [&] { return reader->ReadNext (&batch); });
178+ EXPECT_EQ (retried.get_future ().wait_for (std::chrono::seconds (2 )), std::future_status::ready);
179+ EXPECT_EQ (fetch.wait_for (std::chrono::seconds (0 )), std::future_status::timeout);
180+ auto block = std::make_shared<Block>(ColumnHelper::create_block<DataTypeInt64>({1 , 2 }));
181+ EXPECT_TRUE (_buffer->add_batch (&_state, block).ok ());
182+ bool fully_closed = false ;
183+ EXPECT_TRUE (_buffer->close (_state.fragment_instance_id (), Status::OK (), 2 , fully_closed).ok ());
184+ const auto ready = fetch.wait_for (std::chrono::seconds (2 ));
185+ if (ready != std::future_status::ready) {
186+ _buffer->cancel (Status::Cancelled (" test cleanup" ));
187+ }
188+ EXPECT_EQ (ready, std::future_status::ready);
189+ ASSERT_TRUE (fetch.get ().ok ());
190+ ASSERT_NE (batch, nullptr );
191+ EXPECT_EQ (batch->num_rows (), 2 );
192+ ASSERT_TRUE (reader->ReadNext (&batch).ok ());
193+ EXPECT_EQ (batch, nullptr );
194+ EXPECT_TRUE (reader->Close ().ok ());
195+ EXPECT_FALSE (_state.get_query_ctx ()->is_cancelled ());
196+ }
197+
144198TEST_F (ArrowFlightCancelTest, ConversionFailureCancelsQuery) {
145199 auto block = std::make_shared<Block>(ColumnHelper::create_block<DataTypeString>({" bad" }));
146200 ASSERT_TRUE (_buffer->add_batch (&_state, block).ok ());
0 commit comments