-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
coroutine: experimental: generator: implement move and swap #2354
Open
bhalevy
wants to merge
1
commit into
scylladb:master
Choose a base branch
from
bhalevy:fix-coroutine-generator-move
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -741,6 +741,14 @@ SEASTAR_TEST_CASE(test_as_future_preemption) { | |
BOOST_REQUIRE_THROW(f0.get(), std::runtime_error); | ||
} | ||
|
||
std::vector<int> gen_expected_fibs(unsigned count) { | ||
std::vector<int> expected_fibs = {0, 1}; | ||
for (unsigned i = 2; i < count; ++i) { | ||
expected_fibs.emplace_back(expected_fibs[i-2] + expected_fibs[i-1]); | ||
} | ||
return expected_fibs; | ||
} | ||
|
||
template<template<typename> class Container> | ||
coroutine::experimental::generator<int, Container> | ||
fibonacci_sequence(coroutine::experimental::buffer_size_t size, unsigned count) { | ||
|
@@ -755,10 +763,8 @@ fibonacci_sequence(coroutine::experimental::buffer_size_t size, unsigned count) | |
} | ||
|
||
template<template<typename> class Container> | ||
seastar::future<> test_async_generator_drained() { | ||
auto expected_fibs = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}; | ||
auto fib = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, | ||
std::size(expected_fibs)); | ||
seastar::future<> test_async_generator_drained(coroutine::experimental::generator<int, Container> fib, unsigned count) { | ||
auto expected_fibs = gen_expected_fibs(count); | ||
for (auto expected_fib : expected_fibs) { | ||
auto actual_fib = co_await fib(); | ||
BOOST_REQUIRE(actual_fib.has_value()); | ||
|
@@ -768,6 +774,37 @@ seastar::future<> test_async_generator_drained() { | |
BOOST_REQUIRE(!sentinel.has_value()); | ||
} | ||
|
||
template<template<typename> class Container> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The entire new section is missing some verbal explanations about the tests, the expected results and how they are achieved. |
||
seastar::future<> test_async_generator_drained() { | ||
unsigned count = 11; | ||
co_await test_async_generator_drained(fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, count), count); | ||
} | ||
|
||
template<template<typename> class Container> | ||
seastar::future<> test_move_async_generator_drained() { | ||
unsigned count = 11; | ||
auto fib0 = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, count); | ||
co_await test_async_generator_drained(std::move(fib0), count); | ||
auto fib1 = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, ++count); | ||
fib0 = std::move(fib1); | ||
co_await test_async_generator_drained(std::move(fib0), count); | ||
fib0 = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, ++count); | ||
fib1 = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, ++count); | ||
fib0 = std::move(fib1); | ||
co_await test_async_generator_drained(std::move(fib0), count); | ||
} | ||
|
||
template<template<typename> class Container> | ||
seastar::future<> test_swap_async_generator_drained() { | ||
unsigned count[2] = {11, 17}; | ||
auto fib0 = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, count[0]); | ||
auto fib1 = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, count[1]); | ||
std::swap(fib0, fib1); | ||
std::swap(count[0], count[1]); | ||
co_await test_async_generator_drained(std::move(fib0), count[0]); | ||
co_await test_async_generator_drained(std::move(fib1), count[1]); | ||
} | ||
|
||
template<typename T> | ||
using buffered_container = circular_buffer<T>; | ||
|
||
|
@@ -779,6 +816,55 @@ SEASTAR_TEST_CASE(test_async_generator_drained_unbuffered) { | |
return test_async_generator_drained<std::optional>(); | ||
} | ||
|
||
SEASTAR_TEST_CASE(test_move_async_generator_drained_buffered) { | ||
return test_move_async_generator_drained<buffered_container>(); | ||
} | ||
|
||
SEASTAR_TEST_CASE(test_move_async_generator_drained_unbuffered) { | ||
return test_move_async_generator_drained<std::optional>(); | ||
} | ||
|
||
SEASTAR_TEST_CASE(test_swap_async_generator_drained_buffered) { | ||
return test_swap_async_generator_drained<buffered_container>(); | ||
} | ||
|
||
SEASTAR_TEST_CASE(test_swap_async_generator_drained_unbuffered) { | ||
return test_swap_async_generator_drained<std::optional>(); | ||
} | ||
|
||
template<template<typename> class Container> | ||
seastar::future<coroutine::experimental::generator<int, Container>> test_async_generator_drained_incrementally(coroutine::experimental::generator<int, Container> fib, std::optional<int> expected_value) { | ||
auto actual_fib = co_await fib(); | ||
if (expected_value) { | ||
BOOST_REQUIRE(actual_fib.has_value()); | ||
BOOST_REQUIRE_EQUAL(actual_fib.value(), *expected_value); | ||
} else { | ||
BOOST_REQUIRE(!actual_fib.has_value()); | ||
} | ||
co_return fib; | ||
} | ||
|
||
template<template<typename> class Container> | ||
seastar::future<> test_async_generator_drained_incrementally() { | ||
unsigned count = 17; | ||
auto expected_fibs = gen_expected_fibs(count); | ||
auto fib = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, count); | ||
for (auto it = expected_fibs.begin(); it != expected_fibs.end(); ++it) { | ||
fib = co_await test_async_generator_drained_incrementally(std::move(fib), *it); | ||
} | ||
fib = co_await test_async_generator_drained_incrementally(std::move(fib), std::nullopt); | ||
// once drained generator return std::nullopt | ||
fib = co_await test_async_generator_drained_incrementally(std::move(fib), std::nullopt); | ||
} | ||
|
||
SEASTAR_TEST_CASE(test_async_generator_drained_incrementally_buffered) { | ||
return test_async_generator_drained_incrementally<buffered_container>(); | ||
} | ||
|
||
SEASTAR_TEST_CASE(test_async_generator_drained_incrementally_unbuffered) { | ||
return test_async_generator_drained_incrementally<std::optional>(); | ||
} | ||
|
||
template<template<typename> class Container> | ||
seastar::future<> test_async_generator_not_drained() { | ||
auto fib = fibonacci_sequence<Container>(coroutine::experimental::buffer_size_t{2}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe: