Skip to content

Commit

Permalink
Fix microsoft#8695: til::spsc assignment operators don't return anyth…
Browse files Browse the repository at this point in the history
…ing (microsoft#8811)

The following code didn't previously work as the assignment operators
didn't return a self reference:

```cpp
auto channel = til::spsc::channel<QueueItem>(100);
auto producer = std::move(channel.first);
channel.first = std::move(producer);
```

## Validation Steps Performed

I've added a basic smoke test for `til::spsc`.

Closes microsoft#8695
  • Loading branch information
lhecker authored and mpela81 committed Jan 28, 2021
1 parent 31e6972 commit e6e8f17
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/inc/til/spsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ namespace til::spsc
{
drop();
_arc = std::exchange(other._arc, nullptr);
return *this;
}

~producer()
Expand Down Expand Up @@ -543,6 +544,7 @@ namespace til::spsc
{
drop();
_arc = std::exchange(other._arc, nullptr);
return *this;
}

~consumer()
Expand Down
32 changes: 32 additions & 0 deletions src/til/ut_til/SPSCTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,44 @@ class SPSCTests
TEST_CLASS_PROPERTY(L"TestTimeout", L"0:0:10") // 10s timeout
END_TEST_CLASS()

TEST_METHOD(SmokeTest);
TEST_METHOD(DropEmptyTest);
TEST_METHOD(DropSameRevolutionTest);
TEST_METHOD(DropDifferentRevolutionTest);
TEST_METHOD(IntegrationTest);
};

void SPSCTests::SmokeTest()
{
// This test mostly ensures that the API wasn't broken.

// construction
auto [tx, rx] = til::spsc::channel<int>(32);
std::array<int, 3> data{};

// move constructor
til::spsc::producer tx2(std::move(tx));
til::spsc::consumer rx2(std::move(rx));

// move assignment operator
tx = std::move(tx2);
rx = std::move(rx2);

// push
tx.emplace(0);
tx.push(data.begin(), data.end());
tx.push(til::spsc::block_initially, data.begin(), data.end());
tx.push(til::spsc::block_forever, data.begin(), data.end());
tx.push_n(data.begin(), data.size());
tx.push_n(til::spsc::block_initially, data.begin(), data.size());
tx.push_n(til::spsc::block_forever, data.begin(), data.size());

// pop
std::optional<int> x = rx.pop();
rx.pop_n(til::spsc::block_initially, data.begin(), data.size());
rx.pop_n(til::spsc::block_forever, data.begin(), data.size());
}

void SPSCTests::DropEmptyTest()
{
auto [tx, rx] = til::spsc::channel<drop_indicator>(5);
Expand Down

0 comments on commit e6e8f17

Please sign in to comment.