@@ -22,12 +22,19 @@ def index
22
22
23
23
RSpec ::Matchers . define :have_kind_of_middleware do |expected |
24
24
match do |actual |
25
+ found = 0
25
26
while actual
26
- return true if actual . class <= expected
27
+ found += 1 if actual . class <= expected
27
28
without_warnings { actual = actual . instance_variable_get ( :@app ) }
28
29
end
29
- false
30
+ found == ( count || 1 )
30
31
end
32
+
33
+ chain :once do
34
+ @count = 1
35
+ end
36
+
37
+ chain :copies , :count
31
38
end
32
39
33
40
before ( :each ) do
@@ -44,8 +51,8 @@ def index
44
51
context 'set to true' do
45
52
let ( :rails_options ) { super ( ) . merge ( middleware : true ) }
46
53
47
- it { expect ( app ) . to have_kind_of_middleware ( Datadog ::Contrib ::Rack ::TraceMiddleware ) }
48
- it { expect ( app ) . to have_kind_of_middleware ( Datadog ::Contrib ::Rails ::ExceptionMiddleware ) }
54
+ it { expect ( app ) . to have_kind_of_middleware ( Datadog ::Contrib ::Rack ::TraceMiddleware ) . once }
55
+ it { expect ( app ) . to have_kind_of_middleware ( Datadog ::Contrib ::Rails ::ExceptionMiddleware ) . once }
49
56
end
50
57
51
58
context 'set to false' do
@@ -56,4 +63,28 @@ def index
56
63
it { expect ( app ) . to_not have_kind_of_middleware ( Datadog ::Contrib ::Rails ::ExceptionMiddleware ) }
57
64
end
58
65
end
66
+
67
+ describe 'when load hooks run twice' do
68
+ before ( :each ) do
69
+ # Set expectations
70
+ expect ( Datadog ::Contrib ::Rails ::Patcher ) . to receive ( :add_middleware )
71
+ . with ( a_kind_of ( Rails ::Application ) )
72
+ . once
73
+ . and_call_original
74
+
75
+ without_warnings do
76
+ # Then load the app, which run load hooks
77
+ app
78
+
79
+ # Then manually re-run load hooks
80
+ ActiveSupport . run_load_hooks ( :before_initialize , app )
81
+ ActiveSupport . run_load_hooks ( :after_initialize , app )
82
+ end
83
+ end
84
+
85
+ it 'only includes the middleware once' do
86
+ expect ( app ) . to have_kind_of_middleware ( Datadog ::Contrib ::Rack ::TraceMiddleware ) . once
87
+ expect ( app ) . to have_kind_of_middleware ( Datadog ::Contrib ::Rails ::ExceptionMiddleware ) . once
88
+ end
89
+ end
59
90
end
0 commit comments