1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var path = require('path');
23 var assert = require('assert');
25 exports.testDir = path.dirname(__filename);
26 exports.fixturesDir = path.join(exports.testDir, 'fixtures');
27 exports.libDir = path.join(exports.testDir, '../lib');
28 exports.tmpDir = path.join(exports.testDir, 'tmp');
30 exports.PROXY_PORT = 1234567;
32 if (process.platform === 'win32') {
33 exports.PIPE = '\\\\.\\pipe\\libuv-test';
35 exports.PIPE = exports.tmpDir + '/test.sock';
38 var util = require('util');
39 for (var i in util) exports[i] = util[i];
40 //for (var i in exports) global[i] = exports[i];
42 function protoCtrChain(o) {
44 for (; o; o = o.__proto__) { result.push(o.constructor); }
48 exports.indirectInstanceOf = function (obj, cls) {
49 if (obj instanceof cls) { return true; }
50 var clsChain = protoCtrChain(cls.prototype);
51 var objChain = protoCtrChain(obj);
52 return objChain.slice(-clsChain.length) === clsChain;
56 exports.ddCommand = function (filename, kilobytes) {
57 if (process.platform === 'win32') {
58 var p = path.resolve(exports.fixturesDir, 'create-file.js');
59 return '"' + process.argv[0] + '" "' + p + '" "' +
60 filename + '" ' + (kilobytes * 1024);
62 return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes;
67 exports.spawnPwd = function (options) {
68 var spawn = require('child_process').spawn;
70 if (process.platform === 'win32') {
71 return spawn('cmd.exe', ['/c', 'cd'], options);
73 return spawn('pwd', [], options);
78 // Turn this off if the test should not check for global leaks.
79 exports.globalCheck = true;
81 process.on('exit', function () {
82 if (!exports.globalCheck) return;
83 var knownGlobals = [setTimeout,
92 if (global.setImmediate) {
93 knownGlobals.push(setImmediate);
94 knownGlobals.push(clearImmediate);
98 knownGlobals.push(errno);
102 knownGlobals.push(gc);
105 if (global.DTRACE_HTTP_SERVER_RESPONSE) {
106 knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
107 knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
108 knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
109 knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
110 knownGlobals.push(DTRACE_NET_STREAM_END);
111 knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
112 knownGlobals.push(DTRACE_NET_SOCKET_READ);
113 knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
116 if (global.ArrayBuffer) {
117 knownGlobals.push(ArrayBuffer);
118 knownGlobals.push(Int8Array);
119 knownGlobals.push(Uint8Array);
120 knownGlobals.push(Int16Array);
121 knownGlobals.push(Uint16Array);
122 knownGlobals.push(Int32Array);
123 knownGlobals.push(Uint32Array);
124 knownGlobals.push(Float32Array);
125 knownGlobals.push(Float64Array);
126 knownGlobals.push(DataView);
128 if (global.Uint8ClampedArray) {
129 knownGlobals.push(Uint8ClampedArray);
133 for (var x in global) {
136 for (var y in knownGlobals) {
137 if (global[x] === knownGlobals[y]) {
144 console.error('Unknown global: %s', x);
145 assert.ok(false, 'Unknown global found');
151 var mustCallChecks = [];
154 function runCallChecks() {
155 var failed = mustCallChecks.filter(function (context) {
156 return context.actual !== context.expected;
159 failed.forEach(function (context) {
160 console.log('Mismatched %s function calls. Expected %d, actual %d.',
164 console.log(context.stack.split('\n').slice(2).join('\n'));
167 if (failed.length) process.exit(1);
171 exports.mustCall = function (fn, expected) {
172 if (typeof expected !== 'number') expected = 1;
177 stack: (new Error).stack,
178 name: fn.name || '<anonymous>'
181 // add the exit listener only once to avoid listener leak warnings
182 if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
184 mustCallChecks.push(context);
188 return fn.apply(this, arguments);