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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
// deno-lint-ignore-file no-undef
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import os from "node:os";
import {
assert,
assertEquals,
assertNotEquals,
assertThrows,
} from "@std/assert";
import console from "node:console";
Deno.test({
name: "build architecture is a string",
fn() {
assertEquals(typeof os.arch(), "string");
},
});
Deno.test({
name: "build architecture",
fn() {
if (Deno.build.arch == "x86_64") {
assertEquals(os.arch(), "x64");
} else if (Deno.build.arch == "aarch64") {
assertEquals(os.arch(), "arm64");
} else {
throw new Error("unreachable");
}
},
});
Deno.test({
name: "os machine (arch)",
fn() {
if (Deno.build.arch == "aarch64") {
assertEquals(os.machine(), "arm64");
} else {
assertEquals(os.machine(), Deno.build.arch);
}
},
});
Deno.test({
name: "home directory is a string",
fn() {
assertEquals(typeof os.homedir(), "string");
},
});
Deno.test({
name: "home directory when HOME is not set",
fn() {
Deno.env.delete("HOME");
assertEquals(typeof os.homedir(), "string");
},
});
Deno.test({
name: "tmp directory is a string",
fn() {
assertEquals(typeof os.tmpdir(), "string");
},
});
Deno.test({
name: "hostname is a string",
fn() {
assertEquals(typeof os.hostname(), "string");
},
});
Deno.test({
name: "platform is a string",
fn() {
assertEquals(typeof os.platform(), "string");
},
});
Deno.test({
name: "release is a string",
fn() {
assertEquals(typeof os.release(), "string");
},
});
Deno.test({
name: "type is a string",
fn() {
assertEquals(typeof os.type(), "string");
},
});
Deno.test({
name: "getPriority(): PID must be a 32 bit integer",
fn() {
assertThrows(
() => {
os.getPriority(3.15);
},
Error,
"pid must be 'an integer'",
);
assertThrows(
() => {
os.getPriority(9999999999);
},
Error,
"must be >= -2147483648 && <= 2147483647",
);
},
});
Deno.test({
name: "setPriority(): PID must be a 32 bit integer",
fn() {
assertThrows(
() => {
os.setPriority(3.15, 0);
},
Error,
"pid must be 'an integer'",
);
assertThrows(
() => {
os.setPriority(9999999999, 0);
},
Error,
"pid must be >= -2147483648 && <= 2147483647",
);
},
});
Deno.test({
name: "setPriority(): priority must be an integer between -20 and 19",
fn() {
assertThrows(
() => {
os.setPriority(0, 3.15);
},
Error,
"priority must be 'an integer'",
);
assertThrows(
() => {
os.setPriority(0, -21);
},
Error,
"priority must be >= -20 && <= 19",
);
assertThrows(
() => {
os.setPriority(0, 20);
},
Error,
"priority must be >= -20 && <= 19",
);
assertThrows(
() => {
os.setPriority(0, 9999999999);
},
Error,
"priority must be >= -20 && <= 19",
);
},
});
Deno.test({
name:
"setPriority(): if only one argument specified, then this is the priority, NOT the pid",
fn() {
assertThrows(
() => {
os.setPriority(3.15);
},
Error,
"priority must be 'an integer'",
);
assertThrows(
() => {
os.setPriority(-21);
},
Error,
"priority must be >= -20 && <= 19",
);
assertThrows(
() => {
os.setPriority(20);
},
Error,
"priority must be >= -20 && <= 19",
);
assertThrows(
() => {
os.setPriority(9999999999);
},
Error,
"priority must be >= -20 && <= 19",
);
},
});
Deno.test({
name: "EOL is as expected",
fn() {
assert(os.EOL == "\r\n" || os.EOL == "\n");
},
});
Deno.test({
name: "Endianness is determined",
fn() {
assert(["LE", "BE"].includes(os.endianness()));
},
});
Deno.test({
name: "Load average is an array of 3 numbers",
fn() {
const result = os.loadavg();
assert(result.length == 3);
assertEquals(typeof result[0], "number");
assertEquals(typeof result[1], "number");
assertEquals(typeof result[2], "number");
},
});
Deno.test({
name: "Primitive coercion works as expected",
fn() {
assertEquals(`${os.arch}`, os.arch());
assertEquals(`${os.endianness}`, os.endianness());
assertEquals(`${os.platform}`, os.platform());
},
});
Deno.test({
name: "Total memory amount should be greater than 0",
fn() {
assert(os.totalmem() > 0);
},
});
Deno.test({
name: "Free memory amount should be greater than 0",
fn() {
assert(os.freemem() > 0);
},
});
Deno.test({
name: "Uptime should be greater than 0",
fn() {
assert(os.uptime() > 0);
},
});
Deno.test({
name: "os.cpus()",
fn() {
assertEquals(os.cpus().length, navigator.hardwareConcurrency);
for (const cpu of os.cpus()) {
assert(cpu.model.length > 0);
assert(cpu.speed >= 0);
assert(cpu.times.user > 0);
assert(cpu.times.sys > 0);
assert(cpu.times.idle > 0);
}
},
});
Deno.test({
name: "os.setPriority() & os.getPriority()",
// disabled because os.getPriority() doesn't work without sudo
ignore: true,
fn() {
const child = new Deno.Command(Deno.execPath(), {
args: ["eval", "while (true) { console.log('foo') }"],
}).spawn();
const originalPriority = os.getPriority(child.pid);
assertNotEquals(originalPriority, os.constants.priority.PRIORITY_HIGH);
os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
assertEquals(
os.getPriority(child.pid),
os.constants.priority.PRIORITY_HIGH,
);
os.setPriority(child.pid, originalPriority);
assertEquals(os.getPriority(child.pid), originalPriority);
child.kill();
},
});
Deno.test({
name:
"os.setPriority() throw os permission denied error & os.getPriority() doesn't",
async fn() {
const child = new Deno.Command(Deno.execPath(), {
args: ["eval", "while (true) { console.log('foo') }"],
}).spawn();
assertThrows(
() => {
try {
os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
} catch (err) {
console.error(err);
throw err;
}
},
Deno.errors.PermissionDenied,
);
os.getPriority(child.pid);
child.kill();
await child.status;
},
});
// Gets the diff in log_10 scale
function diffLog10(a: number, b: number): number {
return Math.abs(Math.log10(a) - Math.log10(b));
}
Deno.test({
name:
"os.freemem() is equivalent of Deno.systemMemoryInfo().free except on linux",
ignore: Deno.build.os === "linux",
fn() {
const diff = diffLog10(os.freemem(), Deno.systemMemoryInfo().free);
assert(diff < 1);
},
});
Deno.test({
name:
"os.freemem() is equivalent of Deno.systemMemoryInfo().available on linux",
ignore: Deno.build.os !== "linux",
fn() {
const diff = diffLog10(os.freemem(), Deno.systemMemoryInfo().available);
assert(diff < 1);
},
});
|