Skip to content

Commit

Permalink
add DEBUG_NETWORK_SEND_DELAY for simulating slow network
Browse files Browse the repository at this point in the history
Summary:
It can be a pain to debug slow network issues, especially with the iOS simulator which doesn't have a network link conditioner. This makes it really easy and predictable by simply adding a `setTimeout` around calling `sendRequest`.

Changelog:
[General] [Added] - DEBUG_NETWORK_SEND_DELAY can be used to simulate slow network.

Reviewed By: PeteTheHeat

Differential Revision: D19236911

fbshipit-source-id: 14762c7e0c6408a8364aa569c482729a7a1fe740
  • Loading branch information
sahrens authored and facebook-github-bot committed Jan 2, 2020
1 parent 674b591 commit 4aac019
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions Libraries/Network/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const base64 = require('base64-js');
const invariant = require('invariant');
const warning = require('fbjs/lib/warning');

const DEBUG_NETWORK_SEND_DELAY: false = false; // Set to a number of milliseconds when debugging

export type NativeResponseType = 'base64' | 'blob' | 'text';
export type ResponseType =
| ''
Expand Down Expand Up @@ -511,22 +513,29 @@ class XMLHttpRequest extends (EventTarget(...XHR_EVENTS): any) {
nativeResponseType = 'blob';
}

invariant(this._method, 'Request method needs to be defined.');
invariant(this._url, 'Request URL needs to be defined.');
RCTNetworking.sendRequest(
this._method,
this._trackingName,
this._url,
this._headers,
data,
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
* when making Flow check .android.js files. */
nativeResponseType,
incrementalEvents,
this.timeout,
this.__didCreateRequest.bind(this),
this.withCredentials,
);
const doSend = () => {
invariant(this._method, 'Request method needs to be defined.');
invariant(this._url, 'Request URL needs to be defined.');
RCTNetworking.sendRequest(
this._method,
this._trackingName,
this._url,
this._headers,
data,
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
* when making Flow check .android.js files. */
nativeResponseType,
incrementalEvents,
this.timeout,
this.__didCreateRequest.bind(this),
this.withCredentials,
);
};
if (DEBUG_NETWORK_SEND_DELAY) {
setTimeout(doSend, DEBUG_NETWORK_SEND_DELAY);
} else {
doSend();
}
}

abort(): void {
Expand Down

0 comments on commit 4aac019

Please sign in to comment.