• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Test for "overlapped" stdio option. This test uses the "overlapped-checker"
2// helper program which basically a specialized echo program.
3//
4// The test has two goals:
5//
6// - Verify that overlapped I/O works on windows. The test program will deadlock
7//   if stdin doesn't have the FILE_FLAG_OVERLAPPED flag set on startup (see
8//   test/overlapped-checker/main_win.c for more details).
9// - Verify that "overlapped" stdio option works transparently as a pipe (on
10//   unix/windows)
11//
12// This is how the test works:
13//
14// - This script assumes only numeric strings are written to the test program
15//   stdout.
16// - The test program will be spawned with "overlapped" set on stdin and "pipe"
17//   set on stdout/stderr and at startup writes a number to its stdout
18// - When this script receives some data, it will parse the number, add 50 and
19//   write to the test program's stdin.
20// - The test program will then echo the number back to us which will repeat the
21//   cycle until the number reaches 200, at which point we send the "exit"
22//   string, which causes the test program to exit.
23// - Extra assertion: Every time the test program writes a string to its stdout,
24//   it will write the number of bytes written to stderr.
25// - If overlapped I/O is not setup correctly, this test is going to hang.
26'use strict';
27const common = require('../common');
28const assert = require('assert');
29const path = require('path');
30const child_process = require('child_process');
31
32const exeExtension = process.platform === 'win32' ? '.exe' : '';
33const exe = 'overlapped-checker' + exeExtension;
34const exePath = path.join(path.dirname(process.execPath), exe);
35
36const child = child_process.spawn(exePath, [], {
37  stdio: ['overlapped', 'pipe', 'pipe']
38});
39
40child.stdin.setEncoding('utf8');
41child.stdout.setEncoding('utf8');
42child.stderr.setEncoding('utf8');
43
44function writeNext(n) {
45  child.stdin.write((n + 50).toString());
46}
47
48child.stdout.on('data', (s) => {
49  const n = Number(s);
50  if (n >= 200) {
51    child.stdin.write('exit');
52    return;
53  }
54  writeNext(n);
55});
56
57let stderr = '';
58child.stderr.on('data', (s) => {
59  stderr += s;
60});
61
62child.stderr.on('end', common.mustCall(() => {
63  // This is the sequence of numbers sent to us:
64  // - 0 (1 byte written)
65  // - 50 (2 bytes written)
66  // - 100 (3 bytes written)
67  // - 150 (3 bytes written)
68  // - 200 (3 bytes written)
69  assert.strictEqual(stderr, '12333');
70}));
71
72child.on('exit', common.mustCall((status) => {
73  // The test program will return the number of writes as status code.
74  assert.strictEqual(status, 0);
75}));
76