|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe Sentry::BackpressureMonitor do |
| 4 | + let(:string_io) { StringIO.new } |
| 5 | + |
| 6 | + before do |
| 7 | + perform_basic_setup do |config| |
| 8 | + config.enable_backpressure_handling = true |
| 9 | + config.logger = Logger.new(string_io) |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + let(:configuration) { Sentry.configuration } |
| 14 | + let(:client) { Sentry.get_current_client } |
| 15 | + let(:transport) { client.transport } |
| 16 | + let(:background_worker) { Sentry.background_worker } |
| 17 | + |
| 18 | + subject { described_class.new(configuration, client) } |
| 19 | + |
| 20 | + describe '#healthy?' do |
| 21 | + it 'returns true by default' do |
| 22 | + expect(subject.healthy?).to eq(true) |
| 23 | + end |
| 24 | + |
| 25 | + it 'returns false when unhealthy' do |
| 26 | + expect(transport).to receive(:any_rate_limited?).and_return(true) |
| 27 | + subject.run |
| 28 | + expect(subject.healthy?).to eq(false) |
| 29 | + end |
| 30 | + |
| 31 | + it 'spawns new thread' do |
| 32 | + expect { subject.healthy? }.to change { Thread.list.count }.by(1) |
| 33 | + expect(subject.instance_variable_get(:@thread)).to be_a(Thread) |
| 34 | + end |
| 35 | + |
| 36 | + it 'spawns only one thread' do |
| 37 | + expect { subject.healthy? }.to change { Thread.list.count }.by(1) |
| 38 | + thread = subject.instance_variable_get(:@thread) |
| 39 | + expect(thread).to receive(:alive?).and_return(true) |
| 40 | + expect { subject.healthy? }.to change { Thread.list.count }.by(0) |
| 41 | + end |
| 42 | + |
| 43 | + context 'when thread creation fails' do |
| 44 | + before do |
| 45 | + expect(Thread).to receive(:new).and_raise(ThreadError) |
| 46 | + end |
| 47 | + |
| 48 | + it 'does not create new thread' do |
| 49 | + expect { subject.healthy? }.to change { Thread.list.count }.by(0) |
| 50 | + end |
| 51 | + |
| 52 | + it 'returns true (the default)' do |
| 53 | + expect(subject.healthy?).to eq(true) |
| 54 | + end |
| 55 | + |
| 56 | + it 'logs error' do |
| 57 | + subject.healthy? |
| 58 | + expect(string_io.string).to match(/\[BackpressureMonitor\] Thread creation failed/) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context 'when killed' do |
| 63 | + before { subject.kill } |
| 64 | + |
| 65 | + it 'returns true (the default)' do |
| 66 | + expect(subject.healthy?).to eq(true) |
| 67 | + end |
| 68 | + |
| 69 | + it 'does not create new thread' do |
| 70 | + expect(Thread).not_to receive(:new) |
| 71 | + expect { subject.healthy? }.to change { Thread.list.count }.by(0) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + # thread behavior is tested above in healthy? |
| 77 | + describe '#downsample_factor' do |
| 78 | + it 'returns 0 by default' do |
| 79 | + expect(subject.downsample_factor).to eq(0) |
| 80 | + end |
| 81 | + |
| 82 | + it 'increases when unhealthy upto limit' do |
| 83 | + allow(transport).to receive(:any_rate_limited?).and_return(true) |
| 84 | + |
| 85 | + 10.times do |i| |
| 86 | + subject.run |
| 87 | + expect(subject.downsample_factor).to eq(i + 1) |
| 88 | + end |
| 89 | + |
| 90 | + 2.times do |i| |
| 91 | + subject.run |
| 92 | + expect(subject.downsample_factor).to eq(10) |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe '#run' do |
| 98 | + it 'logs behavior' do |
| 99 | + allow(background_worker).to receive(:full?).and_return(true) |
| 100 | + subject.run |
| 101 | + expect(string_io.string).to match(/\[BackpressureMonitor\] health check negative, downsampling with a factor of 1/) |
| 102 | + |
| 103 | + allow(background_worker).to receive(:full?).and_return(false) |
| 104 | + subject.run |
| 105 | + expect(string_io.string).to match(/\[BackpressureMonitor\] health check positive, reverting to normal sampling/) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + describe '#kill' do |
| 110 | + it 'kills the thread and logs a message' do |
| 111 | + subject.healthy? |
| 112 | + expect(subject.instance_variable_get(:@thread)).to receive(:kill) |
| 113 | + subject.kill |
| 114 | + expect(string_io.string).to match(/\[BackpressureMonitor\] killing monitor/) |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments