Skip to content

Commit eeb6e9c

Browse files
daeyeonhs0225
authored andcommitted
test: add Start with no resource
Signed-off-by: Daeyeon Jeong <[email protected]>
1 parent 09f3bcc commit eeb6e9c

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

test/embedding/embedtest.cc

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <chrono>
55
#include <filesystem>
6+
#include <fstream>
67
#include <future>
78
#include <iostream>
89
#include <thread>
@@ -177,3 +178,167 @@ TEST(Embedtest, MessagePortErrorAfterRegisterOnMessage, 5000) {
177178
// This test should not exit due to timeout
178179
worker.join();
179180
}
181+
182+
class OnScopeLeave {
183+
public:
184+
// clang-format off
185+
using Function = std::function<void()>;
186+
OnScopeLeave(const OnScopeLeave& other) = delete;
187+
OnScopeLeave& operator=(const OnScopeLeave& other) = delete;
188+
explicit OnScopeLeave(Function&& function) : function_(std::move(function)) {}
189+
OnScopeLeave(OnScopeLeave&& other) : function_(std::move(other.function_)) { other.function_ = nullptr; }
190+
~OnScopeLeave() { if (function_) function_(); }
191+
static WARN_UNUSED_RESULT OnScopeLeave create(Function&& function) { return OnScopeLeave(std::move(function)); }
192+
// clang-format on
193+
private:
194+
Function function_;
195+
};
196+
197+
static std::filesystem::path getOldPath(
198+
const std::filesystem::path& original_path) {
199+
std::filesystem::path old_path;
200+
if (original_path.has_extension()) {
201+
old_path =
202+
original_path.parent_path() / (original_path.stem().string() + "_old" +
203+
original_path.extension().string());
204+
} else {
205+
old_path = original_path.parent_path() /
206+
(original_path.filename().string() + "_old");
207+
}
208+
return old_path;
209+
}
210+
211+
static void RenameToOld(const std::string& file_path) {
212+
std::filesystem::path original_path(file_path);
213+
std::filesystem::path old_path = getOldPath(original_path);
214+
215+
try {
216+
if (std::filesystem::exists(original_path)) {
217+
std::filesystem::rename(original_path, old_path);
218+
std::cout << "Renamed '" << original_path << "' to '" << old_path << "'"
219+
<< std::endl;
220+
}
221+
} catch (const std::filesystem::filesystem_error& e) {
222+
std::cerr << "Error renaming to old: " << e.what() << std::endl;
223+
}
224+
}
225+
226+
static void RenameToOriginal(const std::string& file_path) {
227+
std::filesystem::path original_path(file_path);
228+
std::filesystem::path old_path = getOldPath(original_path);
229+
230+
try {
231+
if (std::filesystem::exists(old_path)) {
232+
std::filesystem::rename(old_path, original_path);
233+
std::cout << "Reverted '" << old_path << "' to '" << original_path << "'"
234+
<< std::endl;
235+
}
236+
} catch (const std::filesystem::filesystem_error& e) {
237+
std::cerr << "Error reverting to original: " << e.what() << std::endl;
238+
}
239+
}
240+
241+
TEST0(Embedtest, Rename_Test) {
242+
std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
243+
std::filesystem::path original_temp_file_path =
244+
temp_dir / "embedtest_rename_test_file.tmp";
245+
std::filesystem::path old_temp_file_path =
246+
getOldPath(original_temp_file_path);
247+
248+
auto _ = OnScopeLeave::create([&]() {
249+
std::error_code ec;
250+
std::filesystem::remove(original_temp_file_path, ec);
251+
std::filesystem::remove(old_temp_file_path, ec);
252+
});
253+
254+
// 2. Setup
255+
std::error_code ec;
256+
std::filesystem::remove(original_temp_file_path, ec);
257+
std::filesystem::remove(old_temp_file_path, ec);
258+
{
259+
std::ofstream outfile(original_temp_file_path);
260+
}
261+
ASSERT_EQ(std::filesystem::exists(original_temp_file_path), true);
262+
263+
// 3. Test RenameToOld
264+
RenameToOld(original_temp_file_path.string());
265+
ASSERT_EQ(!std::filesystem::exists(original_temp_file_path), true);
266+
ASSERT_EQ(std::filesystem::exists(old_temp_file_path), true);
267+
268+
// 4. Test RenameToOriginal
269+
RenameToOriginal(original_temp_file_path.string());
270+
ASSERT_EQ(std::filesystem::exists(original_temp_file_path), true);
271+
ASSERT_EQ(!std::filesystem::exists(old_temp_file_path), true);
272+
}
273+
274+
extern std::string getExternalBuiltinsPath();
275+
276+
TEST0(Embedtest, StartWithNoResource) {
277+
const char* script = "test/embedding/test-22-runtime-heartbeat.js";
278+
std::string path = (std::filesystem::current_path() / script).string();
279+
280+
char* args[] = {const_cast<char*>(""),
281+
const_cast<char*>("--unhandled-rejections=strict"),
282+
const_cast<char*>("--expose-gc"),
283+
const_cast<char*>(path.c_str())};
284+
285+
std::string builtin_path = getExternalBuiltinsPath();
286+
287+
// 0. Setup
288+
auto _ = OnScopeLeave::create([&]() { RenameToOriginal(builtin_path); });
289+
RenameToOriginal(builtin_path);
290+
291+
// 1. Simulate no lwnode.dat
292+
RenameToOld(builtin_path);
293+
294+
{
295+
auto runtime = std::make_shared<lwnode::Runtime>();
296+
297+
std::promise<void> promise;
298+
std::future<void> init_future = promise.get_future();
299+
300+
int result = -1;
301+
302+
std::thread worker = std::thread(
303+
[&](std::promise<void>&& promise) mutable {
304+
result = runtime->Start(COUNT_OF(args), args, std::move(promise));
305+
},
306+
std::move(promise));
307+
308+
init_future.wait_for(std::chrono::seconds(2));
309+
310+
ASSERT(result == 100);
311+
312+
worker.join();
313+
std::this_thread::sleep_for(std::chrono::seconds(1));
314+
std::cout << "" << std::endl;
315+
}
316+
317+
// 2. Simulate reverting lwnode.dat.
318+
RenameToOriginal(builtin_path);
319+
320+
{
321+
auto runtime = std::make_shared<lwnode::Runtime>();
322+
323+
std::promise<void> promise;
324+
std::future<void> init_future = promise.get_future();
325+
326+
int result = 0;
327+
328+
std::thread worker = std::thread(
329+
[&](std::promise<void>&& promise) mutable {
330+
result = runtime->Start(COUNT_OF(args), args, std::move(promise));
331+
},
332+
std::move(promise));
333+
334+
init_future.wait();
335+
336+
std::this_thread::sleep_for(std::chrono::seconds(3));
337+
runtime->Stop();
338+
339+
worker.join();
340+
std::this_thread::sleep_for(std::chrono::seconds(1));
341+
342+
EXPECT(result == 0);
343+
}
344+
}

0 commit comments

Comments
 (0)