Skip to content

Commit e3708f2

Browse files
Check write buffer on stream closed check (#1578)
Helps to avoid "empty JSON" errors when stream is being reset Relates-To: DATASDK-57 Signed-off-by: Andrey Kashcheev <[email protected]>
1 parent c2e791c commit e3708f2

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

olp-cpp-sdk-dataservice-read/src/repositories/AsyncJsonStream.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ size_t RapidJsonByteStream::PutEnd(char*) { return 0; }
5050
bool RapidJsonByteStream::ReadEmpty() const {
5151
return count_ == read_buffer_.size();
5252
}
53-
bool RapidJsonByteStream::WriteEmpty() const { return write_buffer_.empty(); }
53+
bool RapidJsonByteStream::WriteEmpty() const {
54+
std::unique_lock<std::mutex> lock(mutex_);
55+
return write_buffer_.empty();
56+
}
5457

5558
void RapidJsonByteStream::AppendContent(const char* content, size_t length) {
5659
std::unique_lock<std::mutex> lock(mutex_);
@@ -64,7 +67,7 @@ void RapidJsonByteStream::AppendContent(const char* content, size_t length) {
6467

6568
void RapidJsonByteStream::SwapBuffers() {
6669
std::unique_lock<std::mutex> lock(mutex_);
67-
cv_.wait(lock, [&]() { return !WriteEmpty(); });
70+
cv_.wait(lock, [&]() { return !write_buffer_.empty(); });
6871
std::swap(read_buffer_, write_buffer_);
6972
write_buffer_.clear();
7073
count_ = 0;
@@ -114,7 +117,7 @@ boost::optional<client::ApiError> AsyncJsonStream::GetError() const {
114117

115118
bool AsyncJsonStream::IsClosed() const {
116119
std::unique_lock<std::mutex> lock(mutex_);
117-
return closed_;
120+
return closed_ && (error_ || current_stream_->WriteEmpty());
118121
}
119122

120123
} // namespace repository

olp-cpp-sdk-dataservice-read/tests/PartitionsRepositoryTest.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,10 +2076,13 @@ TEST_F(PartitionsRepositoryTest, StreamPartitions) {
20762076

20772077
repository.StreamPartitions(async_stream, kVersion, additional_fields,
20782078
billing_tag, context);
2079-
EXPECT_TRUE(async_stream->IsClosed());
2079+
// Not closed since the stream is not empty
2080+
EXPECT_FALSE(async_stream->IsClosed());
20802081
EXPECT_FALSE(async_stream->GetError());
20812082
EXPECT_STREQ(ref_stream_data.c_str(),
20822083
get_stream_content(*async_stream).c_str());
2084+
// Now it's closed as we read all its content
2085+
EXPECT_TRUE(async_stream->IsClosed());
20832086

20842087
{
20852088
SCOPED_TRACE("Data with offset is in the stream");
@@ -2091,10 +2094,13 @@ TEST_F(PartitionsRepositoryTest, StreamPartitions) {
20912094
repository.StreamPartitions(second_stream, kVersion, additional_fields,
20922095
billing_tag, context);
20932096

2094-
EXPECT_TRUE(second_stream->IsClosed());
2097+
// Not closed since the stream is not empty
2098+
EXPECT_FALSE(second_stream->IsClosed());
20952099
EXPECT_FALSE(second_stream->GetError());
20962100
EXPECT_STREQ((initial_value + ref_stream_data).c_str(),
20972101
get_stream_content(*second_stream).c_str());
2102+
// Now it's closed as we read all its content
2103+
EXPECT_TRUE(second_stream->IsClosed());
20982104
}
20992105
}
21002106
}

0 commit comments

Comments
 (0)