This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
96 lines (76 loc) · 2.76 KB
/
index.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
'use strict'
const { spawn } = require('child_process')
const rimraf = require('rimraf')
const mkdirp = require('mkdirp')
const getPortOffset = pid => {
const maxOffset = 65535 - 28015
return pid - (Math.floor(pid / maxOffset) * maxOffset)
}
const getStorageDir = (cwd, pid) => `${cwd}/.db-test-${pid}`
let rethink
module.exports.init = initialData => t => new Promise((resolve, reject) => {
const offset = getPortOffset(process.pid)
const port = 28015 + offset
const r = require('rethinkdb')
const dir = getStorageDir(process.cwd(), process.pid)
r.net.Connection.prototype.DEFAULT_PORT = port
mkdirp.sync(dir)
rethink = spawn('rethinkdb', ['-o', `${offset}`, '-d', `${dir}/${port}`])
if (process.env.AVA_RETHINKDB_DEBUG) {
console.error(`==> Process ${process.pid} spawning RethinkDB server on port ${port}...`)
}
rethink.stdout.on('data', chunk => {
if (chunk.toString('utf8').startsWith('Listening for client driver connections')) {
if (initialData) {
importData().then(() => resolve(port))
} else {
resolve(port)
}
if (process.env.AVA_RETHINKDB_DEBUG) {
console.error(`==> Process ${process.pid} RethinkDB server booted!`)
}
}
if (process.env.AVA_RETHINKDB_DEBUG) {
console.error(chunk.toString())
}
})
if (process.env.AVA_RETHINKDB_DEBUG) {
rethink.stderr.on('data', chunk => console.error(chunk.toString()))
}
rethink.on('error', reject)
function importData () {
let conn
return r.connect({}).then(_ => { conn = _ })
.then(() => Promise.all(
Object.keys(initialData).map(db => db !== 'test' && r.dbCreate(db).run(conn))
))
.then(() => Promise.all(
collectTables(initialData).map(([db, table]) => r.db(db).tableCreate(table).run(conn))
))
.then(() => Promise.all(
collectDocuments(initialData).map(([db, table, doc]) => r.db(db).table(table).insert(doc).run(conn))
))
}
})
module.exports.cleanup = () => {
if (process.env.AVA_RETHINKDB_DEBUG) {
console.error(`==> Process ${process.pid} killing RethinkDB server...`)
}
rethink.kill()
if (process.env.AVA_RETHINKDB_DEBUG) {
console.error(`==> Process ${process.pid} killed RethinkDB server!`)
}
rimraf.sync(getStorageDir(process.cwd(), process.pid))
}
function collectTables (data) {
return Object.keys(data)
.map(db => Object.keys(data[db]).map(table => [db, table]))
.reduce((a, b) => a.concat(b))
}
function collectDocuments (data) {
return Object.keys(data)
.map(db => Object.keys(data[db]).map(table => data[db][table].map(doc => [db, table, doc])))
.reduce((a, b) => a.concat(b))
.reduce((a, b) => a.concat(b))
}
module.exports.getPortOffset = getPortOffset