node-datachannel 0.1.13 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/test.js CHANGED
@@ -35,115 +35,114 @@ describe('PeerConnection Classes', () => {
35
35
  });
36
36
 
37
37
 
38
- // describe('P2P', () => {
39
- // let peer1 = new nodeDataChannel.PeerConnection("Peer1", { iceServers: ["stun:stun.l.google.com:19302"] });
40
- // let peer2 = new nodeDataChannel.PeerConnection("Peer2", { iceServers: ["stun:stun.l.google.com:19302"] });
41
- // let dc1 = null;
42
- // let dc2 = null;
43
-
44
- // // Mocks
45
- // const p1StateMock = jest.fn();
46
- // const p1GatheringStateMock = jest.fn();
47
- // const p2StateMock = jest.fn();
48
- // const p2GatheringStateMock = jest.fn();
49
- // const p1SDPMock = jest.fn();
50
- // const p1CandidateMock = jest.fn();
51
- // const p2SDPMock = jest.fn();
52
- // const p2CandidateMock = jest.fn();
53
-
54
- // const p1DCMock = jest.fn();
55
- // const p1DCMessageMock = jest.fn();
56
- // const p2DCMock = jest.fn();
57
- // const p2DCMessageMock = jest.fn();
58
-
59
- // // Set Callbacks
60
- // peer1.onStateChange(p1StateMock);
61
- // peer1.onGatheringStateChange(p1GatheringStateMock);
62
- // peer1.onLocalDescription((sdp, type) => {
63
- // p1SDPMock();
64
- // peer2.setRemoteDescription(sdp, type);
65
- // });
66
- // peer1.onLocalCandidate((candidate, mid) => {
67
- // p1CandidateMock();
68
- // peer2.addRemoteCandidate(candidate, mid);
69
- // });
70
-
71
- // // Set Callbacks
72
- // peer2.onStateChange(p2StateMock);
73
- // peer2.onGatheringStateChange(p2GatheringStateMock);
74
- // peer2.onLocalDescription((sdp, type) => {
75
- // p2SDPMock();
76
- // peer1.setRemoteDescription(sdp, type);
77
- // });
78
- // peer2.onLocalCandidate((candidate, mid) => {
79
- // p2CandidateMock();
80
- // peer1.addRemoteCandidate(candidate, mid);
81
- // });
82
- // peer2.onDataChannel((dc) => {
83
- // p2DCMock();
84
- // dc2 = dc;
85
- // dc2.onMessage((msg) => {
86
- // p2DCMessageMock(msg);
87
- // });
88
- // dc2.sendMessage("Hello From Peer2");
89
- // });
90
-
91
- // dc1 = peer1.createDataChannel("test-p2p");
92
- // dc1.onOpen(() => {
93
- // p1DCMock();
94
- // dc1.sendMessage("Hello From Peer1");
95
- // });
96
- // dc1.onMessage((msg) => {
97
- // p1DCMessageMock(msg);
98
- // });
99
-
100
- // test('P2P', (done) => {
101
- // // Default is 5000 ms but we need more
102
- // jest.setTimeout(30000);
103
-
104
- // setTimeout(() => {
105
- // dc1.close();
106
- // dc2.close();
107
- // peer1.close();
108
- // peer2.close();
109
-
110
- // // Fee memory
111
- // dc1 = null;
112
- // dc2 = null;
113
- // peer1 = null;
114
- // peer2 = null;
115
-
116
- // // Cleanup Threads
117
- // nodeDataChannel.cleanup();
118
- // }, 8 * 1000);
119
-
120
- // setTimeout(() => {
121
- // // State Callbacks
122
- // expect(p1StateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
123
- // expect(p1GatheringStateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
124
- // expect(p2StateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
125
- // expect(p2GatheringStateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
126
-
127
- // // SDP
128
- // expect(p1SDPMock.mock.calls.length).toBe(1);
129
- // expect(p2SDPMock.mock.calls.length).toBe(1);
130
-
131
- // // Candidates
132
- // expect(p1CandidateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
133
- // expect(p2CandidateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
134
-
135
- // // DataChannel
136
- // expect(p1DCMock.mock.calls.length).toBe(1);
137
- // expect(p1DCMessageMock.mock.calls.length).toBe(1);
138
- // expect(p1DCMessageMock.mock.calls[0][0]).toEqual("Hello From Peer2");
139
- // expect(p2DCMock.mock.calls.length).toBe(1);
140
- // // These 2 tests fails randomly
141
- // // Needs investigation ?
142
- // // expect(p2DCMessageMock.mock.calls.length).toBe(1);
143
- // // expect(p2DCMessageMock.mock.calls[0][0]).toEqual("Hello From Peer1");
144
-
145
- // done();
146
- // }, 20 * 1000);
147
-
148
- // });
149
- // });
38
+ describe('P2P', () => {
39
+ // Default is 5000 ms but we need more
40
+ jest.setTimeout(30000);
41
+
42
+ let peer1 = new nodeDataChannel.PeerConnection("Peer1", { iceServers: ["stun:stun.l.google.com:19302"] });
43
+ let peer2 = new nodeDataChannel.PeerConnection("Peer2", { iceServers: ["stun:stun.l.google.com:19302"] });
44
+ let dc1 = null;
45
+ let dc2 = null;
46
+
47
+ // Mocks
48
+ const p1StateMock = jest.fn();
49
+ const p1GatheringStateMock = jest.fn();
50
+ const p2StateMock = jest.fn();
51
+ const p2GatheringStateMock = jest.fn();
52
+ const p1SDPMock = jest.fn();
53
+ const p1CandidateMock = jest.fn();
54
+ const p2SDPMock = jest.fn();
55
+ const p2CandidateMock = jest.fn();
56
+
57
+ const p1DCMock = jest.fn();
58
+ const p1DCMessageMock = jest.fn();
59
+ const p2DCMock = jest.fn();
60
+ const p2DCMessageMock = jest.fn();
61
+
62
+ // Set Callbacks
63
+ peer1.onStateChange(p1StateMock);
64
+ peer1.onGatheringStateChange(p1GatheringStateMock);
65
+ peer1.onLocalDescription((sdp, type) => {
66
+ p1SDPMock();
67
+ peer2.setRemoteDescription(sdp, type);
68
+ });
69
+ peer1.onLocalCandidate((candidate, mid) => {
70
+ p1CandidateMock();
71
+ peer2.addRemoteCandidate(candidate, mid);
72
+ });
73
+
74
+ // Set Callbacks
75
+ peer2.onStateChange(p2StateMock);
76
+ peer2.onGatheringStateChange(p2GatheringStateMock);
77
+ peer2.onLocalDescription((sdp, type) => {
78
+ p2SDPMock();
79
+ peer1.setRemoteDescription(sdp, type);
80
+ });
81
+ peer2.onLocalCandidate((candidate, mid) => {
82
+ p2CandidateMock();
83
+ peer1.addRemoteCandidate(candidate, mid);
84
+ });
85
+ peer2.onDataChannel((dc) => {
86
+ p2DCMock();
87
+ dc2 = dc;
88
+ dc2.onMessage((msg) => {
89
+ p2DCMessageMock(msg);
90
+ });
91
+ dc2.sendMessage("Hello From Peer2");
92
+ });
93
+
94
+ dc1 = peer1.createDataChannel("test-p2p");
95
+ dc1.onOpen(() => {
96
+ p1DCMock();
97
+ dc1.sendMessage("Hello From Peer1");
98
+ });
99
+ dc1.onMessage((msg) => {
100
+ p1DCMessageMock(msg);
101
+ });
102
+
103
+ test('P2P', (done) => {
104
+ setTimeout(() => {
105
+ dc1.close();
106
+ dc2.close();
107
+ peer1.close();
108
+ peer2.close();
109
+
110
+ // Fee memory
111
+ dc1 = null;
112
+ dc2 = null;
113
+ peer1 = null;
114
+ peer2 = null;
115
+
116
+ // Cleanup Threads
117
+ nodeDataChannel.cleanup();
118
+ }, 10 * 1000);
119
+
120
+ setTimeout(() => {
121
+ // State Callbacks
122
+ expect(p1StateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
123
+ expect(p1GatheringStateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
124
+ expect(p2StateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
125
+ expect(p2GatheringStateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
126
+
127
+ // SDP
128
+ expect(p1SDPMock.mock.calls.length).toBe(1);
129
+ expect(p2SDPMock.mock.calls.length).toBe(1);
130
+
131
+ // Candidates
132
+ expect(p1CandidateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
133
+ expect(p2CandidateMock.mock.calls.length).toBeGreaterThanOrEqual(1);
134
+
135
+ // DataChannel
136
+ expect(p1DCMock.mock.calls.length).toBe(1);
137
+ expect(p1DCMessageMock.mock.calls.length).toBe(1);
138
+ expect(p1DCMessageMock.mock.calls[0][0]).toEqual("Hello From Peer2");
139
+ expect(p2DCMock.mock.calls.length).toBe(1);
140
+
141
+ expect(p2DCMessageMock.mock.calls.length).toBe(1);
142
+ expect(p2DCMessageMock.mock.calls[0][0]).toEqual("Hello From Peer1");
143
+
144
+ done();
145
+ }, 12 * 1000);
146
+
147
+ });
148
+ });