Close server and connections after each spec

This commit is contained in:
Kevin Sawicki 2017-04-19 09:48:32 -07:00
parent ece4df0ac7
commit 4d9cdad37a

View file

@ -33,8 +33,10 @@ describe('crashReporter module', function () {
const generateSpecs = (description, browserWindowOpts) => { const generateSpecs = (description, browserWindowOpts) => {
describe(description, function () { describe(description, function () {
var w = null var w = null
var stopServer = null
beforeEach(function () { beforeEach(function () {
stopServer = null
w = new BrowserWindow(Object.assign({ w = new BrowserWindow(Object.assign({
show: false show: false
}, browserWindowOpts)) }, browserWindowOpts))
@ -44,13 +46,19 @@ describe('crashReporter module', function () {
return closeWindow(w).then(function () { w = null }) return closeWindow(w).then(function () { w = null })
}) })
afterEach(function (done) {
if (stopServer != null) {
stopServer(done)
}
})
it('should send minidump when renderer crashes', function (done) { it('should send minidump when renderer crashes', function (done) {
if (process.env.APPVEYOR === 'True') return done() if (process.env.APPVEYOR === 'True') return done()
if (process.env.TRAVIS === 'true') return done() if (process.env.TRAVIS === 'true') return done()
this.timeout(120000) this.timeout(120000)
startServer({ stopServer = startServer({
callback (port) { callback (port) {
const crashUrl = url.format({ const crashUrl = url.format({
protocol: 'file', protocol: 'file',
@ -70,7 +78,7 @@ describe('crashReporter module', function () {
this.timeout(120000) this.timeout(120000)
startServer({ stopServer = startServer({
callback (port) { callback (port) {
const crashesDir = path.join(app.getPath('temp'), `${app.getName()} Crashes`) const crashesDir = path.join(app.getPath('temp'), `${app.getName()} Crashes`)
const version = app.getVersion() const version = app.getVersion()
@ -85,7 +93,6 @@ describe('crashReporter module', function () {
it('should not send minidump if uploadToServer is false', function (done) { it('should not send minidump if uploadToServer is false', function (done) {
this.timeout(120000) this.timeout(120000)
let server
let dumpFile let dumpFile
let crashesDir = crashReporter.getCrashesDirectory() let crashesDir = crashReporter.getCrashesDirectory()
const existingDumpFiles = new Set() const existingDumpFiles = new Set()
@ -98,7 +105,6 @@ describe('crashReporter module', function () {
if (uploaded) { if (uploaded) {
return done(new Error('fail')) return done(new Error('fail'))
} }
server.close()
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
crashReporter.setUploadToServer(true) crashReporter.setUploadToServer(true)
} }
@ -139,7 +145,7 @@ describe('crashReporter module', function () {
}) })
}) })
server = startServer({ stopServer = startServer({
callback (port) { callback (port) {
const crashUrl = url.format({ const crashUrl = url.format({
protocol: 'file', protocol: 'file',
@ -159,7 +165,7 @@ describe('crashReporter module', function () {
this.timeout(120000) this.timeout(120000)
startServer({ stopServer = startServer({
callback (port) { callback (port) {
const crashUrl = url.format({ const crashUrl = url.format({
protocol: 'file', protocol: 'file',
@ -254,7 +260,6 @@ const waitForCrashReport = () => {
const startServer = ({callback, processType, done}) => { const startServer = ({callback, processType, done}) => {
var called = false var called = false
var server = http.createServer((req, res) => { var server = http.createServer((req, res) => {
server.close()
var form = new multiparty.Form() var form = new multiparty.Form()
form.parse(req, (error, fields) => { form.parse(req, (error, fields) => {
if (error) throw error if (error) throw error
@ -283,6 +288,15 @@ const startServer = ({callback, processType, done}) => {
}) })
}) })
}) })
const activeConnections = new Set()
server.on('connection', (connection) => {
activeConnections.add(connection)
connection.once('close', () => {
activeConnections.delete(connection)
})
})
let {port} = remote.process let {port} = remote.process
server.listen(port, '127.0.0.1', () => { server.listen(port, '127.0.0.1', () => {
port = server.address().port port = server.address().port
@ -295,5 +309,13 @@ const startServer = ({callback, processType, done}) => {
} }
callback(port) callback(port)
}) })
return server
return function stopServer (done) {
for (const connection of activeConnections) {
connection.destroy()
}
server.close(function () {
done()
})
}
} }