Skip to content

Commit

Permalink
senecajs#139: Add inward/outward support on TCP / HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromevalentin committed Oct 31, 2016
1 parent 0b13474 commit 27fa44c
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 8 deletions.
25 changes: 22 additions & 3 deletions lib/transport-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ internals.Utils.prototype.handle_response = function (seneca, data, client_optio
return false
}


var actinfo = {
id: data.id,
accept: data.accept,
Expand Down Expand Up @@ -234,14 +233,34 @@ internals.Utils.prototype.handle_request = function (seneca, data, listen_option

input.id$ = data.id

this.requestAct(seneca, input, output, respond)
this.requestAct(seneca, input, output, respond, listen_options.inward, listen_options.outward)
}

internals.Utils.prototype.requestAct = function (seneca, input, output, respond) {
internals.Utils.prototype.requestAct = function (seneca, input, output, respond, inward, outward) {
var self = this

try {
if (inward) {
inward({ seneca }, { msg: input })
}
seneca.act(input, function (err, out) {
if (outward) {
try {
var outward_data = {
err: err,
msg: input,
res: out
}
outward({ seneca }, outward_data)
err = outward_data.err
out = outward_data.res
}
catch (e) {
// outward failed, keep a trace of the original err if there was one
e.act_error = err
err = e
}
}
self.update_output(input, output, err, out)
respond(output)
})
Expand Down
101 changes: 100 additions & 1 deletion test/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ describe('Specific http', function () {
})
})


it('http-query', function (fin) {
CreateInstance({errhandler: fin})
.add('a:1', function (args, done) {
Expand Down Expand Up @@ -193,6 +192,106 @@ describe('Specific http', function () {
})
})
})

it('apply inward/outward listen options on HTTP remote act call', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(null, { BAR: args.bar })
})
.listen({type: 'http', port: '18997',
inward: (context, data) => {
data.msg.bar += 1
data.msg.inward = 'INPUT UPGRADED'
},
outward: (context, data) => {
data.res.BAR += 10
data.res.inward = data.msg.inward
data.res.outward = 'OUTPUT UPGRADED'
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'http', port: '18997'})

siClient.act('foo:1,bar:2', function (err, out) {
if (err) return fin(err)
Assert.equal(out.BAR, 13)
Assert.equal(out.inward, 'INPUT UPGRADED')
Assert.equal(out.outward, 'OUTPUT UPGRADED')
fin()
})
})
})

it('reject HTTP remote act call in inward listen option', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(null, { BAR: args.bar })
})
.listen({type: 'http', port: '18996',
inward: (context, data) => {
var e = new Error('HTTP inward rejected!')
e.error_code = 'inward_rejected'
throw e
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'http', port: '18996'})

siClient.act('foo:1,bar:2', function (err, out) {
Assert.equal(err.error_code, 'inward_rejected')
if (err) return fin()
fin(new Error('Inward does not reject remote call'))
})
})
})

it('reject HTTP remote act call in outward listen option', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(null, { BAR: args.bar })
})
.listen({type: 'http', port: '18995',
outward: (context, data) => {
var e = new Error('HTTP outward rejected!')
e.error_code = 'outward_rejected'
throw e
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'http', port: '18995'})

siClient.act('foo:1,bar:2', function (err, out) {
Assert.equal(err.error_code, 'outward_rejected')
if (err) return fin()
fin(new Error('Outward does not reject remote call'))
})
})
})

it('catch HTTP remote act call error in outward listen option', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(new Error('Catchable failure'), {BAR: args.bar})
})
.listen({type: 'http', port: '18994',
outward: (context, data) => {
delete data.err
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'http', port: '18994'})

siClient.act('foo:1,bar:2', function (err, out) {
if (err) return fin(err)
Assert.equal(out.BAR, 2)
fin()
})
})
})
})

describe('Specific https', function () {
Expand Down
4 changes: 1 addition & 3 deletions test/misc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('Miscellaneous', function () {
counters.own++
}


var a = CreateInstance({
log: {map: [
{level: 'debug', regex: /\{a:1\}/, handler: log_a},
Expand All @@ -66,7 +65,6 @@ describe('Miscellaneous', function () {
.listen({type: type, port: 40406})
.client({type: type, port: 40405})


a.ready(function () {
b.ready(function () {
a.act('a:1', function (err, out) {
Expand Down Expand Up @@ -193,7 +191,6 @@ describe('Miscellaneous', function () {
.listen({type: type, port: 40407})
.client({type: type, port: 40405})


a.ready(function () {
b.ready(function () {
c.ready(function () {
Expand Down Expand Up @@ -375,6 +372,7 @@ describe('Miscellaneous', function () {
})
})
})

it('listen-http-pin (#97)', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
Expand Down
101 changes: 100 additions & 1 deletion test/tcp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ describe('Specific tcp', function () {

var settings = {tcp: {port: 0, host: 'localhost'}}


var transportUtil = new TransportUtil({
callmap: {},
seneca: seneca,
Expand Down Expand Up @@ -215,5 +214,105 @@ describe('Specific tcp', function () {

setTimeout(finish, 2000)
})

it('apply inward/outward listen options on TCP remote act call', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(null, { BAR: args.bar })
})
.listen({type: 'tcp', port: '19997',
inward: (context, data) => {
data.msg.bar += 1
data.msg.inward = 'INPUT UPGRADED'
},
outward: (context, data) => {
data.res.BAR += 10
data.res.inward = data.msg.inward
data.res.outward = 'OUTPUT UPGRADED'
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'tcp', port: '19997'})

siClient.act('foo:1,bar:2', function (err, out) {
if (err) return fin(err)
Assert.equal(out.BAR, 13)
Assert.equal(out.inward, 'INPUT UPGRADED')
Assert.equal(out.outward, 'OUTPUT UPGRADED')
fin()
})
})
})

it('reject TCP remote act call in inward listen option', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(null, { BAR: args.bar })
})
.listen({type: 'tcp', port: '19996',
inward: (context, data) => {
var e = new Error('TCP inward rejected!')
e.error_code = 'inward_rejected'
throw e
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'tcp', port: '19996'})

siClient.act('foo:1,bar:2', function (err, out) {
Assert.equal(err.error_code, 'inward_rejected')
if (err) return fin()
fin(new Error('Inward does not reject remote call'))
})
})
})

it('reject TCP remote act call in outward listen option', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(null, { BAR: args.bar })
})
.listen({type: 'tcp', port: '19995',
outward: (context, data) => {
var e = new Error('TCP outward rejected!')
e.error_code = 'outward_rejected'
throw e
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'tcp', port: '19995'})

siClient.act('foo:1,bar:2', function (err, out) {
Assert.equal(err.error_code, 'outward_rejected')
if (err) return fin()
fin(new Error('Outward does not reject remote call'))
})
})
})

it('catch TCP remote act call error in outward listen option', function (fin) {
CreateInstance()
.add('foo:1', function (args, done) {
done(new Error('Catchable failure'), {BAR: args.bar})
})
.listen({type: 'tcp', port: '19994',
outward: (context, data) => {
delete data.err
}
})
.ready(function () {
var siClient = CreateInstance()
.client({type: 'tcp', port: '19994'})

siClient.act('foo:1,bar:2', function (err, out) {
if (err) return fin(err)
Assert.equal(out.BAR, 2)
fin()
})
})
})
})
})

0 comments on commit 27fa44c

Please sign in to comment.