Integration test: offline mode
This commit is contained in:
@@ -151,13 +151,18 @@ function init () {
|
|||||||
|
|
||||||
ipc.on('testOffline', function (e, isOffline) {
|
ipc.on('testOffline', function (e, isOffline) {
|
||||||
log('Testing, network ' + (isOffline ? 'OFFLINE' : 'ONLINE'))
|
log('Testing, network ' + (isOffline ? 'OFFLINE' : 'ONLINE'))
|
||||||
// Get the two Electron BrowserWindows (main UI window, hidden webtorrent window)
|
windows.webtorrent.send('wt-test-offline')
|
||||||
const wins = [windows.main.win, windows.webtorrent.win]
|
|
||||||
wins.forEach(function (win) {
|
// TODO: this easy way to disable networking doesn't work due to a Chrome bug.
|
||||||
if (isOffline) win.webContents.session.enableNetworkEmulation({ latency: 10e3 })
|
// WebRTC is unaffected by DevTools throttling:
|
||||||
else win.webContents.session.disableNetworkEmulation()
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=490143
|
||||||
log('WTF')
|
// Also doesn't work due to an Electron bug. Node networking APIs are unaffected, only
|
||||||
})
|
// Chrome networking (external resources, XHRs, etc) are affected.
|
||||||
|
// const wins = [windows.main.win, windows.webtorrent.win]
|
||||||
|
// wins.forEach(function (win) {
|
||||||
|
// if (isOffline) win.webContents.session.enableNetworkEmulation({ offline: true })
|
||||||
|
// else win.webContents.session.disableNetworkEmulation()
|
||||||
|
// !})
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -54,11 +54,16 @@ const VERSION_STR = VERSION.match(/([0-9]+)/g)
|
|||||||
*/
|
*/
|
||||||
const VERSION_PREFIX = '-WD' + VERSION_STR + '-'
|
const VERSION_PREFIX = '-WD' + VERSION_STR + '-'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an ephemeral peer ID each time.
|
||||||
|
* TODO: once there are around 2^24 = ~8 million WebTorrent Desktops online at the same time,
|
||||||
|
* ID collisions will start happening. Birthday paradox. Can we use more than six bytes?
|
||||||
|
*/
|
||||||
|
const PEER_ID = Buffer.from(VERSION_PREFIX + crypto.randomBytes(6).toString('hex'))
|
||||||
|
|
||||||
// Connect to the WebTorrent and BitTorrent networks. WebTorrent Desktop is a hybrid
|
// Connect to the WebTorrent and BitTorrent networks. WebTorrent Desktop is a hybrid
|
||||||
// client, as explained here: https://webtorrent.io/faq
|
// client, as explained here: https://webtorrent.io/faq
|
||||||
const client = window.client = new WebTorrent({
|
let client = window.client = new WebTorrent({ peerId: PEER_ID })
|
||||||
peerId: Buffer.from(VERSION_PREFIX + crypto.randomBytes(6).toString('hex'))
|
|
||||||
})
|
|
||||||
|
|
||||||
// WebTorrent-to-HTTP streaming sever
|
// WebTorrent-to-HTTP streaming sever
|
||||||
let server = null
|
let server = null
|
||||||
@@ -69,8 +74,7 @@ let prevProgress = null
|
|||||||
init()
|
init()
|
||||||
|
|
||||||
function init () {
|
function init () {
|
||||||
client.on('warning', (err) => ipc.send('wt-warning', null, err.message))
|
listenToClientEvents()
|
||||||
client.on('error', (err) => ipc.send('wt-error', null, err.message))
|
|
||||||
|
|
||||||
ipc.on('wt-start-torrenting', (e, torrentKey, torrentID, path, fileModtimes, selections) =>
|
ipc.on('wt-start-torrenting', (e, torrentKey, torrentID, path, fileModtimes, selections) =>
|
||||||
startTorrenting(torrentKey, torrentID, path, fileModtimes, selections))
|
startTorrenting(torrentKey, torrentID, path, fileModtimes, selections))
|
||||||
@@ -91,6 +95,20 @@ function init () {
|
|||||||
ipc.on('wt-select-files', (e, infoHash, selections) =>
|
ipc.on('wt-select-files', (e, infoHash, selections) =>
|
||||||
selectFiles(infoHash, selections))
|
selectFiles(infoHash, selections))
|
||||||
|
|
||||||
|
// TODO: remove this once the following bugs are fixed:
|
||||||
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=490143
|
||||||
|
// https://github.com/electron/electron/issues/7212
|
||||||
|
ipc.on('wt-test-offline', () => {
|
||||||
|
console.log('Test, going OFFLINE')
|
||||||
|
client = window.client = new WebTorrent({
|
||||||
|
peerId: PEER_ID,
|
||||||
|
tracker: false,
|
||||||
|
dht: false,
|
||||||
|
webSeeds: false
|
||||||
|
})
|
||||||
|
listenToClientEvents()
|
||||||
|
})
|
||||||
|
|
||||||
ipc.send('ipcReadyWebTorrent')
|
ipc.send('ipcReadyWebTorrent')
|
||||||
|
|
||||||
window.addEventListener('error', (e) =>
|
window.addEventListener('error', (e) =>
|
||||||
@@ -101,6 +119,11 @@ function init () {
|
|||||||
console.timeEnd('init')
|
console.timeEnd('init')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function listenToClientEvents () {
|
||||||
|
client.on('warning', (err) => ipc.send('wt-warning', null, err.message))
|
||||||
|
client.on('error', (err) => ipc.send('wt-error', null, err.message))
|
||||||
|
}
|
||||||
|
|
||||||
// Starts a given TorrentID, which can be an infohash, magnet URI, etc.
|
// Starts a given TorrentID, which can be an infohash, magnet URI, etc.
|
||||||
// Returns a WebTorrent object. See https://git.io/vik9M
|
// Returns a WebTorrent object. See https://git.io/vik9M
|
||||||
function startTorrenting (torrentKey, torrentID, path, fileModtimes, selections) {
|
function startTorrenting (torrentKey, torrentID, path, fileModtimes, selections) {
|
||||||
|
|||||||
Reference in New Issue
Block a user