forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_pipe_utils_unittest.cc
64 lines (54 loc) · 2.1 KB
/
data_pipe_utils_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/data_pipe_utils/data_pipe_utils.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/condition_variable.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace common {
namespace {
void TransferBooleanValueAndExecute(const base::Closure& closure,
bool* to_write,
bool value) {
*to_write = value;
if (!closure.is_null()) {
closure.Run();
}
}
TEST(DataPipeUtilsTest, AsyncFileTransfer) {
const char kData[] = "Hello world.";
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath input;
base::FilePath output;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &input));
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &output));
ASSERT_TRUE(base::WriteFile(input, kData, arraysize(kData)));
base::MessageLoop loop;
scoped_refptr<base::SequencedWorkerPool> blocking_pool =
new base::SequencedWorkerPool(2, "blocking_pool");
bool write_succeded = false;
bool read_succeded = false;
DataPipe pipes;
CopyFromFile(input, pipes.producer_handle.Pass(), 0, blocking_pool.get(),
base::Bind(&TransferBooleanValueAndExecute, base::Closure(),
base::Unretained(&write_succeded)));
CopyToFile(pipes.consumer_handle.Pass(), output, blocking_pool.get(),
base::Bind(&TransferBooleanValueAndExecute,
base::MessageLoop::QuitClosure(),
base::Unretained(&read_succeded)));
loop.Run();
EXPECT_TRUE(write_succeded);
EXPECT_TRUE(read_succeded);
EXPECT_TRUE(base::ContentsEqual(input, output));
blocking_pool->Shutdown();
}
} // namespace
} // namespace common
} // namespace mojo