-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
WebSocketTest.js
153 lines (131 loc) · 3.4 KB
/
WebSocketTest.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
150
151
152
153
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const React = require('react');
const ReactNative = require('react-native');
const {View} = ReactNative;
const {TestModule} = ReactNative.NativeModules;
const DEFAULT_WS_URL = 'ws://localhost:5555/';
const WS_EVENTS = ['close', 'error', 'message', 'open'];
type State = {
url: string,
fetchStatus: ?string,
socket: ?WebSocket,
socketState: ?number,
lastSocketEvent: ?string,
lastMessage: ?string | ?ArrayBuffer,
testMessage: string,
testExpectedResponse: string,
...
};
class WebSocketTest extends React.Component<{...}, State> {
state: State = {
url: DEFAULT_WS_URL,
fetchStatus: null,
socket: null,
socketState: null,
lastSocketEvent: null,
lastMessage: null,
testMessage: 'testMessage',
testExpectedResponse: 'testMessage_response',
};
_waitFor = (condition: any, timeout: any, callback: any) => {
let remaining = timeout;
const timeoutFunction = function () {
if (condition()) {
callback(true);
return;
}
remaining--;
if (remaining === 0) {
callback(false);
} else {
setTimeout(timeoutFunction, 1000);
}
};
setTimeout(timeoutFunction, 1000);
};
_connect = () => {
const socket = new WebSocket(this.state.url);
WS_EVENTS.forEach(ev => socket.addEventListener(ev, this._onSocketEvent));
this.setState({
socket,
socketState: socket.readyState,
});
};
_socketIsConnected = () => {
return this.state.socketState === 1; //'OPEN'
};
_socketIsDisconnected = () => {
return this.state.socketState === 3; //'CLOSED'
};
_disconnect = () => {
if (!this.state.socket) {
return;
}
this.state.socket.close();
};
_onSocketEvent = (event: any) => {
const state: any = {
socketState: event.target.readyState,
lastSocketEvent: event.type,
};
if (event.type === 'message') {
state.lastMessage = event.data;
}
this.setState(state);
};
_sendText = (text: string) => {
if (!this.state.socket) {
return;
}
this.state.socket.send(text);
};
_sendTestMessage = () => {
this._sendText(this.state.testMessage);
};
_receivedTestExpectedResponse = () => {
return this.state.lastMessage === this.state.testExpectedResponse;
};
componentDidMount() {
this.testConnect();
}
testConnect: () => void = () => {
this._connect();
this._waitFor(this._socketIsConnected, 5, connectSucceeded => {
if (!connectSucceeded) {
TestModule.markTestPassed(false);
return;
}
this.testSendAndReceive();
});
};
testSendAndReceive: () => void = () => {
this._sendTestMessage();
this._waitFor(this._receivedTestExpectedResponse, 5, messageReceived => {
if (!messageReceived) {
TestModule.markTestPassed(false);
return;
}
this.testDisconnect();
});
};
testDisconnect: () => void = () => {
this._disconnect();
this._waitFor(this._socketIsDisconnected, 5, disconnectSucceeded => {
TestModule.markTestPassed(disconnectSucceeded);
});
};
render(): React.Node {
return <View />;
}
}
WebSocketTest.displayName = 'WebSocketTest';
module.exports = WebSocketTest;