Permalink
Browse files

moved test transports to stubs folder so they can be re-used - will b…

…e adding tranport error tests to error.test.js
  • Loading branch information...
1 parent 71775c3 commit dc2c0b25b2399a9330177faf9e5093f396d409ff @rjrodger rjrodger committed Sep 7, 2016
Showing with 158 additions and 3 deletions.
  1. +1 −1 .eslintignore
  2. +146 −0 test/stubs/transports.js
  3. +11 −2 test/transport.test.js
View
@@ -1,4 +1,4 @@
node_modules
docs
-
+tmp
test/stubs/plugin/bad-syntax-plugin.js
@@ -0,0 +1,146 @@
+/* Copyright (c) 2014-2016 Richard Rodger, MIT License */
+'use strict'
+
+var _ = require('lodash')
+var Async = require('async')
+var Util = require('util')
+
+
+exports.make_test_transport = make_test_transport
+exports.make_balance_transport = make_balance_transport
+
+
+// A simple transport that uses async.queue as the transport mechanism
+function make_test_transport () {
+ test_transport.outmsgs = []
+ test_transport.queuemap = {}
+
+ return test_transport
+
+ function test_transport (options) {
+ var seneca = this
+
+ var tu = seneca.export('transport/utils')
+
+ seneca.add({role: 'transport', hook: 'listen', type: 'test'}, hook_listen_test)
+ seneca.add({role: 'transport', hook: 'client', type: 'test'}, hook_client_test)
+
+ function hook_listen_test (args, done) {
+ var seneca = this
+ var type = args.type
+ var listen_options = seneca.util.clean(_.extend({}, options[type], args))
+
+ tu.listen_topics(seneca, args, listen_options, function (topic) {
+ seneca.log.debug('listen', 'subscribe', topic + '_act',
+ listen_options, seneca)
+
+ test_transport.queuemap[topic + '_act'] = Async.queue(function (data, done) {
+ tu.handle_request(seneca, data, listen_options, function (out) {
+ if (out == null) return done()
+
+ test_transport.outmsgs.push(out)
+
+ test_transport.queuemap[topic + '_res'].push(out)
+ return done()
+ })
+ })
+ })
+
+ seneca.add('role:seneca,cmd:close', function (close_args, done) {
+ var closer = this
+ closer.prior(close_args, done)
+ })
+
+ seneca.log.info('listen', 'open', listen_options, seneca)
+
+ done()
+ }
+
+ function hook_client_test (args, clientdone) {
+ var seneca = this
+ var type = args.type
+ var client_options = seneca.util.clean(_.extend({}, options[type], args))
+
+ tu.make_client(make_send, client_options, clientdone)
+
+ function make_send (spec, topic, send_done) {
+ seneca.log.debug('client', 'subscribe', topic + '_res', client_options, seneca)
+
+ test_transport.queuemap[topic + '_res'] = Async.queue(function (data, done) {
+ tu.handle_response(seneca, data, client_options)
+ return done()
+ })
+
+ send_done(null, function (args, done) {
+ if (!test_transport.queuemap[topic + '_act']) {
+ return done(new Error('Unknown topic:' + topic +
+ ' for: ' + Util.inspect(args)))
+ }
+ var outmsg = tu.prepare_request(seneca, args, done)
+ test_transport.queuemap[topic + '_act'].push(outmsg)
+ })
+ }
+
+ seneca.add('role:seneca,cmd:close', function (close_args, done) {
+ var closer = this
+ closer.prior(close_args, done)
+ })
+ }
+ }
+}
+
+
+// A simple load balancing transport
+function make_balance_transport () {
+ var targets = []
+
+ test_transport.preload = function () {
+ this.options({
+ transport: {
+ balance: {
+ makehandle: function () {
+ return function (pat, action) {
+ targets.push(action)
+ }
+ }
+ }
+ }
+ })
+ }
+
+ return test_transport
+
+ function test_transport (options) {
+ var seneca = this
+
+ var tu = seneca.export('transport/utils')
+
+ seneca.add({
+ role: 'transport', hook: 'client', type: 'balance'
+ }, hook_client_test)
+
+ function hook_client_test (args, clientdone) {
+ var seneca = this
+ var type = args.type
+ var client_options = seneca.util.clean(_.extend({}, options[type], args))
+
+ tu.make_client(make_send, client_options, clientdone)
+
+ var index = -1
+
+ function make_send (spec, topic, send_done) {
+ seneca.log.debug('client', 'send', topic + '_res', client_options, seneca)
+
+ send_done(null, function (args, done) {
+ index = (index + 1) % targets.length
+ targets[index].call(this, args, done)
+ })
+ }
+
+ seneca.add('role:seneca,cmd:close', function (close_args, done) {
+ var closer = this
+ closer.prior(close_args, done)
+ })
+ }
+ }
+}
@@ -1,21 +1,28 @@
/* Copyright (c) 2014-2016 Richard Rodger, MIT License */
'use strict'
-var Util = require('util')
var _ = require('lodash')
var Async = require('async')
var Code = require('code')
+var Lab = require('lab')
+
var Seneca = require('..')
var Common = require('../lib/common')
var Transport = require('../lib/transport')
-var Lab = require('lab')
+var TransportStubs = require('./stubs/transports')
+
// Test shortcuts
var lab = exports.lab = Lab.script()
var describe = lab.describe
var it = lab.it
var expect = Code.expect
+
+var make_test_transport = TransportStubs.make_test_transport
+var make_balance_transport = TransportStubs.make_balance_transport
+
+
function testact (args, done) {
var seneca = this
setTimeout(function () {
@@ -967,6 +974,7 @@ describe('transport', function () {
*/
})
+/*
// A simple transport that uses async.queue as the transport mechanism
function make_test_transport () {
test_transport.outmsgs = []
@@ -1101,3 +1109,4 @@ function make_balance_transport () {
}
}
}
+*/

0 comments on commit dc2c0b2

Please sign in to comment.