Test iteration over a stream with and without attaching memory.
| 857 | |
| 858 | /// Test iteration over a stream with and without attaching memory. |
| 859 | void SimpleTupleStreamTest::TestAttachMemory(bool pin_stream, bool attach_on_read) { |
| 860 | // Use smaller buffers so that the explicit FLUSH_RESOURCES flag is required to |
| 861 | // make the batch at capacity. |
| 862 | int buffer_size = 4 * 1024; |
| 863 | Init(100 * buffer_size); |
| 864 | |
| 865 | BufferedTupleStream stream( |
| 866 | runtime_state_, int_desc_, &client_, buffer_size, buffer_size); |
| 867 | ASSERT_OK(stream.Init("SimpleTupleStreamTest::TestAttachMemory", pin_stream)); |
| 868 | bool got_write_reservation; |
| 869 | ASSERT_OK(stream.PrepareForWrite(&got_write_reservation)); |
| 870 | ASSERT_TRUE(got_write_reservation); |
| 871 | RowBatch* in_batch = CreateIntBatch(0, 1024, false); |
| 872 | |
| 873 | // Construct a stream with 4 pages. |
| 874 | const int total_num_pages = 4; |
| 875 | while (stream.byte_size() < total_num_pages * buffer_size) { |
| 876 | Status status; |
| 877 | for (int i = 0; i < in_batch->num_rows(); ++i) { |
| 878 | bool ret = stream.AddRow(in_batch->GetRow(i), &status); |
| 879 | EXPECT_TRUE(ret); |
| 880 | ASSERT_OK(status); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | RowBatch* out_batch = pool_.Add(new RowBatch(int_desc_, 100, &tracker_)); |
| 885 | int num_buffers_attached = 0; |
| 886 | int num_flushes = 0; |
| 887 | int64_t num_rows_returned = 0; |
| 888 | bool got_read_reservation; |
| 889 | ASSERT_OK(stream.PrepareForRead(attach_on_read, &got_read_reservation)); |
| 890 | ASSERT_TRUE(got_read_reservation); |
| 891 | bool eos; |
| 892 | do { |
| 893 | ASSERT_EQ(0, out_batch->num_buffers()); |
| 894 | ASSERT_OK(stream.GetNext(out_batch, &eos)); |
| 895 | EXPECT_LE(out_batch->num_buffers(), 1) << "Should only attach one buffer at a time"; |
| 896 | if (out_batch->num_buffers() > 0) { |
| 897 | EXPECT_TRUE(out_batch->AtCapacity()) << "Flush resources flag should have been set"; |
| 898 | } |
| 899 | num_buffers_attached += out_batch->num_buffers(); |
| 900 | for (int i = 0; i < out_batch->num_rows(); ++i) { |
| 901 | int slot_offset = int_desc_->tuple_descriptors()[0]->slots()[0]->tuple_offset(); |
| 902 | TupleRow* in_row = in_batch->GetRow(num_rows_returned % in_batch->num_rows()); |
| 903 | EXPECT_EQ(*in_row->GetTuple(0)->GetIntSlot(slot_offset), |
| 904 | *out_batch->GetRow(i)->GetTuple(0)->GetIntSlot(slot_offset)); |
| 905 | ++num_rows_returned; |
| 906 | } |
| 907 | num_flushes += out_batch->flush_mode() == RowBatch::FlushMode::FLUSH_RESOURCES; |
| 908 | out_batch->Reset(); |
| 909 | } while (!eos); |
| 910 | |
| 911 | if (attach_on_read) { |
| 912 | EXPECT_EQ(4, num_buffers_attached) << "All buffers attached during iteration."; |
| 913 | } else { |
| 914 | EXPECT_EQ(0, num_buffers_attached) << "No buffers attached during iteration."; |
| 915 | } |
| 916 | if (attach_on_read || !pin_stream) { |
nothing calls this directly
no test coverage detected