'use strict';

var DebugLogtron = require('debug-logtron');

var AutobahnClient = require('../index.js');

var allocCluster = require('autobahn/test/lib/test-cluster.js');

allocCluster.test('register with autobahn down', {
    size: 2
}, function t(cluster, assert) {
    var client = AutobahnClient({
        serviceName: 'A',
        callerName: 'A-client',
        // 5001 & 5002 should be DEAD ports
        hostPortList: ['127.0.0.1:5001', '127.0.0.1:5002'],
        tchannel: cluster.dummies[0],
        hardFail: true,
        registrationTimeout: 200,
        logger: DebugLogtron('autobahnClient')
    });

    client.logger.whitelist('error',
        'AutobahnClient: registration failure, marking server as sick'
    );
    client.logger.whitelist('fatal',
        'AutobahnClient: registration timed out'
    );

    client.register();
    client.once('error', onError);

    function onError(err) {
        assert.ok(err);

        assert.equal(err.type,
            'autobahn-client.registration-timeout');
        assert.equal(err.time, 200);
        assert.equal(err.code, 'ECONNREFUSED');
        assert.equal(err.syscall, 'connect');
        assert.equal(err.fullType,
            'autobahn-client.registration-timeout' +
            '~!~tchannel.socket' +
            '~!~error.wrapped-io.connect.ECONNREFUSED');
        assert.equal(err.causeMessage,
            'tchannel socket error ' +
            '(ECONNREFUSED from connect): ' +
            'connect ECONNREFUSED');

        assert.end();
    }
});

allocCluster.test('register with autobahn down + no hardFail', {
    size: 5
}, function t(cluster, assert) {
    var client = AutobahnClient({
        serviceName: 'A',
        callerName: 'A-client',
        // 5001 & 5002 should be DEAD ports
        hostPortList: ['127.0.0.1:5001', '127.0.0.1:5002'],
        tchannel: cluster.dummies[0],
        logger: DebugLogtron('autobahnClient')
    });

    var attempts = 0;

    client.on('error', onError);
    client.on('register-attempt', onRegisterAttempt);
    client.register();

    function onError() {
        assert.ok(false, 'should not error');
    }

    function onRegisterAttempt() {
        if (++attempts < 3) {
            return;
        }

        client.removeListener('register-attempt', onRegisterAttempt);

        client.destroy();
        assert.ok(true);

        assert.end();
    }
});

@@ -58,20 +58,21 @@
 );%0A%0Avar 
-Auto
+Hyper
 bahnClie
@@ -88,16 +88,29 @@
 ire('../
+../hyperbahn/
 index.js
@@ -117,18 +117,21 @@
 ');%0A
-%0A
 var 
-alloc
+Hyperbahn
 Clus
@@ -149,30 +149,24 @@
 re('
-autobahn/test/lib/test
+../lib/hyperbahn
 -clu
@@ -173,29 +173,33 @@
 ster.js');%0A%0A
-alloc
+Hyperbahn
 Cluster.test
@@ -206,36 +206,37 @@
 ('register with 
-auto
+hyper
 bahn down', %7B%0A  
@@ -270,32 +270,68 @@
 ster, assert) %7B%0A
+    var bob = cluster.remotes.bob;%0A%0A
     var client =
@@ -323,36 +323,37 @@
    var client = 
-Auto
+Hyper
 bahnClient(%7B%0A   
@@ -529,34 +529,27 @@
 hannel: 
-cluster.dummies%5B0%5D
+bob.channel
 ,%0A      
@@ -622,36 +622,37 @@
 : DebugLogtron('
-auto
+hyper
 bahnClient')%0A   
@@ -695,36 +695,37 @@
 rror',%0A        '
-Auto
+Hyper
 bahnClient: regi
@@ -819,20 +819,21 @@
        '
-Auto
+Hyper
 bahnClie
@@ -1016,36 +1016,37 @@
 e,%0A            '
-auto
+hyper
 bahn-client.regi
@@ -1244,20 +1244,21 @@
        '
-auto
+hyper
 bahn-cli
@@ -1576,13 +1576,17 @@
 );%0A%0A
-alloc
+Hyperbahn
 Clus
@@ -1609,20 +1609,21 @@
 er with 
-auto
+hyper
 bahn dow
@@ -1679,32 +1679,68 @@
 ster, assert) %7B%0A
+    var bob = cluster.remotes.bob;%0A%0A
     var client =
@@ -1744,12 +1744,13 @@
 t = 
-Auto
+Hyper
 bahn
@@ -1942,26 +1942,19 @@
 el: 
-cluster.dummies%5B0%5D
+bob.channel
 ,%0A  
@@ -1985,12 +1985,13 @@
 on('
-auto
+hyper
 bahn

refactor autobahn-down test to pass