18
18
<<" OPTIONS" >>,
19
19
<<" PATCH" >>]).
20
20
-define (DEFAULT_PREFIX , [? HANDLER ]).
21
- -define (CLASSES , [<<" 1XX " >>, << " 2XX" >>, <<" 3XX" >>, <<" 4XX" >>, <<" 5XX" >>]).
21
+ -define (CLASSES , [<<" 2XX" >>, <<" 3XX" >>, <<" 4XX" >>, <<" 5XX" >>]).
22
22
23
23
24
24
@@ -39,12 +39,10 @@ all() ->
39
39
% %-------------------------------------------------------------------
40
40
41
41
init_per_suite (Config ) ->
42
- application :ensure_all_started (gun ),
43
42
ejabberd_helper :start_ejabberd_with_config (Config , " ejabberd.cfg" ),
44
43
Config .
45
44
46
45
end_per_suite (_Config ) ->
47
- application :stop (gun ),
48
46
ejabberd_helper :stop_ejabberd (),
49
47
ok .
50
48
@@ -71,8 +69,8 @@ metrics_are_not_recorded_when_record_metrics_is_false(_Config) ->
71
69
destroy_metrics ().
72
70
73
71
metrics_are_not_recorded () ->
74
- run_requests ([get , head , post , put , delete , options , patch ],
75
- [<<" 100 " >>, << " 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>]),
72
+ run_requests ([get , head , post , put , delete , options , patch ],
73
+ [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>]),
76
74
[ensure_metric_value (M , 0 )
77
75
|| M <- count_metrics (? DEFAULT_PREFIX ) ++ latency_metrics (? DEFAULT_PREFIX )].
78
76
@@ -81,10 +79,11 @@ metrics_are_recorded(_Config) ->
81
79
start_listener ([{'_' , ? HANDLER , []}], [{record_metrics , true }]),
82
80
83
81
timer :sleep (1000 ),
82
+ Statuses = [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>],
84
83
run_requests ([get , head , post , put , delete , options , patch ],
85
- [<< " 100 " >>, << " 200 " >>, << " 300 " >>, << " 400 " >>, << " 500 " >>] ),
84
+ Statuses ),
86
85
87
- [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), 5 ) || M <- ? METHODS ],
86
+ [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), length ( Statuses ) ) || M <- ? METHODS ],
88
87
[ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , C ), 1 ) || M <- ? METHODS , C <- ? CLASSES ],
89
88
[ensure_metric_bumped (response_latency_metric (? DEFAULT_PREFIX , M , C )) || M <- ? METHODS , C <- ? CLASSES ],
90
89
@@ -98,10 +97,11 @@ metrics_are_recorded_with_configured_prefix(_Config) ->
98
97
start_listener ([{'_' , ? HANDLER , []}], [{record_metrics , true },
99
98
{handler_to_metric_prefix , #{? HANDLER => Prefix }}]),
100
99
100
+ Statuses = [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>],
101
101
run_requests ([get , head , post , put , delete , options , patch ],
102
- [<< " 100 " >>, << " 200 " >>, << " 300 " >>, << " 400 " >>, << " 500 " >>] ),
102
+ Statuses ),
103
103
104
- [ensure_metric_value (request_count_metric (Prefix , M ), 5 ) || M <- ? METHODS ],
104
+ [ensure_metric_value (request_count_metric (Prefix , M ), length ( Statuses ) ) || M <- ? METHODS ],
105
105
[ensure_metric_value (response_count_metric (Prefix , M , C ), 1 ) || M <- ? METHODS , C <- ? CLASSES ],
106
106
[ensure_metric_bumped (response_latency_metric (Prefix , M , C )) || M <- ? METHODS , C <- ? CLASSES ],
107
107
@@ -112,12 +112,13 @@ unsent_responses_generate_metrics(_Config) ->
112
112
create_metrics (),
113
113
start_listener ([{'_' , ? HANDLER , [{action , none }]}], [{record_metrics , true }]),
114
114
115
+ Statuses = [<<" 200" >>, <<" 300" >>, <<" 400" >>, <<" 500" >>],
115
116
run_requests ([get , head , post , put , delete , options , patch ],
116
- [<< " 100 " >>, << " 200 " >>, << " 300 " >>, << " 400 " >>, << " 500 " >>] ),
117
+ Statuses ),
117
118
118
119
% % when response is not sent Cowboy returns 204
119
- [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), 5 ) || M <- ? METHODS ],
120
- [ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , <<" 2XX" >>), 5 ) || M <- ? METHODS ],
120
+ [ensure_metric_value (request_count_metric (? DEFAULT_PREFIX , M ), length ( Statuses ) ) || M <- ? METHODS ],
121
+ [ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , <<" 2XX" >>), length ( Statuses ) ) || M <- ? METHODS ],
121
122
[ensure_metric_value (response_count_metric (? DEFAULT_PREFIX , M , C ), 0 ) || M <- ? METHODS , C <- ? CLASSES -- [<<" 2XX" >>]],
122
123
[ensure_metric_bumped (response_latency_metric (? DEFAULT_PREFIX , M , <<" 2XX" >>)) || M <- ? METHODS ],
123
124
[ensure_metric_value (response_latency_metric (? DEFAULT_PREFIX , M , C ), 0 ) || M <- ? METHODS , C <- ? CLASSES -- [<<" 2XX" >>]],
@@ -135,12 +136,12 @@ init(_Type, Req, Opts) ->
135
136
handle (Req , echo_status = State ) ->
136
137
{BinStatus , Req1 } = cowboy_req :qs_val (<<" status" >>, Req ),
137
138
Status = binary_to_integer (BinStatus ),
138
- {ok , RespReq } = cowboy_req :reply (Status , [], <<>>, Req1 ),
139
+ {ok , RespReq } = cowboy_req :reply (Status , Req1 ),
139
140
{ok , RespReq , State };
140
141
handle (Req , none = State ) ->
141
142
{ok , Req , State }.
142
143
143
- terminate (_Reason , _Req , _State ) ->
144
+ terminate (Reason , _Req , _State ) ->
144
145
ok .
145
146
146
147
% %-------------------------------------------------------------------
@@ -192,10 +193,19 @@ ensure_metric_bumped(Metric) ->
192
193
? assertNotEqual (0 , proplists :get_value (value , DataPoints )).
193
194
194
195
run_requests (Methods , Statuses ) ->
195
- {ok , Conn } = gun :open (" localhost" , ranch :get_port (? LISTENER )),
196
- {ok , _ } = gun :await_up (Conn ),
197
- [begin
198
- StreamRef = gun :M (Conn , <<" /?status=" , S /binary >>, []),
199
- {response , fin , _ , _ } = gun :await (Conn , StreamRef )
200
- end || M <- Methods , S <- Statuses ].
201
-
196
+ Port = ranch :get_port (? LISTENER ),
197
+ {ok , Conn } = fusco :start_link ({" localhost" , Port , false }, []),
198
+ Res = [{ok , _Result } = fusco :request (Conn , <<" /?status=" , S /binary >>, method_to_upper_bin (M ), [], [], 5000 )
199
+ || M <- Methods , S <- Statuses ],
200
+ fusco :disconnect (Conn ),
201
+ Res .
202
+
203
+ method_to_upper_bin (Method ) when is_binary (Method ) ->
204
+ Method ;
205
+ method_to_upper_bin (Method ) when is_atom (Method ) ->
206
+ MethodBin = atom_to_binary (Method , utf8 ),
207
+ list_to_binary (
208
+ string :to_upper (
209
+ binary_to_list (MethodBin )
210
+ )
211
+ ).
0 commit comments