forked from holochain/holochain-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (201 loc) · 6.91 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const binary = require('node-pre-gyp');
const path = require('path');
const tape = require('tape');
// deals with ensuring the correct version for the machine/node version
const binding_path = binary.find(path.resolve(path.join(__dirname, './package.json')));
const { makeInstanceId, makeConfig, TestConductor: Conductor } = require(binding_path);
const promiser = (fulfill, reject) => (err, val) => {
if (err) {
reject(err)
} else {
fulfill(val)
}
}
/////////////////////////////////////////////////////////////
const defaultOpts = {
debugLog: true
}
const Config = {
agent: name => ({ name }),
dna: (path, name) => {
if (!name) {
name = path
}
return { path, name }
},
instance: (agent, dna, name) => {
if (!name) {
name = agent.name
}
return { agent, dna, name }
},
conductor: (instances, opts=defaultOpts) => makeConfig(instances, opts)
}
/////////////////////////////////////////////////////////////
Conductor.prototype._start = Conductor.prototype.start
Conductor.prototype._stop = Conductor.prototype.stop
Conductor.prototype._callRaw = Conductor.prototype.call
// DEPRECATED: use Conductor.run()
Conductor.prototype.start = function () {
this._stopPromise = new Promise((fulfill, reject) => {
try {
this._start(promiser(fulfill, reject))
} catch (e) {
reject(e)
}
})
}
// DEPRECATED: use Conductor.run()
Conductor.prototype.stop = function () {
this._stop()
return this._stopPromise
}
Conductor.prototype.call = function (id, zome, fn, params) {
const stringInput = JSON.stringify(params)
let rawResult
let result
try {
rawResult = this._callRaw(id, zome, fn, stringInput)
} catch (e) {
console.error("Exception occurred while calling zome function: ", e)
throw e
}
try {
result = JSON.parse(rawResult)
} catch (e) {
console.warn("JSON.parse failed to parse the result. The raw value is: ", rawResult)
return rawResult
}
return result
}
Conductor.prototype.callWithPromise = function (...args) {
try {
const promise = new Promise((fulfill, reject) => {
this.register_callback(() => fulfill())
})
const result = this.call(...args)
return [result, promise]
} catch (e) {
return [undefined, Promise.reject(e)]
}
}
Conductor.prototype.callSync = function (...args) {
const [result, promise] = this.callWithPromise(...args)
return promise.then(() => result)
}
// Convenience function for making an object that can call into the conductor
// in the context of a particular instance. This may be temporary.
Conductor.prototype.makeCaller = function (agentId, dnaPath) {
const instanceId = dnaPath ? makeInstanceId(agentId, dnaPath) : agentId
return {
call: (zome, fn, params) => this.call(instanceId, zome, fn, params),
agentId: this.agent_id(instanceId),
dnaAddress: this.dna_address(instanceId),
}
}
// DEPRECATED: use Conductor.run()
Conductor.withInstances = function (instances, opts=defaultOpts) {
const config = Config.conductor(instances, opts)
return new Conductor(config)
}
/**
* Run a new Conductor, specified by a closure:
* (stop, callers, conductor) => { (code to run) }
* where `stop` is a function that shuts down the Conductor and must be called in the closure body
* `opts` is an optional object of configuration
* and the `callers` is an Object of instances specified in the config, keyed by "name"
* (name is the optional third parameter of `Config.instance`)
*
* e.g.:
* Conductor.run([
* instanceAlice,
* instanceBob,
* instanceCarol,
* ], (stop, {alice, bob, carol}) => {
* const resultAlice = alice.call(...)
* const resultBob = bob.call(...)
* assert(resultAlice === resultBob)
* stop()
* })
*/
Conductor.run = function (instances, opts, fn) {
if (typeof opts === 'function') {
fn = opts
opts = {}
}
const conductor = Conductor.withInstances(instances, opts)
return new Promise((fulfill, reject) => {
try {
conductor._start(promiser(fulfill, reject))
const callers = {}
instances.map(inst => {
callers[inst.name] = conductor.makeCaller(inst.name)
})
fn(() => conductor._stop(), callers, conductor)
} catch (e) {
reject(e)
}
})
}
/////////////////////////////////////////////////////////////
class Scenario {
constructor(instances, opts=defaultOpts) {
this.instances = instances
this.opts = opts
}
static setTape(tape) {
Scenario._tape = tape
}
/**
* Run a test case, specified by a closure:
* (stop, {instances}) => { test body }
* where `stop` is a function that ends the test and shuts down the running Conductor
* and the `instances` is an Object of instances specified in the config, keyed by "name"
* (name is the optional third parameter of `Config.instance`)
*
* e.g.:
* scenario.run(async (stop, {alice, bob, carol}) => {
* const resultAlice = await alice.callSync(...)
* const resultBob = await bob.callSync(...)
* assert(resultAlice === resultBob)
* stop()
* })
*/
run(fn) {
return Conductor.run(this.instances, this.opts, (stop, _, conductor) => {
const callers = {}
this.instances.forEach(instance => {
const id = makeInstanceId(instance.agent.name, instance.dna.name)
const name = instance.name
if (name in callers) {
throw `instance with duplicate name '${name}', please give one of these instances a new name,\ne.g. Config.instance(agent, dna, "newName")`
}
callers[name] = {
call: (...args) => conductor.call(name, ...args),
callSync: (...args) => conductor.callSync(name, ...args),
callWithPromise: (...args) => conductor.callWithPromise(name, ...args),
agentId: conductor.agent_id(name),
dnaAddress: conductor.dna_address(name),
}
})
return fn(stop, callers)
})
}
runTape(description, fn) {
if (!Scenario._tape) {
throw new Error("must call `scenario.setTape(require('tape'))` before running tape-based tests!")
}
Scenario._tape(description, t => {
this.run(async (stop, instances) => {
await fn(t, instances)
stop()
})
.catch(e => {
t.fail(e)
})
.then(t.end)
})
}
}
/////////////////////////////////////////////////////////////
module.exports = { Config, Conductor, Scenario };