-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathfile_tests.cpp
More file actions
215 lines (173 loc) · 4.61 KB
/
file_tests.cpp
File metadata and controls
215 lines (173 loc) · 4.61 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/io_service.hpp>
#include <cppcoro/read_only_file.hpp>
#include <cppcoro/write_only_file.hpp>
#include <cppcoro/read_write_file.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all.hpp>
#include <cppcoro/cancellation_source.hpp>
#include <cppcoro/on_scope_exit.hpp>
#include <random>
#include <thread>
#include <cassert>
#include "io_service_fixture.hpp"
#include <ostream>
#include <string>
#include "doctest/doctest.h"
TEST_SUITE_BEGIN("file");
namespace fs = std::experimental::filesystem;
namespace
{
class temp_dir_fixture
{
public:
temp_dir_fixture()
{
auto tempDir = fs::temp_directory_path();
std::random_device random;
for (int attempt = 1;; ++attempt)
{
m_path = tempDir / std::to_string(random());
try
{
fs::create_directories(m_path);
return;
}
catch (const fs::filesystem_error&)
{
if (attempt == 10)
{
throw;
}
}
}
}
~temp_dir_fixture()
{
fs::remove_all(m_path);
}
const std::experimental::filesystem::path& temp_dir()
{
return m_path;
}
private:
std::experimental::filesystem::path m_path;
};
class temp_dir_with_io_service_fixture :
public io_service_fixture,
public temp_dir_fixture
{
};
}
TEST_CASE_FIXTURE(temp_dir_fixture, "write a file")
{
auto filePath = temp_dir() / "foo";
cppcoro::io_service ioService;
auto write = [&](cppcoro::io_service& ioService) -> cppcoro::task<>
{
std::printf(" starting write\n"); std::fflush(stdout);
auto f = cppcoro::write_only_file::open(ioService, filePath);
CHECK(f.size() == 0);
char buffer[1024];
char c = 'a';
for (int i = 0; i < sizeof(buffer); ++i, c = (c == 'z' ? 'a' : c + 1))
{
buffer[i] = c;
}
for (int chunk = 0; chunk < 10; ++chunk)
{
co_await f.write(chunk * sizeof(buffer), buffer, sizeof(buffer));
}
};
auto read = [&](cppcoro::io_service& io) -> cppcoro::task<>
{
std::printf(" starting read\n"); std::fflush(stdout);
auto f = cppcoro::read_only_file::open(io, filePath);
const auto fileSize = f.size();
CHECK(fileSize == 10240);
char buffer[20];
for (std::uint64_t i = 0; i < fileSize;)
{
auto bytesRead = co_await f.read(i, buffer, 20);
for (size_t j = 0; j < bytesRead; ++j, ++i)
{
CHECK(buffer[j] == ('a' + ((i % 1024) % 26)));
}
}
};
cppcoro::sync_wait(cppcoro::when_all(
[&]() -> cppcoro::task<int>
{
auto stopOnExit = cppcoro::on_scope_exit([&] { ioService.stop(); });
co_await write(ioService);
co_await read(ioService);
co_return 0;
}(),
[&]() -> cppcoro::task<int>
{
ioService.process_events();
co_return 0;
}()));
}
TEST_CASE_FIXTURE(temp_dir_with_io_service_fixture, "read write file")
{
auto run = [&]() -> cppcoro::task<>
{
cppcoro::io_work_scope ioScope{ io_service() };
auto f = cppcoro::read_write_file::open(io_service(), temp_dir() / "foo.txt");
char buffer1[100];
std::memset(buffer1, 0xAB, sizeof(buffer1));
co_await f.write(0, buffer1, sizeof(buffer1));
char buffer2[50];
std::memset(buffer2, 0xCC, sizeof(buffer2));
co_await f.read(0, buffer2, 50);
CHECK(std::memcmp(buffer1, buffer2, 50) == 0);
co_await f.read(50, buffer2, 50);
CHECK(std::memcmp(buffer1 + 50, buffer2, 50) == 0);
};
cppcoro::sync_wait(run());
}
TEST_CASE_FIXTURE(temp_dir_with_io_service_fixture, "cancel read")
{
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
cppcoro::io_work_scope ioScope{ io_service() };
auto f = cppcoro::read_write_file::open(io_service(), temp_dir() / "foo.txt");
f.set_size(20 * 1024 * 1024);
cppcoro::cancellation_source canceller;
try
{
(void)co_await cppcoro::when_all(
[&]() -> cppcoro::task<int>
{
const auto fileSize = f.size();
const std::size_t bufferSize = 64 * 1024;
auto buffer = std::make_unique<std::uint8_t[]>(bufferSize);
std::uint64_t offset = 0;
while (offset < fileSize)
{
auto bytesRead = co_await f.read(offset, buffer.get(), bufferSize, canceller.token());
offset += bytesRead;
}
WARN("should have been cancelled");
co_return 0;
}(),
[&]() -> cppcoro::task<int>
{
using namespace std::chrono_literals;
co_await io_service().schedule_after(1ms);
canceller.request_cancellation();
co_return 0;
}());
WARN("Expected exception to be thrown");
}
catch (const cppcoro::operation_cancelled&)
{
}
}());
}
TEST_SUITE_END();