<commit_before>/* Copyright (c) 2013-2015 <NAME> */
'use strict'


var assert = require('assert')
var Lab = require('lab')
var Seneca = require('seneca')
var Shared   = require('seneca-transport-test')
var Wreck = require('wreck')
var Transport = require('../')


var lab = exports.lab = Lab.script()
var describe = lab.describe
var it = lab.it

var no_t = {transport:false}

process.setMaxListeners(999)


function run_client( type, port, done, tag ) {
  Seneca({tag:tag,log:'silent',default_plugins:no_t,debug:{short_logs:true}})
    .use(Transport)
    .client({ type: type, port: port })
    .ready( function() {

      this.act('c:1,d:A',function(err,out){
        if(err) return fin(err);

        assert.equal( '{"s":"1-A"}', JSON.stringify(out) )

        this.act('c:1,d:AA',function(err,out){
          if(err) return fin(err);

          assert.equal( '{"s":"1-AA"}', JSON.stringify(out) )

          this.close(done)
        })
      })
    })
}

function get_seneca(tag) {
  return Seneca({tag:tag, log:'silent', default_plugins: no_t, debug: {short_logs:true}}).use(Transport)
}

describe('transport', function() {

  Shared.basictest({
    seneca: get_seneca(),
    script: lab,
    type: 'tcp'
  })

  Shared.basicpintest({
    seneca: get_seneca(),
    script: lab,
    type: 'tcp'
  })

  Shared.basictest({
    seneca: get_seneca(),
    script: lab,
    type: 'web'
  })

  Shared.basicpintest({
    seneca: get_seneca(),
    script: lab,
    type: 'web'
  })

  it('tcp-basic', function( fin ) {
    Seneca({tag:'srv',log:'silent',default_plugins:no_t,debug:{short_logs:true}})
      .use('../transport.js')
      .add( 'c:1', function(args,done){done(null,{s:'1-'+args.d})} )
      .listen({type:'tcp',port:20102})
      .ready( function() {
        var seneca = this

        var count = 0
        function check() {
          count++
          if( 3 == count ) {
            seneca.close(fin)
          }
        }

        run_client( 'tcp', 20102, check, 'cln0' )
        run_client( 'tcp', 20102, check, 'cln1' )
        run_client( 'tcp', 20102, check, 'cln2' )
      })
  })

  it('sets correct tx$ properties', function (done) {
    Seneca({ log:'silent', default_plugins: no_t })
      .use(Transport)
      .add({ cmd: 'test' }, function (args, cb) {
        var test = this.make$('test')
        test.name = 'bar'
        test.save$(function (err, result) {
          cb(null, { result: result.toString(), tx: args.tx$ })
        })
      })
      .listen({ type:'tcp', port: 20103 })
      .ready(function () {
        Seneca({ log:'silent', default_plugins: no_t })
        .use(Transport)
        .client({ type: 'tcp', port: 20103 })
        .ready(function() {
          this.act({ cmd: 'test' }, function (err, res) {
            assert(!err)
            assert(res.tx)
            assert(res.result.indexOf('name:bar') !== -1)
            done()
          })
        })
      })
  })


  it('web-basic', function( fin ) {
    Seneca({log:'silent',errhandler:fin,default_plugins:no_t})
      .use('../transport.js')
      .add( 'c:1', function(args,done){done(null,{s:'1-'+args.d})} )
      .listen({type:'web',port:20202})
      .ready( function() {

        var count = 0<commit_msg>Create a test for issue #52<commit_after>/* Copyright (c) 2013-2015 <NAME> */
'use strict'


var assert = require('assert')
var Lab = require('lab')
var Seneca = require('seneca')
var Shared   = require('seneca-transport-test')
var Wreck = require('wreck')
var Transport = require('../')


var lab = exports.lab = Lab.script()
var describe = lab.describe
var it = lab.it

var no_t = {transport:false}

process.setMaxListeners(999)


function run_client( type, port, done, tag ) {
  Seneca({tag:tag,log:'silent',default_plugins:no_t,debug:{short_logs:true}})
    .use(Transport)
    .client({ type: type, port: port })
    .ready( function() {

      this.act('c:1,d:A',function(err,out){
        if(err) return fin(err);

        assert.equal( '{"s":"1-A"}', JSON.stringify(out) )

        this.act('c:1,d:AA',function(err,out){
          if(err) return fin(err);

          assert.equal( '{"s":"1-AA"}', JSON.stringify(out) )

          this.close(done)
        })
      })
    })
}

function get_seneca(tag) {
  return Seneca({tag:tag, log:'silent', default_plugins: no_t, debug: {short_logs:true}}).use(Transport)
}

describe('transport', function() {

  Shared.basictest({
    seneca: get_seneca(),
    script: lab,
    type: 'tcp'
  })

  Shared.basicpintest({
    seneca: get_seneca(),
    script: lab,
    type: 'tcp'
  })

  Shared.basictest({
    seneca: get_seneca(),
    script: lab,
    type: 'web'
  })

  Shared.basicpintest({
    seneca: get_seneca(),
    script: lab,
    type: 'web'
  })

  it('tcp-basic', function( fin ) {
    Seneca({tag:'srv',log:'silent',default_plugins:no_t,debug:{short_logs:true}})
      .use('../transport.js')
      .add( 'c:1', function(args,done){done(null,{s:'1-'+args.d})} )
      .listen({type:'tcp',port:20102})
      .ready( function() {
        var seneca = this

        var count = 0
        function check() {
          count++
          if( 3 == count ) {
            seneca.close(fin)
          }
        }

        run_client( 'tcp', 20102, check, 'cln0' )
        run_client( 'tcp', 20102, check, 'cln1' )
        run_client( 'tcp', 20102, check, 'cln2' )
      })
  })

  it('uses correct tx$ properties on entity actions for "transported" entities', function (done) {
    Seneca({log: 'silent', default_plugins: no_t})
      .use(Transport)
      .add({cmd: 'test'}, function (args, cb) {
        args.entity.save$(function (err, entity) {
          if (err) return cb(err)
          this.act({cmd: 'test2'}, function (err, test2Result) {
            if (err) return cb(err)
            cb(null, {entity: entity, txBeforeEntityAction: args.tx$, txAfterEntityAction: test2Result.tx})
          })
        })
      })
      .add({cmd: 'test2'}, function (args, cb) {
        cb(null, {tx: args.tx$})
      })
      .listen({type: 'tcp', port: 20103})
      .ready(function () {
        Seneca({log: 'silent', default_plugins: no_t})
          .use(Transport)
          .client({type: 'tcp', port: 20103})
          .ready(function () {
            this.act({cmd: 'test', entity: this.make$('test').data$({name: 'bar'})}, function (err, res) {
              assert(!err)
              assert(res.entity.name === 'bar')
              assert(res.txBeforeEntityAction === res.txAfterEntityAction)
              done()
            })
          })
      })
  })


  it('web-basic', function( fin ) {
    Seneca({log:'silent',errhandler:fin,default_plugins:no_t})
      .use('../transport.js')
      .add( 'c:1', function(args,done){done(null,{s:'1-'+args.d})} )
      .listen({type:'web',port:20202})
      .ready( function() {

        var count = 0
{'fraction_non_alphanumeric': 0.18546733079402286, 'fraction_numerical': 0.018605332552007033, 'mean_word_length': 2.5354738477472814, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2228711', 'n_tokens_mistral': 2461, 'n_tokens_neox': 2329, 'n_words': 465}