-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathembedtest.cc
More file actions
223 lines (172 loc) · 6.56 KB
/
embedtest.cc
File metadata and controls
223 lines (172 loc) · 6.56 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
216
217
218
219
220
221
222
223
#define MYTEST_CONFIG_USE_MAIN
#include "mytest.h"
#include <chrono>
#include <filesystem>
#include <future>
#include <iostream>
#include <thread>
#include <lwnode-public.h>
#include <message-port.h>
template <typename T, size_t N>
constexpr size_t COUNT_OF(T (&)[N]) noexcept {
return N;
}
std::string getTimestamp() {
using namespace std::chrono;
auto now = system_clock::now();
auto timeT = system_clock::to_time_t(now);
auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
std::ostringstream oss;
oss << std::put_time(std::gmtime(&timeT), "%M:%S") << '.' << std::setw(3)
<< std::setfill('0') << ms.count();
return oss.str();
}
TEST0(Embedtest, MessagePort2_Post_Many_JS_First) {
auto runtime = std::make_shared<lwnode::Runtime>();
std::promise<void> promise;
std::future<void> init_future = promise.get_future();
const char* script = "test/embedding/test-02-message-port-many.js";
std::string path = (std::filesystem::current_path() / script).string();
const bool post_first = true;
char* args[] = {const_cast<char*>(""),
const_cast<char*>(path.c_str()),
const_cast<char*>(std::to_string(post_first).c_str())};
std::thread worker = std::thread(
[&](std::promise<void>&& promise) mutable {
runtime->Start(COUNT_OF(args), args, std::move(promise));
},
std::move(promise));
init_future.wait();
int count1 = 0;
auto port2 = runtime->GetPort();
port2->OnMessage([&](const MessageEvent* event) {
count1++;
if (event->data() == "ping") {
auto extra = std::to_string(count1);
std::cout << getTimestamp() << " NS pong " + extra << std::endl;
port2->PostMessage(MessageEvent::New("pong " + extra));
} else {
std::cout << getTimestamp() << " NS ping" << std::endl;
port2->PostMessage(MessageEvent::New("ping"));
}
});
if (post_first == 0) {
std::cout << getTimestamp() << " NS ping" << std::endl;
port2->PostMessage(MessageEvent::New("ping"));
}
worker.join();
EXPECT_EQ(count1, 10);
}
TEST0(Embedtest, MessagePort2_Post_JS_First) {
int count1 = 0;
auto runtime = std::make_shared<lwnode::Runtime>();
// 1. Set OnMessage before starting the runtime
auto port2 = runtime->GetPort();
port2->OnMessage([&](const MessageEvent* event) {
count1++;
if (event->data() == "ping") {
auto extra = std::to_string(count1);
std::cout << getTimestamp() << " NS pong " + extra << std::endl;
port2->PostMessage(MessageEvent::New("pong " + extra));
} else {
std::cout << getTimestamp() << " NS ping" << std::endl;
port2->PostMessage(MessageEvent::New("ping"));
}
});
// 2. Start the runtime
std::promise<void> promise;
std::future<void> init_future = promise.get_future();
const char* script = "test/embedding/test-05-message-port-first.js";
std::string path = (std::filesystem::current_path() / script).string();
const bool post_first = true;
char* args[] = {const_cast<char*>(""),
const_cast<char*>(path.c_str()),
const_cast<char*>(std::to_string(post_first).c_str())};
std::thread worker = std::thread(
[&](std::promise<void>&& promise) mutable {
runtime->Start(COUNT_OF(args), args, std::move(promise));
},
std::move(promise));
// 3. Wait for the entry script to run
init_future.wait();
worker.join();
EXPECT_EQ(count1, 1);
}
TEST0(Embedtest, Restart) {
TEST_SKIP("This is not yet production-ready.");
int count = 0;
for (int i = 0; i < 3; i++) {
auto runtime = std::make_shared<lwnode::Runtime>();
std::promise<void> promise;
std::future<void> init_future = promise.get_future();
const char* script = "test/embedding/test-21-runtime-hello.js";
std::string path = (std::filesystem::current_path() / script).string();
char* args[] = {const_cast<char*>(""), const_cast<char*>(path.c_str())};
std::thread worker = std::thread(
[&](std::promise<void>&& promise) mutable {
std::cout << ++count << " Start " << std::endl;
runtime->Start(COUNT_OF(args), args, std::move(promise));
std::cout << count << " /Start " << std::endl;
},
std::move(promise));
init_future.wait();
worker.join();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
EXPECT_EQ(count, 3);
}
TEST0(Embedtest, RestartAfterStop) {
TEST_SKIP("This is not yet production-ready.");
int count = 0;
for (int i = 0; i < 3; i++) {
auto runtime = std::make_shared<lwnode::Runtime>();
std::promise<void> promise;
std::future<void> init_future = promise.get_future();
const char* script = "test/embedding/test-22-runtime-heartbeat.js";
std::string path = (std::filesystem::current_path() / script).string();
char* args[] = {const_cast<char*>(""),
const_cast<char*>("--unhandled-rejections=strict"),
const_cast<char*>("--expose-gc"),
const_cast<char*>(path.c_str())};
std::cout << ++count << "Thread " << std::endl;
std::thread worker = std::thread(
[&](std::promise<void>&& promise) mutable {
std::cout << count << " Start " << std::endl;
runtime->Start(COUNT_OF(args), args, std::move(promise));
std::cout << count << " /Start " << std::endl;
},
std::move(promise));
init_future.wait();
std::this_thread::sleep_for(std::chrono::seconds(3));
runtime->Stop();
worker.join();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << count << " /Thread " << std::endl;
}
}
TEST(Embedtest, MessagePortErrorAfterRegisterOnMessage, 5000) {
auto runtime = std::make_shared<lwnode::Runtime>();
std::promise<void> promise;
std::future<void> init_future = promise.get_future();
const char* script = "test/embedding/test-04-message-port-error.js";
std::string path = (std::filesystem::current_path() / script).string();
const bool post_first = true;
char* args[] = {const_cast<char*>(""),
const_cast<char*>(path.c_str()),
const_cast<char*>(std::to_string(post_first).c_str())};
std::thread worker = std::thread(
[&](std::promise<void>&& promise) mutable {
runtime->Start(COUNT_OF(args), args, std::move(promise));
},
std::move(promise));
init_future.wait();
int count1 = 0;
auto port2 = runtime->GetPort();
port2->OnMessage([&](const MessageEvent* event) {
std::cout << event->data() << std::endl;
count1++;
});
port2->PostMessage(MessageEvent::New("ping"));
// This test should not exit due to timeout
worker.join();
}