Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1839)

Unified Diff: net/base/upload_data_stream_unittest.cc

Issue 9350060: net: Make UploadDataStream::Init() asynchronous. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/upload_data_stream.cc ('k') | net/http/http_network_transaction.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/upload_data_stream_unittest.cc
diff --git a/net/base/upload_data_stream_unittest.cc b/net/base/upload_data_stream_unittest.cc
index 078f2007bbece58a34bff7c790a2ed4a2ae0fd68..a9ebc3494bfb7c333fdcaa23b13375db2dc1e276 100644
--- a/net/base/upload_data_stream_unittest.cc
+++ b/net/base/upload_data_stream_unittest.cc
@@ -12,9 +12,11 @@
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
+#include "base/threading/thread_restrictions.h"
#include "base/time.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
+#include "net/base/test_completion_callback.h"
#include "net/base/upload_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -37,13 +39,23 @@ class UploadDataStreamTest : public PlatformTest {
const base::Time& time,
bool error_expected);
+ virtual void SetUp() {
+ // To ensure that file IO is not performed on the main thread in the
+ // test (i.e. InitSync() will fail if file IO is performed).
+ base::ThreadRestrictions::SetIOAllowed(false);
+ }
+
+ virtual void TearDown() {
+ base::ThreadRestrictions::SetIOAllowed(true);
+ }
+
scoped_refptr<UploadData> upload_data_;
};
TEST_F(UploadDataStreamTest, EmptyUploadData) {
upload_data_->AppendBytes("", 0);
scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_));
- ASSERT_EQ(OK, stream->Init());
+ ASSERT_EQ(OK, stream->InitSync());
ASSERT_TRUE(stream.get());
EXPECT_EQ(0U, stream->size());
EXPECT_EQ(0U, stream->position());
@@ -53,7 +65,7 @@ TEST_F(UploadDataStreamTest, EmptyUploadData) {
TEST_F(UploadDataStreamTest, ConsumeAll) {
upload_data_->AppendBytes(kTestData, kTestDataSize);
scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_));
- ASSERT_EQ(OK, stream->Init());
+ ASSERT_EQ(OK, stream->InitSync());
ASSERT_TRUE(stream.get());
EXPECT_EQ(kTestDataSize, stream->size());
EXPECT_EQ(0U, stream->position());
@@ -69,9 +81,12 @@ TEST_F(UploadDataStreamTest, ConsumeAll) {
TEST_F(UploadDataStreamTest, FileSmallerThanLength) {
FilePath temp_file_path;
- ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path));
- ASSERT_EQ(static_cast<int>(kTestDataSize),
- file_util::WriteFile(temp_file_path, kTestData, kTestDataSize));
+ {
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+ ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path));
+ ASSERT_EQ(static_cast<int>(kTestDataSize),
+ file_util::WriteFile(temp_file_path, kTestData, kTestDataSize));
+ }
const uint64 kFakeSize = kTestDataSize*2;
std::vector<UploadData::Element> elements;
@@ -80,10 +95,13 @@ TEST_F(UploadDataStreamTest, FileSmallerThanLength) {
element.SetContentLength(kFakeSize);
elements.push_back(element);
upload_data_->SetElements(elements);
+ // Although upload_data_ carries a file, GetContentLengthSync() works here,
+ // as the file size is overriden by SetContentLength().
EXPECT_EQ(kFakeSize, upload_data_->GetContentLengthSync());
scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data_));
- ASSERT_EQ(OK, stream->Init());
+ // For the same reason, InitSync() works.
+ ASSERT_EQ(OK, stream->InitSync());
ASSERT_TRUE(stream.get());
EXPECT_EQ(kFakeSize, stream->size());
EXPECT_EQ(0U, stream->position());
@@ -101,7 +119,10 @@ TEST_F(UploadDataStreamTest, FileSmallerThanLength) {
EXPECT_EQ(kFakeSize, read_counter);
EXPECT_EQ(read_counter, stream->position());
- file_util::Delete(temp_file_path, false);
+ {
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+ file_util::Delete(temp_file_path, false);
+ }
}
void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path,
@@ -117,7 +138,10 @@ void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path,
upload_data->SetElements(elements);
scoped_ptr<UploadDataStream> stream(new UploadDataStream(upload_data));
- int error_code = stream->Init();
+ TestCompletionCallback callback;
+ int error_code = stream->Init(callback.callback());
+ ASSERT_EQ(ERR_IO_PENDING, error_code);
+ error_code = callback.WaitForResult();
if (error_expected)
ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code);
else
@@ -126,12 +150,14 @@ void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path,
TEST_F(UploadDataStreamTest, FileChanged) {
FilePath temp_file_path;
- ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path));
- ASSERT_EQ(static_cast<int>(kTestDataSize),
- file_util::WriteFile(temp_file_path, kTestData, kTestDataSize));
-
base::PlatformFileInfo file_info;
- ASSERT_TRUE(file_util::GetFileInfo(temp_file_path, &file_info));
+ {
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+ ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path));
+ ASSERT_EQ(static_cast<int>(kTestDataSize),
+ file_util::WriteFile(temp_file_path, kTestData, kTestDataSize));
+ ASSERT_TRUE(file_util::GetFileInfo(temp_file_path, &file_info));
+ }
// Test file not changed.
FileChangedHelper(temp_file_path, file_info.last_modified, false);
@@ -141,7 +167,10 @@ TEST_F(UploadDataStreamTest, FileChanged) {
file_info.last_modified - base::TimeDelta::FromSeconds(1),
true);
- file_util::Delete(temp_file_path, false);
+ {
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+ file_util::Delete(temp_file_path, false);
+ }
}
} // namespace net
« no previous file with comments | « net/base/upload_data_stream.cc ('k') | net/http/http_network_transaction.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698