/
app.test.js
494 lines (418 loc) · 12.9 KB
/
app.test.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import test from 'ava'
import path from 'path'
import grpc from 'grpc'
import hl from 'highland'
import async from 'async'
import _ from 'lodash'
import isCI from 'is-ci'
import Mali from '../'
import * as tu from './util'
const pl = require('@grpc/proto-loader')
const ARRAY_DATA = [
{ message: '1 foo' },
{ message: '2 bar' },
{ message: '3 asd' },
{ message: '4 qwe' },
{ message: '5 rty' },
{ message: '6 zxc' }
]
function getArrayData () {
return _.cloneDeep(ARRAY_DATA)
}
test('should set development env when NODE_ENV missing', t => {
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
const NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = ''
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
process.env.NODE_ENV = NODE_ENV
t.is(app.env, 'development')
})
test('app.inspect should return app properties', t => {
const util = require('util')
const NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = ''
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
const app = new Mali(PROTO_PATH, 'Greeter')
app.foo = 'bar'
t.truthy(app)
const str = util.inspect(app)
process.env.NODE_ENV = NODE_ENV
t.is('{ ports: [],\n context: Context {},\n env: \'development\',\n name: \'Greeter\',\n foo: \'bar\' }'.replace(/\s/g, ''), str.replace(/\s/g, ''))
})
test.cb('app.start() with a default port from OS when no params given', t => {
t.plan(5)
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start()
t.truthy(server)
const ports = app.ports
t.truthy(ports)
t.is(ports.length, 1)
t.true(typeof ports[0] === 'number')
app.close().then(() => t.end())
})
test.cb('app.start() should start and not throw with incomplete API', t => {
t.plan(5)
const PROTO_PATH = path.resolve(__dirname, './protos/transform.proto')
function upper (ctx) {
ctx.res = { message: ctx.req.message.toUpperCase() }
}
const app = new Mali(PROTO_PATH, 'TransformService')
t.truthy(app)
app.use({ upper })
const server = app.start()
t.truthy(server)
const ports = app.ports
t.truthy(ports)
t.is(ports.length, 1)
t.true(typeof ports[0] === 'number')
app.close().then(() => t.end())
})
test.cb('app.start() with a default port from OS with object param', t => {
t.plan(5)
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start(grpc.ServerCredentials.createInsecure())
t.truthy(server)
const ports = app.ports
t.truthy(ports)
t.is(ports.length, 1)
t.true(typeof ports[0] === 'number')
app.close().then(() => t.end())
})
test.cb('app.start() with a default port from OS with "127.0.0.1:0"', t => {
t.plan(5)
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start('127.0.0.1:0')
t.truthy(server)
const ports = app.ports
t.truthy(ports)
t.is(ports.length, 1)
t.true(typeof ports[0] === 'number')
app.close().then(() => t.end())
})
test.cb('app.start() with a default port from OS with ""', t => {
t.plan(5)
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start('')
t.truthy(server)
const ports = app.ports
t.truthy(ports)
t.is(ports.length, 1)
t.true(typeof ports[0] === 'number')
app.close().then(() => t.end())
})
test.cb('app.start() with param', t => {
t.plan(5)
const PORT = tu.getPort()
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start(`127.0.0.1:0${PORT}`)
t.truthy(server)
const ports = app.ports
t.truthy(ports)
t.is(ports.length, 1)
t.is(ports[0], PORT)
app.close().then(() => t.end())
})
test.cb('app.start() with port param and invalid creds', t => {
t.plan(5)
const PORT = tu.getPort()
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start(`127.0.0.1:0${PORT}`, 'foo')
t.truthy(server)
const ports = app.ports
t.truthy(ports)
t.is(ports.length, 1)
t.is(ports[0], PORT)
app.close().then(() => t.end())
})
test.cb('app.start() should throw when binding to taken port', t => {
// workround for https://github.com/travis-ci/travis-ci/issues/9918
if (isCI) {
t.pass()
t.end()
return
}
t.plan(3)
function sayHello (ctx) {
ctx.res = { message: `Hello ${ctx.req.name}!` }
}
const app = new Mali({ file: 'protos/multipkg.proto', root: __dirname })
const port = tu.getHost()
app.use({ sayHello })
const server = app.start(port)
t.truthy(server)
const app2 = new Mali({ file: 'protos/multipkg.proto', root: __dirname })
app2.use({ sayHello })
const error = t.throws(() => {
app2.start(`0.0.0.0:${app.ports[0]}`)
}, Error)
console.log(error)
t.is(error.message, `Failed to bind to port: ${port}`)
app.close().then(() => t.end())
})
test.cb('should handle req/res request', t => {
t.plan(5)
const APP_HOST = tu.getHost()
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start(APP_HOST)
t.truthy(server)
const pd = pl.loadSync(PROTO_PATH)
const helloproto = grpc.loadPackageDefinition(pd).helloworld
const client = new helloproto.Greeter(APP_HOST, grpc.credentials.createInsecure())
client.sayHello({ name: 'Bob' }, (err, response) => {
t.falsy(err)
t.truthy(response)
t.is(response.message, 'Hello Bob')
app.close().then(() => t.end())
})
})
test.cb('should handle req/res request where res is a promise', t => {
t.plan(5)
const APP_HOST = tu.getHost()
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ message: 'Hello ' + ctx.req.name })
}, 40)
})
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server = app.start(APP_HOST)
t.truthy(server)
const pd = pl.loadSync(PROTO_PATH)
const helloproto = grpc.loadPackageDefinition(pd).helloworld
const client = new helloproto.Greeter(APP_HOST, grpc.credentials.createInsecure())
client.sayHello({ name: 'Jim' }, (err, response) => {
t.falsy(err)
t.truthy(response)
t.is(response.message, 'Hello Jim')
app.close().then(() => t.end())
})
})
test.cb('should handle res stream request', t => {
t.plan(3)
const APP_HOST = tu.getHost()
const PROTO_PATH = path.resolve(__dirname, './protos/resstream.proto')
function listStuff (ctx) {
ctx.res = hl(getArrayData())
.map(d => {
d.message = d.message.toUpperCase()
return d
})
}
const app = new Mali(PROTO_PATH, 'ArgService')
t.truthy(app)
app.use({ listStuff })
const server = app.start(APP_HOST)
t.truthy(server)
const pd = pl.loadSync(PROTO_PATH)
const proto = grpc.loadPackageDefinition(pd).argservice
const client = new proto.ArgService(APP_HOST, grpc.credentials.createInsecure())
const call = client.listStuff({ message: 'Hello' })
const resData = []
call.on('data', d => {
resData.push(d.message)
})
call.on('end', () => {
_.delay(() => {
endTest()
}, 200)
})
function endTest () {
t.deepEqual(resData, ['1 FOO', '2 BAR', '3 ASD', '4 QWE', '5 RTY', '6 ZXC'])
app.close().then(() => t.end())
}
})
test.cb('should handle req stream app', t => {
t.plan(6)
const APP_HOST = tu.getHost()
const PROTO_PATH = path.resolve(__dirname, './protos/reqstream.proto')
async function doWork (inputStream) {
return new Promise((resolve, reject) => {
hl(inputStream)
.map(d => {
return d.message.toUpperCase()
})
.collect()
.toCallback((err, r) => {
if (err) {
return reject(err)
}
resolve({
message: r.join(':')
})
})
})
}
async function writeStuff (ctx) {
ctx.res = await doWork(ctx.req)
}
const app = new Mali(PROTO_PATH, 'ArgService')
t.truthy(app)
app.use({ writeStuff })
const server = app.start(APP_HOST)
t.truthy(server)
const pd = pl.loadSync(PROTO_PATH)
const proto = grpc.loadPackageDefinition(pd).argservice
const client = new proto.ArgService(APP_HOST, grpc.credentials.createInsecure())
const call = client.writeStuff((err, res) => {
t.falsy(err)
t.truthy(res)
t.truthy(res.message)
t.is(res.message, '1 FOO:2 BAR:3 ASD:4 QWE:5 RTY:6 ZXC')
app.close().then(() => t.end())
})
async.eachSeries(getArrayData(), (d, asfn) => {
call.write(d)
_.delay(asfn, _.random(10, 50))
}, () => {
call.end()
})
})
test.cb('should handle duplex call', t => {
t.plan(3)
const APP_HOST = tu.getHost()
const PROTO_PATH = path.resolve(__dirname, './protos/duplex.proto')
async function processStuff (ctx) {
ctx.req.on('data', d => {
ctx.req.pause()
_.delay(() => {
const ret = {
message: d.message.toUpperCase()
}
ctx.res.write(ret)
ctx.req.resume()
}, _.random(50, 150))
})
ctx.req.on('end', () => {
_.delay(() => {
ctx.res.end()
}, 200)
})
}
const app = new Mali(PROTO_PATH, 'ArgService')
t.truthy(app)
app.use({ processStuff })
const server = app.start(APP_HOST)
t.truthy(server)
const pd = pl.loadSync(PROTO_PATH)
const proto = grpc.loadPackageDefinition(pd).argservice
const client = new proto.ArgService(APP_HOST, grpc.credentials.createInsecure())
const call = client.processStuff()
const resData = []
call.on('data', d => {
resData.push(d.message)
})
call.on('end', () => {
endTest()
})
async.eachSeries(getArrayData(), (d, asfn) => {
call.write(d)
_.delay(asfn, _.random(10, 50))
}, () => {
call.end()
})
function endTest () {
t.deepEqual(resData, ['1 FOO', '2 BAR', '3 ASD', '4 QWE', '5 RTY', '6 ZXC'])
app.close().then(() => t.end())
}
})
test.cb('should start multipe servers from same application and handle requests', t => {
t.plan(11)
const APP_HOST1 = tu.getHost()
const APP_HOST2 = tu.getHost()
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
function sayHello (ctx) {
ctx.res = { message: 'Hello ' + ctx.req.name }
}
const app = new Mali(PROTO_PATH, 'Greeter')
t.truthy(app)
app.use({ sayHello })
const server1 = app.start(APP_HOST1)
const server2 = app.start(APP_HOST2)
t.truthy(server1)
t.truthy(server2)
t.truthy(Array.isArray(app.servers))
t.is(app.servers.length, 2)
t.is(app.ports.length, 2)
const pd = pl.loadSync(PROTO_PATH)
const helloproto = grpc.loadPackageDefinition(pd).helloworld
const client = new helloproto.Greeter(APP_HOST1, grpc.credentials.createInsecure())
const client2 = new helloproto.Greeter(APP_HOST2, grpc.credentials.createInsecure())
async.parallel({
req1: aecb => client.sayHello({ name: 'Bob' }, aecb),
req2: aecb => client2.sayHello({ name: 'Kate' }, aecb)
}, (err, results) => {
t.falsy(err)
t.truthy(results.req1)
t.is(results.req1.message, 'Hello Bob')
t.truthy(results.req2)
t.is(results.req2.message, 'Hello Kate')
app.close().then(() => t.end())
})
})
test.cb('should work with multi package proto', t => {
t.plan(4)
function sayHello (ctx) {
ctx.res = { message: `Hello ${ctx.req.name}!` }
}
const app = new Mali({ file: 'protos/multipkg.proto', root: __dirname })
const port = tu.getHost()
app.use({ sayHello })
const server = app.start(port)
t.truthy(server)
const pd = pl.loadSync('protos/multipkg.proto', { includeDirs: [__dirname] })
const greet = grpc.loadPackageDefinition(pd).greet
const client = new greet.Greeter(port, grpc.credentials.createInsecure())
client.sayHello({ name: 'Kate' }, (err, response) => {
t.falsy(err)
t.truthy(response)
t.is(response.message, 'Hello Kate!')
app.close().then(() => t.end())
})
})