<commit_before>var assert = require('assert');
var mubsub = require('../lib/index');
var data = require('./fixtures/data');
var helpers = require('./helpers');

describe('Channel', function() {
    beforeEach(function() {
        this.client = mubsub(helpers.uri);
        this.channel = this.client.channel('channel');
    });

    it('unsubscribes properly', function(done) {
        var subscription = this.channel.subscribe('a', function(data) {
            assert.equal(data, 'a');
            subscription.unsubscribe();
            done();
        });

        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
    });

    it('unsubscribes if channel is closed', function(done) {
        var self = this;

        var subscription = this.channel.subscribe('a', function(data) {
            assert.equal(data, 'a');
            self.channel.close();
            done();
        });

        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
    });

    it('unsubscribes if client is closed', function(done) {
        var self = this;

        var subscription = this.channel.subscribe('a', function(data) {
            assert.equal(data, 'a');
            self.client.close();
            done();
        });

        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
    });

    it('should not emit old events to a second client', function(done) {
        var self = this;
        var channel0 = this.client.channel('channel1');

        channel0.subscribe('b', function(data) {
            assert.equal(data, 'b');
        });

        channel0.publish('b', 'b');

        // Client 0 have now published one event
        // Client 1 should not receive that event
        var client1 = mubsub(helpers.uri);
        
        setTimeout(function() {          
            var channel1 = client1.channel('channel1');
            channel1.subscribe('b', function(data) {
                assert.fail(data, '', 'unexpected event');
            });
        }, 1000);

        setTimeout(function() {
            done();
        }, 1500);
    });<commit_msg>Better test<commit_after>var assert = require('assert');
var mubsub = require('../lib/index');
var data = require('./fixtures/data');
var helpers = require('./helpers');

describe('Channel', function() {
    beforeEach(function() {
        this.client = mubsub(helpers.uri);
        this.channel = this.client.channel('channel');
    });

    it('unsubscribes properly', function(done) {
        var subscription = this.channel.subscribe('a', function(data) {
            assert.equal(data, 'a');
            subscription.unsubscribe();
            done();
        });

        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
    });

    it('unsubscribes if channel is closed', function(done) {
        var self = this;

        var subscription = this.channel.subscribe('a', function(data) {
            assert.equal(data, 'a');
            self.channel.close();
            done();
        });

        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
    });

    it('unsubscribes if client is closed', function(done) {
        var self = this;

        var subscription = this.channel.subscribe('a', function(data) {
            assert.equal(data, 'a');
            self.client.close();
            done();
        });

        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
        this.channel.publish('a', 'a');
    });

    it('should not emit old events to a second client', function(done) {
        var self = this;
        var channel0 = this.client.channel('channel1');

        var subscription0 = channel0.subscribe('b', function(data) {
            subscription0.unsubscribe();
            assert.equal(data, 'b');

            // Client 0 have now published and received one event
            // Client 1 should not receive that event but should get the second event
            var client1 = mubsub(helpers.uri);
            var channel1 = client1.channel('channel1');

            var subscription1 = channel1.subscribe('b', function(data) {
                subscription1.unsubscribe();
                assert.equal(data, 'a');
                done();
            });

            channel1.publish('b', 'a');
        });

        channel0.publish('b', 'b');
    });
{'fraction_non_alphanumeric': 0.16364826532838753, 'fraction_numerical': 0.007636919048658084, 'mean_word_length': 2.058038692461641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3987354', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1251, 'n_words': 280}