This source file includes following definitions.
- CountCallbacks
- NewIOBuffer
- Write
- ValidateIOBuffer
- consuming_seed_key_
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "content/browser/byte_stream.h"
#include <deque>
#include <limits>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "net/base/io_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
void CountCallbacks(int* counter) {
++*counter;
}
}
class ByteStreamTest : public testing::Test {
public:
ByteStreamTest();
scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size));
char *bufferp = buffer->data();
for (size_t i = 0; i < buffer_size; i++)
bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
pointer_queue_.push_back(bufferp);
length_queue_.push_back(buffer_size);
++producing_seed_key_;
return buffer;
}
bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
}
bool ValidateIOBuffer(
scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
char *bufferp = buffer->data();
char *expected_ptr = pointer_queue_.front();
size_t expected_length = length_queue_.front();
pointer_queue_.pop_front();
length_queue_.pop_front();
++consuming_seed_key_;
EXPECT_EQ(expected_ptr, bufferp);
if (expected_ptr != bufferp)
return false;
EXPECT_EQ(expected_length, buffer_size);
if (expected_length != buffer_size)
return false;
for (size_t i = 0; i < buffer_size; i++) {
EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
% (1 << sizeof(char))),
bufferp[i]);
if (static_cast<int>((i + consuming_seed_key_ - 1) %
(1 << sizeof(char))) != bufferp[i]) {
return false;
}
}
return true;
}
protected:
base::MessageLoop message_loop_;
private:
int producing_seed_key_;
int consuming_seed_key_;
std::deque<char*> pointer_queue_;
std::deque<size_t> length_queue_;
};
ByteStreamTest::ByteStreamTest()
: producing_seed_key_(0),
consuming_seed_key_(0) { }
TEST_F(ByteStreamTest, ByteStream_PushBack) {
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
3 * 1024, &byte_stream_input, &byte_stream_output);
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
EXPECT_FALSE(Write(byte_stream_input.get(), 1));
EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
byte_stream_input->Close(0);
EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
message_loop_.RunUntilIdle();
EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
message_loop_.RunUntilIdle();
EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
}
TEST_F(ByteStreamTest, ByteStream_Flush) {
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
1024, &byte_stream_input, &byte_stream_output);
EXPECT_TRUE(Write(byte_stream_input.get(), 1));
message_loop_.RunUntilIdle();
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length = 0;
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
byte_stream_input->Flush();
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
byte_stream_input->Flush();
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
byte_stream_input->Close(0);
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
9 * 1024, &byte_stream_input, &byte_stream_output);
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
message_loop_.RunUntilIdle();
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
message_loop_.RunUntilIdle();
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
message_loop_.RunUntilIdle();
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
message_loop_.RunUntilIdle();
EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
message_loop_.RunUntilIdle();
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
3 * 1024, &byte_stream_input, &byte_stream_output);
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
byte_stream_input->Close(0);
message_loop_.RunUntilIdle();
ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_EQ(0, byte_stream_output->GetStatus());
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
3 * 1024, &byte_stream_input, &byte_stream_output);
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
byte_stream_input->Close(0);
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_EQ(0, byte_stream_output->GetStatus());
const int kFakeErrorCode = 22;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
3 * 1024, &byte_stream_input, &byte_stream_output);
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
byte_stream_input->Close(kFakeErrorCode);
message_loop_.RunUntilIdle();
ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
3 * 1024, &byte_stream_input, &byte_stream_output);
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
byte_stream_input->Close(kFakeErrorCode);
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
}
TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner(
new base::TestSimpleTaskRunner());
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), task_runner,
10000, &byte_stream_input, &byte_stream_output);
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
int num_callbacks = 0;
byte_stream_output->RegisterCallback(
base::Bind(CountCallbacks, &num_callbacks));
EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
message_loop_.RunUntilIdle();
EXPECT_EQ(0, num_callbacks);
task_runner->RunUntilIdle();
EXPECT_EQ(1, num_callbacks);
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner(
new base::TestSimpleTaskRunner());
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
task_runner, message_loop_.message_loop_proxy(),
10000, &byte_stream_input, &byte_stream_output);
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
int num_callbacks = 0;
byte_stream_input->RegisterCallback(
base::Bind(CountCallbacks, &num_callbacks));
EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(0, num_callbacks);
task_runner->RunUntilIdle();
EXPECT_EQ(1, num_callbacks);
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_EQ(1, num_callbacks);
task_runner->RunUntilIdle();
EXPECT_EQ(1, num_callbacks);
}
TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner(
new base::TestSimpleTaskRunner());
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), task_runner,
10000, &byte_stream_input, &byte_stream_output);
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
base::Closure intermediate_callback;
int num_callbacks = 0;
byte_stream_output->RegisterCallback(
base::Bind(CountCallbacks, &num_callbacks));
EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
message_loop_.RunUntilIdle();
EXPECT_EQ(0, num_callbacks);
int num_alt_callbacks = 0;
byte_stream_output->RegisterCallback(
base::Bind(CountCallbacks, &num_alt_callbacks));
task_runner->RunUntilIdle();
EXPECT_EQ(0, num_callbacks);
EXPECT_EQ(1, num_alt_callbacks);
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner(
new base::TestSimpleTaskRunner());
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
task_runner, message_loop_.message_loop_proxy(),
10000, &byte_stream_input, &byte_stream_output);
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
base::Closure intermediate_callback;
int num_callbacks = 0;
byte_stream_input->RegisterCallback(
base::Bind(CountCallbacks, &num_callbacks));
EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
int num_alt_callbacks = 0;
byte_stream_input->RegisterCallback(
base::Bind(CountCallbacks, &num_alt_callbacks));
task_runner->RunUntilIdle();
EXPECT_EQ(0, num_callbacks);
EXPECT_EQ(1, num_alt_callbacks);
EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
byte_stream_output->Read(&output_io_buffer, &output_length));
EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner(
new base::TestSimpleTaskRunner());
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), task_runner,
10000, &byte_stream_input, &byte_stream_output);
base::Closure intermediate_callback;
int num_callbacks = 0;
byte_stream_output->RegisterCallback(
base::Bind(CountCallbacks, &num_callbacks));
byte_stream_input->Close(0);
task_runner->RunUntilIdle();
EXPECT_EQ(1, num_callbacks);
}
TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
3 * 1024, &byte_stream_input, &byte_stream_output);
byte_stream_input->Close(0);
message_loop_.RunUntilIdle();
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
3 * 1024, &byte_stream_input, &byte_stream_output);
byte_stream_input->Flush();
message_loop_.RunUntilIdle();
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
byte_stream_input->Close(0);
message_loop_.RunUntilIdle();
EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
scoped_ptr<ByteStreamWriter> byte_stream_input;
scoped_ptr<ByteStreamReader> byte_stream_output;
CreateByteStream(
message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
std::numeric_limits<size_t>::max(),
&byte_stream_input, &byte_stream_output);
EXPECT_TRUE(Write(byte_stream_input.get(), 1));
scoped_refptr<net::IOBuffer> empty_io_buffer;
EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
std::numeric_limits<size_t>::max()));
message_loop_.RunUntilIdle();
scoped_refptr<net::IOBuffer> output_io_buffer;
size_t output_length;
EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
byte_stream_output->Read(&output_io_buffer, &output_length));
}
}