<commit_before>/**
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

var assert = require('assert');
var async = require('async');
var uuid = require('node-uuid');

var env = require('./env.js');
var gcloud = require('../lib')(env);

var Subscription = require('../lib/pubsub/subscription.js');

var pubsub = gcloud.pubsub();

function generateTopicName() {
  return 'test-topic-' + uuid.v4();
}

function generateSubName() {
  return 'test-subscription-' + uuid.v4();
}

describe('pubsub', function() {
  var TOPIC_NAMES = [
    generateTopicName(),
    generateTopicName(),
    generateTopicName()
  ];

  var TOPICS = TOPIC_NAMES.map(pubsub.topic.bind(pubsub));

  var TOPIC_FULL_NAMES = TOPICS.map(function(topic) {
    return topic.name;
  });

  before(function(done) {
    // create all needed topics
    async.each(TOPIC_NAMES, function(name, cb) {
      pubsub.createTopic(name, cb);
    }, done);
  });

  after(function(done) {
    // Delete topics
    async.each(TOPICS, function(topic, callback) {
      topic.delete(callback);
    }, done);
  });

  describe('Topic', function() {

    it('should be listed', function(done) {
      pubsub.getTopics(function(err, topics) {
        assert.ifError(err);

        var results = topics.filter(function(topic) {
          return TOPIC_FULL_NAMES.indexOf(topic.name) !== -1;
        });

        // get all topics in list of known names
        assert.equal(results.length, TOPIC_NAMES.length);
        done();
      });
    });

    it('should return a nextQuery if there are more results', function(done) {
      pubsub.getTopics({
        pageSize: TOPIC_NAMES.length - 1
      }, function(err, topics, next) {
        assert.ifError(err);
        assert(topics.length, TOPIC_NAMES.length - 1);
        assert(next.pageSize, TOPIC_NAMES.length - 1);
        assert(!!next.pageToken, true);
        done();
      });
    });

    it('should be created and deleted', function(done) {
      var TOPIC_NAME = generateTopicName();
      pubsub.createTopic(TOPIC_NAME, function(err) {
        assert.ifError(err);
        pubsub.topic(TOPIC_NAME).delete(done);
      });
    });

    it('should publish a message', function(done) {
      var topic = pubsub.topic(TOPIC_NAMES[0]);
      topic.publish({ data: 'message from me' }, function(err, messageIds) {
        assert.ifError(err);
        assert.equal(messageIds.length, 1);
        done();
      });
    });
  });

  describe('Subscription', function() {
    var TOPIC_NAME = generateTopicName();

    var SUB_NAMES = [
      generateSubName(),
      generateSubName()
    ];

    var SUBSCRIPTIONS = [
      {
        name: SUB_NAMES[0],
        options: { ackDeadlineSeconds: 30 }<commit_msg>pubsub: default autoCreate:true<commit_after>/**
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

var assert = require('assert');
var async = require('async');
var uuid = require('node-uuid');

var env = require('./env.js');
var gcloud = require('../lib')(env);

var Subscription = require('../lib/pubsub/subscription.js');

var pubsub = gcloud.pubsub();

function generateTopicName() {
  return 'test-topic-' + uuid.v4();
}

function generateSubName() {
  return 'test-subscription-' + uuid.v4();
}

describe('pubsub', function() {
  var TOPIC_NAMES = [
    generateTopicName(),
    generateTopicName(),
    generateTopicName()
  ];

  var TOPICS = TOPIC_NAMES.map(pubsub.topic.bind(pubsub));

  var TOPIC_FULL_NAMES = TOPICS.map(function(topic) {
    return topic.name;
  });

  before(function(done) {
    // create all needed topics
    async.each(TOPIC_NAMES, function(name, cb) {
      pubsub.createTopic(name, cb);
    }, done);
  });

  after(function(done) {
    // Delete topics
    async.each(TOPICS, function(topic, callback) {
      topic.delete(callback);
    }, done);
  });

  describe('Topic', function() {
    it('should be listed', function(done) {
      pubsub.getTopics(function(err, topics) {
        assert.ifError(err);

        var results = topics.filter(function(topic) {
          return TOPIC_FULL_NAMES.indexOf(topic.name) !== -1;
        });

        // get all topics in list of known names
        assert.equal(results.length, TOPIC_NAMES.length);
        done();
      });
    });

    it('should return a nextQuery if there are more results', function(done) {
      pubsub.getTopics({
        pageSize: TOPIC_NAMES.length - 1
      }, function(err, topics, next) {
        assert.ifError(err);
        assert(topics.length, TOPIC_NAMES.length - 1);
        assert(next.pageSize, TOPIC_NAMES.length - 1);
        assert(!!next.pageToken, true);
        done();
      });
    });

    it('should be created and deleted', function(done) {
      var TOPIC_NAME = generateTopicName();
      pubsub.createTopic(TOPIC_NAME, function(err) {
        assert.ifError(err);
        pubsub.topic(TOPIC_NAME).delete(done);
      });
    });

    it('should lazily create by default', function(done) {
      var newTopicName = generateTopicName();
      var newTopic = pubsub.topic(newTopicName);

      newTopic.publish({ data: 'message from me' }, function(err) {
        assert.ifError(err);

        pubsub.getTopics(function(err, topics) {
          assert.ifError(err);

          assert(topics.some(function(topic) {
            return topic.name.indexOf(newTopicName) > -1;
          }));

          newTopic.delete(done);
        });
      });
    });

    it('should publish a message', function(done) {
      var topic = pubsub.topic(TOPIC_NAMES[0]);
      topic.publish({ data: 'message from me' }, function(err, messageIds) {
        assert.ifError(err);
        assert.equal(messageIds.length, 1);
        done();
      });
    });
  });

  describe('Subscription', function() {
    var TOPIC_NAME = generateTopicName();

    var SUB_NAMES = [
      generateSubName(),
      generateSubName()
    ];

    var SUBSCRIPTIONS = [
      {
        name: SUB_NAMES[0],
        options: { ackDeadlineSeconds: 30 }
{'fraction_non_alphanumeric': 0.13608218406980016, 'fraction_numerical': 0.005488319729805798, 'mean_word_length': 2.8836065573770493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3243118', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2127, 'n_words': 537}