]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/stream_reader_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / tests / unit / stream_reader_test.cc
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19 /*
20 * Copyright (C) 2020 ScyllaDB.
21 */
22
23 #include <seastar/core/future.hh>
24 #include <seastar/core/temporary_buffer.hh>
25 #include <seastar/core/thread.hh>
26 #include <seastar/testing/test_case.hh>
27 #include <seastar/testing/thread_test_case.hh>
28 #include <seastar/http/request.hh>
29 #include <seastar/util/short_streams.hh>
30 #include <string>
31
32 using namespace seastar;
33 using namespace util;
34
35 /*
36 * Simple data source producing up to total_size bytes
37 * in buffer_size-byte chunks.
38 * */
39 class test_source_impl : public data_source_impl {
40 short _current_letter = 0; // a-z corresponds to 0-25
41 size_t _buffer_size;
42 size_t _remaining_size;
43 public:
44 test_source_impl(size_t buffer_size, size_t total_size)
45 : _buffer_size(buffer_size), _remaining_size(total_size) {
46 }
47 virtual future<temporary_buffer<char>> get() override {
48 size_t len = std::min(_buffer_size, _remaining_size);
49 temporary_buffer<char> tmp(len);
50 for (size_t i = 0; i < len; i++) {
51 tmp.get_write()[i] = 'a' + _current_letter;
52 ++_current_letter %= 26;
53 }
54 _remaining_size -= len;
55 return make_ready_future<temporary_buffer<char>>(std::move(tmp));
56 }
57 virtual future<temporary_buffer<char>> skip(uint64_t n) override {
58 _remaining_size -= std::min(_remaining_size, n);
59 _current_letter += n %= 26;
60 return make_ready_future<temporary_buffer<char>>();
61 }
62 };
63
64 SEASTAR_TEST_CASE(test_read_all) {
65 return async([] {
66 auto check_read_all = [] (input_stream<char>& strm, const char* test) {
67 auto all = read_entire_stream(strm).get0();
68 sstring s;
69 for (auto&& buf: all) {
70 s += seastar::to_sstring(std::move(buf));
71 };
72 BOOST_REQUIRE_EQUAL(s, test);
73 };
74 input_stream<char> inp(data_source(std::make_unique<test_source_impl>(5, 15)));
75 check_read_all(inp, "abcdefghijklmno");
76 BOOST_REQUIRE(inp.eof());
77 input_stream<char> inp2(data_source(std::make_unique<test_source_impl>(5, 16)));
78 check_read_all(inp2, "abcdefghijklmnop");
79 BOOST_REQUIRE(inp2.eof());
80 input_stream<char> empty_inp(data_source(std::make_unique<test_source_impl>(5, 0)));
81 check_read_all(empty_inp, "");
82 BOOST_REQUIRE(empty_inp.eof());
83
84 input_stream<char> inp_cont(data_source(std::make_unique<test_source_impl>(5, 15)));
85 BOOST_REQUIRE_EQUAL(to_sstring(read_entire_stream_contiguous(inp_cont).get0()), "abcdefghijklmno");
86 BOOST_REQUIRE(inp_cont.eof());
87 input_stream<char> inp_cont2(data_source(std::make_unique<test_source_impl>(5, 16)));
88 BOOST_REQUIRE_EQUAL(to_sstring(read_entire_stream_contiguous(inp_cont2).get0()), "abcdefghijklmnop");
89 BOOST_REQUIRE(inp_cont2.eof());
90 input_stream<char> empty_inp_cont(data_source(std::make_unique<test_source_impl>(5, 0)));
91 BOOST_REQUIRE_EQUAL(to_sstring(read_entire_stream_contiguous(empty_inp_cont).get0()), "");
92 BOOST_REQUIRE(empty_inp_cont.eof());
93 });
94 }
95
96 SEASTAR_TEST_CASE(test_skip_all) {
97 return async([] {
98 input_stream<char> inp(data_source(std::make_unique<test_source_impl>(5, 15)));
99 skip_entire_stream(inp).get();
100 BOOST_REQUIRE(inp.eof());
101 BOOST_REQUIRE(to_sstring(inp.read().get0()).empty());
102 input_stream<char> inp2(data_source(std::make_unique<test_source_impl>(5, 16)));
103 skip_entire_stream(inp2).get();
104 BOOST_REQUIRE(inp2.eof());
105 BOOST_REQUIRE(to_sstring(inp2.read().get0()).empty());
106 input_stream<char> empty_inp(data_source(std::make_unique<test_source_impl>(5, 0)));
107 skip_entire_stream(empty_inp).get();
108 BOOST_REQUIRE(empty_inp.eof());
109 BOOST_REQUIRE(to_sstring(empty_inp.read().get0()).empty());
110 });
111 }