'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 timed out autobahn', {
    size: 2
}, function t(cluster, assert) {
    MockBahn(cluster.dummies[0]);

    var client = AutobahnClient({
        serviceName: 'A',
        callerName: 'A-client',
        hostPortList: [cluster.dummies[0].hostPort],
        tchannel: cluster.dummies[1],
        hardFail: true,
        registrationTimeout: 100,
        logger: DebugLogtron('autobahnClient')
    });

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

    client.register({
        timeout: 200
    });
    client.once('error', onError);

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

        assert.equal(err.type,
            'autobahn-client.registration-timeout');
        assert.equal(err.time, 100);
        assert.equal(err.fullType,
            'autobahn-client.registration-timeout' +
            '~!~error.wrapped-unknown');
        assert.equal(err.causeMessage,
            'registration timeout!');

        assert.end();
    }
});

allocCluster.test('register with timed out autobahn + no hardFail', {
    size: 2
}, function t(cluster, assert) {
    MockBahn(cluster.dummies[0]);

    var client = AutobahnClient({
        serviceName: 'A',
        callerName: 'A-client',
        hostPortList: [cluster.dummies[0].hostPort],
        tchannel: cluster.dummies[1],
        logger: DebugLogtron('autobahnClient')
    });

    var attempts = 0;

    client.on('error', onError);
    client.on('register-attempt', onRegisterAttempt);
    client.register({
        timeout: 200
    });

    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();
    }
});

function MockBahn(channel) {
    var hyperChan = channel.makeSubChannel({
        serviceName: 'hyperbahn'
    });

    hyperChan.register('ad', ad);

    function ad(req, res, arg2, arg3) {
        /* do nothing to time out */
    }
}

@@ -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
@@ -220,28 +220,29 @@
 h timed out 
-auto
+hyper
 bahn', %7B%0A   
@@ -291,35 +291,105 @@
     
-MockBahn(cluster.dummies%5B0%5D
+var bob = cluster.remotes.bob;%0A    var steve = cluster.remotes.steve;%0A%0A    MockBahn(steve.channel
 );%0A%0A
@@ -397,36 +397,37 @@
    var client = 
-Auto
+Hyper
 bahnClient(%7B%0A   
@@ -504,34 +504,29 @@
 tList: %5B
-cluster.dummies%5B0%5D
+steve.channel
 .hostPor
@@ -547,34 +547,27 @@
 hannel: 
-cluster.dummies%5B1%5D
+bob.channel
 ,%0A      
@@ -640,36 +640,37 @@
 : DebugLogtron('
-auto
+hyper
 bahnClient')%0A   
@@ -713,36 +713,37 @@
 rror',%0A        '
-Auto
+Hyper
 bahnClient: regi
@@ -837,20 +837,21 @@
        '
-Auto
+Hyper
 bahnClie
@@ -1062,36 +1062,37 @@
 e,%0A            '
-auto
+hyper
 bahn-client.regi
@@ -1196,20 +1196,21 @@
        '
-auto
+hyper
 bahn-cli
@@ -1393,13 +1393,17 @@
 );%0A%0A
-alloc
+Hyperbahn
 Clus
@@ -1436,20 +1436,21 @@
 med out 
-auto
+hyper
 bahn + n
@@ -1517,35 +1517,105 @@
     
-MockBahn(cluster.dummies%5B0%5D
+var bob = cluster.remotes.bob;%0A    var steve = cluster.remotes.steve;%0A%0A    MockBahn(steve.channel
 );%0A%0A
@@ -1635,12 +1635,13 @@
 t = 
-Auto
+Hyper
 bahn
@@ -1734,26 +1734,21 @@
 t: %5B
-cluster.dummies%5B0%5D
+steve.channel
 .hos
@@ -1777,26 +1777,19 @@
 el: 
-cluster.dummies%5B1%5D
+bob.channel
 ,%0A  
@@ -1820,12 +1820,13 @@
 on('
-auto
+hyper
 bahn

refactor times-out test to pass