require 'spec_helper' describe Appsignal::Transaction do describe '.create' do before { Appsignal::Transaction.create('1', {}) } it 'should add the id to the thread' do Thread.current[:appsignal_transaction_id].should == '1' end it 'should add the transaction to the list' do Appsignal.transactions['1'].should be_a Appsignal::Transaction end end describe '.current' do let(:transaction) { Appsignal::Transaction.create('1', {}) } before { transaction } subject { Appsignal::Transaction.current } it 'should return the correct transaction' do should eq transaction end end describe 'transaction instance' do let(:transaction) do Appsignal::Transaction.create('1', { 'HTTP_USER_AGENT' => 'IE6', 'SERVER_NAME' => 'localhost', 'action_dispatch.routes' => 'not_available' }) end describe '#request' do subject { transaction.request } it { should be_a ActionDispatch::Request } end describe '#set_process_action_event' do let(:process_action_event) { create_process_action_event } it 'should add a process action event' do transaction.set_process_action_event(process_action_event) transaction.process_action_event.should == process_action_event transaction.action.should == 'BlogPostsController#show' end end describe '#add_event' do let(:event) {stub(:name => 'test') } it 'should add an event' do expect { transaction.add_event(event) }.to change(transaction, :events).to([event]) end end describe '#add_exception' do let(:exception) {stub(:name => 'test') } it 'should add an exception' do expect { transaction.add_exception(exception) }.to change(transaction, :exception).to(exception) end end describe '#slow_request?' do let(:duration) { 199 } subject { transaction.slow_request? } before { transaction.set_process_action_event( stub(:duration => duration, :payload => {}) ) } it { should be_false } context "when the request took too long" do let(:duration) { 200 } it { should be_true } end context "when process action event is empty" do before { transaction.set_process_action_event(nil) } it { should be_false } end context "when process action event does not have a payload" do before { transaction.set_process_action_event(stub(:payload => nil)) } it { should be_false } end end describe "#clear_payload_and_events!" do subject { slow_transaction } it "should clear the process action payload and events" do subject.clear_payload_and_events! subject.process_action_event.payload.should be_empty subject.events.should be_empty end end describe '#to_hash' do subject { transaction.to_hash } before { transaction.stub(:exception? => false) } context "with an exception request" do before { transaction.stub(:exception? => true) } it "calls TransactionFormatter.faulty with self" do Appsignal::TransactionFormatter.should_receive(:faulty). with(transaction).and_return({}) end end context "with a slow request" do before { transaction.stub(:slow_request? => true) } it "calls TransactionFormatter.slow with self" do Appsignal::TransactionFormatter.should_receive(:slow). with(transaction).and_return({}) end end context "with a regular request" do before { transaction.stub(:slow_request? => false) } it "calls TransactionFormatter.slow with self" do Appsignal::TransactionFormatter.should_receive(:regular). with(transaction).and_return({}) end end after { subject } end describe '#complete!' do before { transaction.set_process_action_event( stub(:duration => 199, :time => Time.now, :payload => nil) ) } it 'should remove transaction from the list' do expect { transaction.complete! }.to change(Appsignal.transactions, :length).by(-1) end context 'calling the appsignal agent' do context 'without events and exception' do it 'should add transaction to the agent' do Appsignal.agent.should_receive(:add_to_queue).with(transaction) end end context 'with events' do before { transaction.add_event(stub) } it 'should add transaction to the agent' do Appsignal.agent.should_receive(:add_to_queue).with(transaction) end end context 'with exception' do before { transaction.add_exception(stub) } it 'should add transaction to the agent' do Appsignal.agent.should_receive(:add_to_queue).with(transaction) end end after { transaction.complete! } end context 'thread' do before { transaction.complete! } it 'should reset the thread transaction id' do Thread.current[:appsignal_transaction_id].should be_nil end end end end end