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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
|
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% https://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2011-2017 Pivotal Software, Inc. All rights reserved.
%%
-module(unit_inbroker_non_parallel_SUITE).
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").
-include_lib("kernel/include/file.hrl").
-include_lib("amqp_client/include/amqp_client.hrl").
-compile(export_all).
-define(TIMEOUT, 30000).
all() ->
[
{group, non_parallel_tests}
].
groups() ->
[
{non_parallel_tests, [], [
app_management, %% Restart RabbitMQ.
channel_statistics, %% Expect specific statistics.
disk_monitor, %% Replace rabbit_misc module.
disk_monitor_enable,
file_handle_cache, %% Change FHC limit.
head_message_timestamp_statistics, %% Expect specific statistics.
log_management, %% Check log files.
log_file_initialised_during_startup,
log_file_fails_to_initialise_during_startup,
externally_rotated_logs_are_automatically_reopened %% Check log files.
]}
].
%% -------------------------------------------------------------------
%% Testsuite setup/teardown.
%% -------------------------------------------------------------------
init_per_suite(Config) ->
rabbit_ct_helpers:log_environment(),
rabbit_ct_helpers:run_setup_steps(Config).
end_per_suite(Config) ->
rabbit_ct_helpers:run_teardown_steps(Config).
init_per_group(Group, Config) ->
Config1 = rabbit_ct_helpers:set_config(Config, [
{rmq_nodename_suffix, Group},
{rmq_nodes_count, 2}
]),
rabbit_ct_helpers:run_steps(Config1,
rabbit_ct_broker_helpers:setup_steps() ++
rabbit_ct_client_helpers:setup_steps() ++ [
fun setup_file_handle_cache/1
]).
setup_file_handle_cache(Config) ->
ok = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, setup_file_handle_cache1, []),
Config.
setup_file_handle_cache1() ->
%% FIXME: Why are we doing this?
application:set_env(rabbit, file_handles_high_watermark, 10),
ok = file_handle_cache:set_limit(10),
ok.
end_per_group(_Group, Config) ->
rabbit_ct_helpers:run_steps(Config,
rabbit_ct_client_helpers:teardown_steps() ++
rabbit_ct_broker_helpers:teardown_steps()).
init_per_testcase(Testcase, Config) ->
rabbit_ct_helpers:testcase_started(Config, Testcase).
end_per_testcase(Testcase, Config) ->
rabbit_ct_helpers:testcase_finished(Config, Testcase).
%% -------------------------------------------------------------------
%% Application management.
%% -------------------------------------------------------------------
app_management(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, app_management1, [Config]).
app_management1(_Config) ->
wait_for_application(rabbit),
%% Starting, stopping and diagnostics. Note that we don't try
%% 'report' when the rabbit app is stopped and that we enable
%% tracing for the duration of this function.
ok = rabbit_trace:start(<<"/">>),
ok = rabbit:stop(),
ok = rabbit:stop(),
ok = no_exceptions(rabbit, status, []),
ok = no_exceptions(rabbit, environment, []),
ok = rabbit:start(),
ok = rabbit:start(),
ok = no_exceptions(rabbit, status, []),
ok = no_exceptions(rabbit, environment, []),
ok = rabbit_trace:stop(<<"/">>),
passed.
no_exceptions(Mod, Fun, Args) ->
try erlang:apply(Mod, Fun, Args) of _ -> ok
catch Type:Ex -> {Type, Ex}
end.
wait_for_application(Application) ->
wait_for_application(Application, 5000).
wait_for_application(_, Time) when Time =< 0 ->
{error, timeout};
wait_for_application(Application, Time) ->
Interval = 100,
case lists:keyfind(Application, 1, application:which_applications()) of
false ->
timer:sleep(Interval),
wait_for_application(Application, Time - Interval);
_ -> ok
end.
%% ---------------------------------------------------------------------------
%% file_handle_cache.
%% ---------------------------------------------------------------------------
file_handle_cache(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, file_handle_cache1, [Config]).
file_handle_cache1(_Config) ->
%% test copying when there is just one spare handle
Limit = file_handle_cache:get_limit(),
ok = file_handle_cache:set_limit(5), %% 1 or 2 sockets, 2 msg_stores
TmpDir = filename:join(rabbit_mnesia:dir(), "tmp"),
ok = filelib:ensure_dir(filename:join(TmpDir, "nothing")),
[Src1, Dst1, Src2, Dst2] = Files =
[filename:join(TmpDir, Str) || Str <- ["file1", "file2", "file3", "file4"]],
Content = <<"foo">>,
CopyFun = fun (Src, Dst) ->
{ok, Hdl} = prim_file:open(Src, [binary, write]),
ok = prim_file:write(Hdl, Content),
ok = prim_file:sync(Hdl),
prim_file:close(Hdl),
{ok, SrcHdl} = file_handle_cache:open(Src, [read], []),
{ok, DstHdl} = file_handle_cache:open(Dst, [write], []),
Size = size(Content),
{ok, Size} = file_handle_cache:copy(SrcHdl, DstHdl, Size),
ok = file_handle_cache:delete(SrcHdl),
ok = file_handle_cache:delete(DstHdl)
end,
Pid = spawn(fun () -> {ok, Hdl} = file_handle_cache:open(
filename:join(TmpDir, "file5"),
[write], []),
receive {next, Pid1} -> Pid1 ! {next, self()} end,
file_handle_cache:delete(Hdl),
%% This will block and never return, so we
%% exercise the fhc tidying up the pending
%% queue on the death of a process.
ok = CopyFun(Src1, Dst1)
end),
ok = CopyFun(Src1, Dst1),
ok = file_handle_cache:set_limit(2),
Pid ! {next, self()},
receive {next, Pid} -> ok end,
timer:sleep(100),
Pid1 = spawn(fun () -> CopyFun(Src2, Dst2) end),
timer:sleep(100),
erlang:monitor(process, Pid),
erlang:monitor(process, Pid1),
exit(Pid, kill),
exit(Pid1, kill),
receive {'DOWN', _MRef, process, Pid, _Reason} -> ok end,
receive {'DOWN', _MRef1, process, Pid1, _Reason1} -> ok end,
[file:delete(File) || File <- Files],
ok = file_handle_cache:set_limit(Limit),
passed.
%% -------------------------------------------------------------------
%% Log management.
%% -------------------------------------------------------------------
log_management(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, log_management1, [Config]).
log_management1(_Config) ->
[LogFile|_] = rabbit:log_locations(),
Suffix = ".0",
ok = test_logs_working([LogFile]),
%% prepare basic logs
file:delete(LogFile ++ Suffix),
ok = test_logs_working([LogFile]),
%% simple log rotation
ok = rabbit:rotate_logs(),
%% FIXME: rabbit:rotate_logs/0 is asynchronous due to a limitation
%% in Lager. Therefore, we have no choice but to wait an arbitrary
%% amount of time.
timer:sleep(2000),
[true, true] = non_empty_files([LogFile ++ Suffix, LogFile]),
ok = test_logs_working([LogFile]),
%% log rotation on empty files
ok = clean_logs([LogFile], Suffix),
ok = rabbit:rotate_logs(),
timer:sleep(2000),
?assertEqual([true, true], non_empty_files([LogFile ++ Suffix, LogFile])),
%% logs with suffix are not writable
ok = rabbit:rotate_logs(),
timer:sleep(2000),
ok = make_files_non_writable([LogFile ++ Suffix]),
ok = rabbit:rotate_logs(),
timer:sleep(2000),
ok = test_logs_working([LogFile]),
%% rotate when original log files are not writable
ok = make_files_non_writable([LogFile]),
ok = rabbit:rotate_logs(),
timer:sleep(2000),
%% logging directed to tty (first, remove handlers)
ok = rabbit:stop(),
ok = clean_logs([LogFile], Suffix),
ok = application:set_env(rabbit, lager_default_file, tty),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
ok = rabbit:start(),
timer:sleep(200),
rabbit_log:info("test info"),
%% rotate logs when logging is turned off
ok = rabbit:stop(),
ok = clean_logs([LogFile], Suffix),
ok = application:set_env(rabbit, lager_default_file, false),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
ok = rabbit:start(),
timer:sleep(200),
rabbit_log:error("test error"),
timer:sleep(200),
?assertEqual([{error,enoent}], empty_files([LogFile])),
%% cleanup
ok = rabbit:stop(),
ok = clean_logs([LogFile], Suffix),
ok = application:set_env(rabbit, lager_default_file, LogFile),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
ok = rabbit:start(),
ok = test_logs_working([LogFile]),
passed.
log_file_initialised_during_startup(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, log_file_initialised_during_startup1, [Config]).
log_file_initialised_during_startup1(_Config) ->
[LogFile|_] = rabbit:log_locations(),
Suffix = ".0",
%% start application with simple tty logging
ok = rabbit:stop(),
ok = clean_logs([LogFile], Suffix),
ok = application:set_env(rabbit, lager_default_file, tty),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
ok = rabbit:start(),
%% start application with logging to non-existing directory
NonExistent = "/tmp/non-existent/test.log",
delete_file(NonExistent),
delete_file(filename:dirname(NonExistent)),
ok = rabbit:stop(),
ok = application:set_env(rabbit, lager_default_file, NonExistent),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
ok = rabbit:start(),
%% clean up
ok = application:set_env(rabbit, lager_default_file, LogFile),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
ok = rabbit:start(),
passed.
log_file_fails_to_initialise_during_startup(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, log_file_fails_to_initialise_during_startup1, [Config]).
log_file_fails_to_initialise_during_startup1(_Config) ->
[LogFile|_] = rabbit:log_locations(),
%% start application with logging to directory with no
%% write permissions
ok = rabbit:stop(),
Run1 = fun() ->
NoPermission1 = "/var/empty/test.log",
delete_file(NoPermission1),
delete_file(filename:dirname(NoPermission1)),
ok = rabbit:stop(),
ok = application:set_env(rabbit, lager_default_file, NoPermission1),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
rabbit:start()
end,
ok = try Run1() of
ok -> exit({got_success_but_expected_failure,
log_rotation_no_write_permission_dir_test})
catch
_:could_not_initialise_logger -> ok
end,
%% start application with logging to a subdirectory which
%% parent directory has no write permissions
NoPermission2 = "/var/empty/non-existent/test.log",
Run2 = fun() ->
delete_file(NoPermission2),
delete_file(filename:dirname(NoPermission2)),
case rabbit:stop() of
ok -> ok;
{error, lager_not_running} -> ok
end,
ok = application:set_env(rabbit, lager_default_file, NoPermission2),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
rabbit:start()
end,
ok = try Run2() of
ok -> exit({got_success_but_expected_failure,
log_rotation_parent_dirs_test})
catch
_:could_not_initialise_logger -> ok
end,
%% clean up
ok = application:set_env(rabbit, lager_default_file, LogFile),
application:unset_env(rabbit, log),
application:unset_env(lager, handlers),
application:unset_env(lager, extra_sinks),
ok = rabbit:start(),
passed.
externally_rotated_logs_are_automatically_reopened(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, externally_rotated_logs_are_automatically_reopened1, [Config]).
externally_rotated_logs_are_automatically_reopened1(_Config) ->
[LogFile|_] = rabbit:log_locations(),
%% Make sure log file is opened
ok = test_logs_working([LogFile]),
%% Move it away - i.e. external log rotation happened
file:rename(LogFile, [LogFile, ".rotation_test"]),
%% New files should be created - test_logs_working/1 will check that
%% LogFile is not empty after doing some logging. And it's exactly
%% what we need to check here.
ok = test_logs_working([LogFile]),
passed.
empty_or_nonexist_files(Files) ->
[case file:read_file_info(File) of
{ok, FInfo} -> FInfo#file_info.size == 0;
{error, enoent} -> true;
Error -> Error
end || File <- Files].
empty_files(Files) ->
[case file:read_file_info(File) of
{ok, FInfo} -> FInfo#file_info.size == 0;
Error -> Error
end || File <- Files].
non_empty_files(Files) ->
[case EmptyFile of
{error, Reason} -> {error, Reason};
_ -> not(EmptyFile)
end || EmptyFile <- empty_files(Files)].
test_logs_working(LogFiles) ->
ok = rabbit_log:error("Log a test message"),
%% give the error loggers some time to catch up
timer:sleep(1000),
lists:all(fun(LogFile) -> [true] =:= non_empty_files([LogFile]) end, LogFiles),
ok.
set_permissions(Path, Mode) ->
case file:read_file_info(Path) of
{ok, FInfo} -> file:write_file_info(
Path,
FInfo#file_info{mode=Mode});
Error -> Error
end.
clean_logs(Files, Suffix) ->
[begin
ok = delete_file(File),
ok = delete_file([File, Suffix])
end || File <- Files],
ok.
delete_file(File) ->
case file:delete(File) of
ok -> ok;
{error, enoent} -> ok;
Error -> Error
end.
make_files_non_writable(Files) ->
[ok = file:write_file_info(File, #file_info{mode=8#444}) ||
File <- Files],
ok.
add_log_handlers(Handlers) ->
[ok = error_logger:add_report_handler(Handler, Args) ||
{Handler, Args} <- Handlers],
ok.
%% sasl_report_file_h returns [] during terminate
%% see: https://github.com/erlang/otp/blob/maint/lib/stdlib/src/error_logger_file_h.erl#L98
%%
%% error_logger_file_h returns ok since OTP 18.1
%% see: https://github.com/erlang/otp/blob/maint/lib/stdlib/src/error_logger_file_h.erl#L98
delete_log_handlers(Handlers) ->
[ok_or_empty_list(error_logger:delete_report_handler(Handler))
|| Handler <- Handlers],
ok.
ok_or_empty_list([]) ->
[];
ok_or_empty_list(ok) ->
ok.
%% -------------------------------------------------------------------
%% Statistics.
%% -------------------------------------------------------------------
channel_statistics(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, channel_statistics1, [Config]).
channel_statistics1(_Config) ->
application:set_env(rabbit, collect_statistics, fine),
%% ATM this just tests the queue / exchange stats in channels. That's
%% by far the most complex code though.
%% Set up a channel and queue
{_Writer, Ch} = test_spawn(),
rabbit_channel:do(Ch, #'queue.declare'{}),
QName = receive #'queue.declare_ok'{queue = Q0} -> Q0
after ?TIMEOUT -> throw(failed_to_receive_queue_declare_ok)
end,
QRes = rabbit_misc:r(<<"/">>, queue, QName),
X = rabbit_misc:r(<<"/">>, exchange, <<"">>),
dummy_event_receiver:start(self(), [node()], [channel_stats]),
%% Check stats empty
Check1 = fun() ->
[] = ets:match(channel_queue_metrics, {Ch, QRes}),
[] = ets:match(channel_exchange_metrics, {Ch, X}),
[] = ets:match(channel_queue_exchange_metrics,
{Ch, {QRes, X}})
end,
test_ch_metrics(Check1, ?TIMEOUT),
%% Publish and get a message
rabbit_channel:do(Ch, #'basic.publish'{exchange = <<"">>,
routing_key = QName},
rabbit_basic:build_content(#'P_basic'{}, <<"">>)),
rabbit_channel:do(Ch, #'basic.get'{queue = QName}),
%% Check the stats reflect that
Check2 = fun() ->
[{{Ch, QRes}, 1, 0, 0, 0, 0, 0, 0}] = ets:lookup(
channel_queue_metrics,
{Ch, QRes}),
[{{Ch, X}, 1, 0, 0, 0}] = ets:lookup(
channel_exchange_metrics,
{Ch, X}),
[{{Ch, {QRes, X}}, 1, 0}] = ets:lookup(
channel_queue_exchange_metrics,
{Ch, {QRes, X}})
end,
test_ch_metrics(Check2, ?TIMEOUT),
%% Check the stats are marked for removal on queue deletion.
rabbit_channel:do(Ch, #'queue.delete'{queue = QName}),
Check3 = fun() ->
[{{Ch, QRes}, 1, 0, 0, 0, 0, 0, 1}] = ets:lookup(
channel_queue_metrics,
{Ch, QRes}),
[{{Ch, X}, 1, 0, 0, 0}] = ets:lookup(
channel_exchange_metrics,
{Ch, X}),
[{{Ch, {QRes, X}}, 1, 1}] = ets:lookup(
channel_queue_exchange_metrics,
{Ch, {QRes, X}})
end,
test_ch_metrics(Check3, ?TIMEOUT),
%% Check the garbage collection removes stuff.
force_metric_gc(),
Check4 = fun() ->
[] = ets:lookup(channel_queue_metrics, {Ch, QRes}),
[{{Ch, X}, 1, 0, 0, 0}] = ets:lookup(
channel_exchange_metrics,
{Ch, X}),
[] = ets:lookup(channel_queue_exchange_metrics,
{Ch, {QRes, X}})
end,
test_ch_metrics(Check4, ?TIMEOUT),
rabbit_channel:shutdown(Ch),
dummy_event_receiver:stop(),
passed.
force_metric_gc() ->
timer:sleep(300),
rabbit_core_metrics_gc ! start_gc,
gen_server:call(rabbit_core_metrics_gc, test).
test_ch_metrics(Fun, Timeout) when Timeout =< 0 ->
Fun();
test_ch_metrics(Fun, Timeout) ->
try
Fun()
catch
_:{badmatch, _} ->
timer:sleep(1000),
test_ch_metrics(Fun, Timeout - 1000)
end.
head_message_timestamp_statistics(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, head_message_timestamp1, [Config]).
head_message_timestamp1(_Config) ->
%% there is no convenient rabbit_channel API for confirms
%% this test could use, so it relies on tx.* methods
%% and gen_server2 flushing
application:set_env(rabbit, collect_statistics, fine),
%% Set up a channel and queue
{_Writer, Ch} = test_spawn(),
rabbit_channel:do(Ch, #'queue.declare'{}),
QName = receive #'queue.declare_ok'{queue = Q0} -> Q0
after ?TIMEOUT -> throw(failed_to_receive_queue_declare_ok)
end,
QRes = rabbit_misc:r(<<"/">>, queue, QName),
{ok, Q1} = rabbit_amqqueue:lookup(QRes),
QPid = Q1#amqqueue.pid,
%% Set up event receiver for queue
dummy_event_receiver:start(self(), [node()], [queue_stats]),
%% the head timestamp field is empty when queue is empty empty
test_queue_statistics_receive_event(QPid,
fun (E) ->
(proplists:get_value(name, E) == QRes)
and
(proplists:get_value(head_message_timestamp, E) == '')
end),
rabbit_channel:do(Ch, #'tx.select'{}),
receive #'tx.select_ok'{} -> ok
after ?TIMEOUT -> throw(failed_to_receive_tx_select_ok)
end,
%% Publish two messages and check that the timestamp is that of first message
rabbit_channel:do(Ch, #'basic.publish'{exchange = <<"">>,
routing_key = QName},
rabbit_basic:build_content(#'P_basic'{timestamp = 1}, <<"">>)),
rabbit_channel:do(Ch, #'basic.publish'{exchange = <<"">>,
routing_key = QName},
rabbit_basic:build_content(#'P_basic'{timestamp = 2}, <<"">>)),
rabbit_channel:do(Ch, #'tx.commit'{}),
rabbit_channel:flush(Ch),
receive #'tx.commit_ok'{} -> ok
after ?TIMEOUT -> throw(failed_to_receive_tx_commit_ok)
end,
test_queue_statistics_receive_event(QPid,
fun (E) ->
(proplists:get_value(name, E) == QRes)
and
(proplists:get_value(head_message_timestamp, E) == 1)
end),
%% Consume a message and check that the timestamp is now that of the second message
rabbit_channel:do(Ch, #'basic.get'{queue = QName, no_ack = true}),
test_queue_statistics_receive_event(QPid,
fun (E) ->
(proplists:get_value(name, E) == QRes)
and
(proplists:get_value(head_message_timestamp, E) == 2)
end),
%% Consume one more message and check again
rabbit_channel:do(Ch, #'basic.get'{queue = QName, no_ack = true}),
test_queue_statistics_receive_event(QPid,
fun (E) ->
(proplists:get_value(name, E) == QRes)
and
(proplists:get_value(head_message_timestamp, E) == '')
end),
%% Teardown
rabbit_channel:do(Ch, #'queue.delete'{queue = QName}),
rabbit_channel:shutdown(Ch),
dummy_event_receiver:stop(),
passed.
test_queue_statistics_receive_event(Q, Matcher) ->
%% Q ! emit_stats,
test_queue_statistics_receive_event1(Q, Matcher).
test_queue_statistics_receive_event1(Q, Matcher) ->
receive #event{type = queue_stats, props = Props} ->
case Matcher(Props) of
true -> Props;
_ -> test_queue_statistics_receive_event1(Q, Matcher)
end
after ?TIMEOUT -> throw(failed_to_receive_event)
end.
test_spawn() ->
{Writer, _Limiter, Ch} = rabbit_ct_broker_helpers:test_channel(),
ok = rabbit_channel:do(Ch, #'channel.open'{}),
receive #'channel.open_ok'{} -> ok
after ?TIMEOUT -> throw(failed_to_receive_channel_open_ok)
end,
{Writer, Ch}.
disk_monitor(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, disk_monitor1, [Config]).
disk_monitor1(_Config) ->
%% Issue: rabbitmq-server #91
%% os module could be mocked using 'unstick', however it may have undesired
%% side effects in following tests. Thus, we mock at rabbit_misc level
ok = meck:new(rabbit_misc, [passthrough]),
ok = meck:expect(rabbit_misc, os_cmd, fun(_) -> "\n" end),
ok = rabbit_sup:stop_child(rabbit_disk_monitor_sup),
ok = rabbit_sup:start_delayed_restartable_child(rabbit_disk_monitor, [1000]),
meck:unload(rabbit_misc),
passed.
disk_monitor_enable(Config) ->
passed = rabbit_ct_broker_helpers:rpc(Config, 0,
?MODULE, disk_monitor_enable1, [Config]).
disk_monitor_enable1(_Config) ->
case os:type() of
{unix, _} ->
disk_monitor_enable1();
_ ->
%% skip windows testing
skipped
end.
disk_monitor_enable1() ->
ok = meck:new(rabbit_misc, [passthrough]),
ok = meck:expect(rabbit_misc, os_cmd, fun(_) -> "\n" end),
application:set_env(rabbit, disk_monitor_failure_retries, 20000),
application:set_env(rabbit, disk_monitor_failure_retry_interval, 100),
ok = rabbit_sup:stop_child(rabbit_disk_monitor_sup),
ok = rabbit_sup:start_delayed_restartable_child(rabbit_disk_monitor, [1000]),
undefined = rabbit_disk_monitor:get_disk_free(),
Cmd = "Filesystem 1024-blocks Used Available Capacity iused ifree %iused Mounted on\n/dev/disk1 975798272 234783364 740758908 25% 58759839 185189727 24% /\n",
ok = meck:expect(rabbit_misc, os_cmd, fun(_) -> Cmd end),
timer:sleep(1000),
Bytes = 740758908 * 1024,
Bytes = rabbit_disk_monitor:get_disk_free(),
meck:unload(rabbit_misc),
application:set_env(rabbit, disk_monitor_failure_retries, 10),
application:set_env(rabbit, disk_monitor_failure_retry_interval, 120000),
passed.
%% ---------------------------------------------------------------------------
%% rabbitmqctl helpers.
%% ---------------------------------------------------------------------------
default_options() -> [{"-p", "/"}, {"-q", "false"}].
expand_options(As, Bs) ->
lists:foldl(fun({K, _}=A, R) ->
case proplists:is_defined(K, R) of
true -> R;
false -> [A | R]
end
end, Bs, As).
|