-
Notifications
You must be signed in to change notification settings - Fork 29.6k
/
test-trace-events-fs-sync.js
149 lines (139 loc) Β· 7.01 KB
/
test-trace-events-fs-sync.js
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
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const tests = new Array();
const traceFile = 'node_trace.1.log';
let gid = 1;
let uid = 1;
if (!common.isWindows) {
gid = process.getgid();
uid = process.getuid();
}
tests['fs.sync.access'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.accessSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.chmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.chmodSync("fs.txt",100);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.chown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.chownSync("fs.txt",' + uid + ',' + gid + ');' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.close'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.copyfile'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.copyFileSync("fs.txt","a.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fchmod'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.fchmodSync(fd,100);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fchown'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.fchownSync(fd,' + uid + ',' + gid + ');' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fdatasync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.fdatasyncSync(fd);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.readFileSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.fsync'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.fsyncSync(fd);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.ftruncate'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.ftruncateSync(fd, 1);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.futimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'const fd = fs.openSync("fs.txt", "r+");' +
'fs.futimesSync(fd,1,1);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.link'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.linkSync("fs.txt", "linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.lstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.lstatSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp");' +
'fs.rmdirSync("fstemp")';
tests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstest");' +
'fs.rmdirSync(fp)';
tests['fs.sync.open'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.read'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.readFileSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.readdir'] = 'fs.readdirSync("./")';
tests['fs.sync.realpath'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.linkSync("fs.txt", "linkx");' +
'fs.realpathSync.native("linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.rename'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.renameSync("fs.txt","xyz.txt"); ' +
'fs.unlinkSync("xyz.txt")';
tests['fs.sync.rmdir'] = 'fs.mkdirSync("fstemp");' +
'fs.rmdirSync("fstemp")';
tests['fs.sync.stat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.statSync("fs.txt");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.unlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.linkSync("fs.txt", "linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.utimes'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.utimesSync("fs.txt",1,1);' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.write'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.unlinkSync("fs.txt")';
// On windows, we need permissions to test symlink and readlink.
// We'll only try to run these tests if we have enough privileges.
if (common.canCreateSymLink()) {
tests['fs.sync.symlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.symlinkSync("fs.txt", "linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
tests['fs.sync.readlink'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
'fs.symlinkSync("fs.txt", "linkx");' +
'fs.readlinkSync("linkx");' +
'fs.unlinkSync("linkx");' +
'fs.unlinkSync("fs.txt")';
}
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
process.chdir(tmpdir.path);
for (const tr in tests) {
const proc = cp.spawnSync(process.execPath,
[ '--trace-events-enabled',
'--trace-event-categories', 'node.fs.sync',
'-e', tests[tr] ]);
// Some AIX versions don't support futimes or utimes, so skip.
if (common.isAIX && proc.status !== 0 && tr === 'fs.sync.futimes') {
continue;
}
if (common.isAIX && proc.status !== 0 && tr === 'fs.sync.utimes') {
continue;
}
// Make sure the operation is successful.
assert.strictEqual(proc.status, 0, tr + ': ' + proc.stderr);
// Confirm that trace log file is created.
assert(common.fileExists(traceFile));
const data = fs.readFileSync(traceFile);
const traces = JSON.parse(data.toString()).traceEvents;
assert(traces.length > 0);
// C++ fs sync trace events should be generated.
assert(traces.some((trace) => {
if (trace.pid !== proc.pid)
return false;
if (trace.cat !== 'node,node.fs,node.fs.sync')
return false;
if (trace.name !== tr)
return false;
return true;
}));
}