<commit_before>require 'spec_helper'

class TestCommandMonitoringSubscriber
  def initialize
    @started_events = []
    @succeeded_events = []
    @failed_events = []
  end

  attr_reader :started_events, :succeeded_events, :failed_events

  def started(event)
    @started_events << event
  end

  def succeeded(event)
    @succeeded_events << event
  end

  def failed(event)
    @failed_events << event
  end
end

describe 'Command monitoring' do
  let(:subscriber) { TestCommandMonitoringSubscriber.new }

  before do
    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, subscriber)
  end

  after do
    Mongo::Monitoring::Global.unsubscribe(Mongo::Monitoring::COMMAND, subscriber)
  end

  let(:client) { Mongo::Client.new(authorized_client.cluster.addresses.map(&:to_s)) }

  it 'notifies on successful commands' do
    result = client.database.command(:ismaster => 1)
    expect(result.documents.first['ismaster']).to be true

    expect(subscriber.started_events.length).to eql(1)
    started_event = subscriber.started_events.first
    expect(started_event.command_name).to eql(:ismaster)
    expect(started_event.command).to be_a(BSON::Document)
    expect(started_event.command['ismaster']).to eql(1)
    expect(started_event.address).to be_a(Mongo::Address)

    expect(subscriber.succeeded_events.length).to eql(1)
    succeeded_event = subscriber.succeeded_events.first
    expect(succeeded_event.command_name).to eql(:ismaster)
    expect(succeeded_event.command).to be_a(BSON::Document)
    expect(succeeded_event.command['ismaster']).to eql(1)
    expect(succeeded_event.reply).to be_a(BSON::Document)
    expect(succeeded_event.reply['ismaster']).to eql(true)<commit_msg>Pass client options to get authorization<commit_after>require 'spec_helper'

class TestCommandMonitoringSubscriber
  def initialize
    @started_events = []
    @succeeded_events = []
    @failed_events = []
  end

  attr_reader :started_events, :succeeded_events, :failed_events

  def started(event)
    @started_events << event
  end

  def succeeded(event)
    @succeeded_events << event
  end

  def failed(event)
    @failed_events << event
  end
end

describe 'Command monitoring' do
  let(:subscriber) { TestCommandMonitoringSubscriber.new }

  before do
    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, subscriber)
  end

  after do
    Mongo::Monitoring::Global.unsubscribe(Mongo::Monitoring::COMMAND, subscriber)
  end

  let(:client) { Mongo::Client.new(authorized_client.cluster.addresses.map(&:to_s), authorized_client.options) }

  it 'notifies on successful commands' do
    result = client.database.command(:ismaster => 1)
    expect(result.documents.first['ismaster']).to be true

    expect(subscriber.started_events.length).to eql(1)
    started_event = subscriber.started_events.first
    expect(started_event.command_name).to eql(:ismaster)
    expect(started_event.command).to be_a(BSON::Document)
    expect(started_event.command['ismaster']).to eql(1)
    expect(started_event.address).to be_a(Mongo::Address)

    expect(subscriber.succeeded_events.length).to eql(1)
    succeeded_event = subscriber.succeeded_events.first
    expect(succeeded_event.command_name).to eql(:ismaster)
    expect(succeeded_event.command).to be_a(BSON::Document)
    expect(succeeded_event.command['ismaster']).to eql(1)
    expect(succeeded_event.reply).to be_a(BSON::Document)
    expect(succeeded_event.reply['ismaster']).to eql(true)
{'fraction_non_alphanumeric': 0.11649365628604383, 'fraction_numerical': 0.0028835063437139563, 'mean_word_length': 5.150709219858156, 'pattern_counts': {'":': 0, '<': 15, '<?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': '5900841', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1092, 'n_words': 177}