blob: e063866b0569b17f65b64ee3cb3d8ba14fd9d7f9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# frozen_string_literal: true
require 'spec_helper'
require 'memory_profiler'
RSpec.describe Gitlab::Middleware::MemoryReport do
let(:app) { proc { |env| [200, { 'Content-Type' => 'text/plain' }, ['Hello world!']] } }
let(:middleware) { described_class.new(app) }
describe '#call' do
shared_examples 'returns original response' do
it 'returns original response' do
expect(MemoryProfiler).not_to receive(:report)
status, headers, body = middleware.call(env)
expect(status).to eq(200)
expect(headers).to eq({ 'Content-Type' => 'text/plain' })
expect(body.first).to eq('Hello world!')
end
it 'does not call the MemoryProfiler' do
expect(MemoryProfiler).not_to receive(:report)
middleware.call(env)
end
end
context 'when the Rails environment is not development' do
let(:env) { Rack::MockRequest.env_for('/') }
it_behaves_like 'returns original response'
end
context 'when the Rails environment is development' do
before do
allow(Rails.env).to receive(:development?).and_return(true)
end
context 'when memory report is not requested' do
let(:env) { Rack::MockRequest.env_for('/') }
it_behaves_like 'returns original response'
end
context 'when memory report is requested' do
let(:env) { Rack::MockRequest.env_for('/', params: { 'performance_bar' => 'memory' }) }
before do
allow(env).to receive(:[]).and_call_original
allow(app).to receive(:call).and_return(empty_memory_report)
end
let(:empty_memory_report) do
report = MemoryProfiler::Results.new
report.register_results(MemoryProfiler::StatHash.new, MemoryProfiler::StatHash.new, 1)
end
it 'returns a memory report' do
expect(MemoryProfiler).to receive(:report).and_yield
status, headers, body = middleware.call(env)
expect(status).to eq(200)
expect(headers).to eq({ 'Content-Type' => 'text/plain' })
expect(body.first).to include('Total allocated: 0 B')
end
context 'when something goes wrong with creating the report' do
before do
expect(MemoryProfiler).to receive(:report).and_raise(StandardError, 'something went terribly wrong!')
end
it 'logs the error' do
expect(::Gitlab::ErrorTracking).to receive(:track_exception)
middleware.call(env)
end
it 'returns the error' do
status, headers, body = middleware.call(env)
expect(status).to eq(500)
expect(headers).to eq({ 'Content-Type' => 'text/plain' })
expect(body.first).to include('Could not generate memory report: something went terribly wrong!')
end
end
end
end
end
end
|