summaryrefslogtreecommitdiff
path: root/test/common/udppair.js
blob: 3f2b0aed78a16908a3aa60f7866858af424d547d (plain)
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
'use strict';
const { internalBinding } = require('internal/test/binding');
const { JSUDPWrap } = internalBinding('js_udp_wrap');
const EventEmitter = require('events');

// FakeUDPWrap is a testing utility that emulates a UDP connection
// for the sake of making UDP tests more deterministic.
class FakeUDPWrap extends EventEmitter {
  constructor() {
    super();

    this._handle = new JSUDPWrap();

    this._handle.onreadstart = () => this._startReading();
    this._handle.onreadstop = () => this._stopReading();
    this._handle.onwrite =
      (wrap, buffers, addr) => this._write(wrap, buffers, addr);
    this._handle.getsockname = (obj) => {
      Object.assign(obj, { address: '127.0.0.1', family: 'IPv4', port: 1337 });
      return 0;
    };

    this.reading = false;
    this.bufferedReceived = [];
    this.emitBufferedImmediate = null;
  }

  _emitBuffered = () => {
    if (!this.reading) return;
    if (this.bufferedReceived.length > 0) {
      this.emitReceived(this.bufferedReceived.shift());
      this.emitBufferedImmediate = setImmediate(this._emitBuffered);
    } else {
      this.emit('wantRead');
    }
  };

  _startReading() {
    this.reading = true;
    this.emitBufferedImmediate = setImmediate(this._emitBuffered);
  }

  _stopReading() {
    this.reading = false;
    clearImmediate(this.emitBufferedImmediate);
  }

  _write(wrap, buffers, addr) {
    this.emit('send', { buffers, addr });
    setImmediate(() => this._handle.onSendDone(wrap, 0));
  }

  afterBind() {
    this._handle.onAfterBind();
  }

  emitReceived(info) {
    if (!this.reading) {
      this.bufferedReceived.push(info);
      return;
    }

    const {
      buffers,
      addr: {
        family = 4,
        address = '127.0.0.1',
        port = 1337,
      },
      flags = 0,
    } = info;

    let familyInt;
    switch (family) {
      case 'IPv4': familyInt = 4; break;
      case 'IPv6': familyInt = 6; break;
      default: throw new Error('bad family');
    }

    for (const buffer of buffers) {
      this._handle.emitReceived(buffer, familyInt, address, port, flags);
    }
  }
}

function makeUDPPair() {
  const serverSide = new FakeUDPWrap();
  const clientSide = new FakeUDPWrap();

  serverSide.on('send',
                (chk) => setImmediate(() => clientSide.emitReceived(chk)));
  clientSide.on('send',
                (chk) => setImmediate(() => serverSide.emitReceived(chk)));

  return { serverSide, clientSide };
}

module.exports = {
  FakeUDPWrap,
  makeUDPPair,
};