Skip to content

Commit 1c8798a

Browse files
Merge pull request #1268 from rabbitmq/rabbitmq-server-1243
Make term_to_binary_compat generate binaries using minor_version 1.
2 parents 2c6edde + 0e148da commit 1c8798a

File tree

3 files changed

+50
-24
lines changed

3 files changed

+50
-24
lines changed

src/rabbit_queue_index.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ recover_message(false, _, no_del, RelSeq, {Segment, DirtyCount}) ->
653653
DirtyCount + 2}.
654654

655655
queue_name_to_dir_name(Name = #resource { kind = queue }) ->
656-
<<Num:128>> = erlang:md5(term_to_binary_compat:queue_name_to_binary(Name)),
656+
<<Num:128>> = erlang:md5(term_to_binary_compat:term_to_binary_1(Name)),
657657
rabbit_misc:format("~.36B", [Num]).
658658

659659
queues_dir() ->

src/term_to_binary_compat.erl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,8 @@
1818

1919
-include("rabbit.hrl").
2020

21-
-export([queue_name_to_binary/1]).
21+
-export([term_to_binary_1/1]).
2222

23-
queue_name_to_binary(#resource{kind = queue} = {resource, VHost, queue, Name}) ->
24-
VHostBSize = byte_size(VHost),
25-
NameBSize = byte_size(Name),
26-
<<131, %% Binary format "version"
27-
104, 4, %% 4-element tuple
28-
100, 0, 8, "resource", %% `resource` atom
29-
109, VHostBSize:32, VHost/binary, %% Vhost binary
30-
100, 0, 5, "queue", %% `queue` atom
31-
109, NameBSize:32, Name/binary>>. %% Name binary
23+
term_to_binary_1(Term) ->
24+
term_to_binary(Term, [{minor_version, 1}]).
3225

test/term_to_binary_compat_prop_SUITE.erl

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
-include_lib("proper/include/proper.hrl").
2525

2626
all() ->
27-
%% The test should run on OTP < 20 (erts < 9)
28-
case erts_gt_8() of
29-
true ->
30-
[];
31-
false ->
32-
[queue_name_to_binary]
33-
end.
27+
[
28+
pre_3_6_11_works,
29+
term_to_binary_latin_atom,
30+
queue_name_to_binary
31+
].
3432

3533
erts_gt_8() ->
3634
Vsn = erlang:system_info(version),
@@ -47,16 +45,51 @@ end_per_suite(Config) ->
4745
init_per_testcase(Testcase, Config) ->
4846
rabbit_ct_helpers:testcase_started(Config, Testcase).
4947

48+
%% If this test fails - the erlang version is not supported in
49+
%% RabbitMQ-3.6.10 and earlier.
50+
pre_3_6_11_works(Config) ->
51+
Fun = fun () -> prop_pre_3_6_11_works(Config) end,
52+
rabbit_ct_proper_helpers:run_proper(Fun, [], 50000).
53+
54+
prop_pre_3_6_11_works(_Config) ->
55+
?FORALL(Term, any(),
56+
begin
57+
Current = term_to_binary(Term),
58+
Compat = term_to_binary_compat:term_to_binary_1(Term),
59+
Current =:= Compat
60+
end).
61+
62+
term_to_binary_latin_atom(Config) ->
63+
Fun = fun () -> prop_term_to_binary_latin_atom(Config) end,
64+
rabbit_ct_proper_helpers:run_proper(Fun, [], 10000).
65+
66+
prop_term_to_binary_latin_atom(_Config) ->
67+
?FORALL(LatinString, list(integer(0, 255)),
68+
begin
69+
Length = length(LatinString),
70+
Atom = list_to_atom(LatinString),
71+
Binary = list_to_binary(LatinString),
72+
<<131,100, Length:16, Binary/binary>> =:= term_to_binary_compat:term_to_binary_1(Atom)
73+
end).
74+
5075
queue_name_to_binary(Config) ->
5176
Fun = fun () -> prop_queue_name_to_binary(Config) end,
5277
rabbit_ct_proper_helpers:run_proper(Fun, [], 10000).
5378

5479

5580
prop_queue_name_to_binary(_Config) ->
56-
?FORALL({Vhost, QName}, {binary(), binary()},
81+
?FORALL({VHost, QName}, {binary(), binary()},
5782
begin
58-
Resource = rabbit_misc:r(Vhost, queue, QName),
59-
Legacy = term_to_binary_compat:queue_name_to_binary(Resource),
60-
Current = term_to_binary(Resource),
61-
Current =:= Legacy
62-
end).
83+
VHostBSize = byte_size(VHost),
84+
NameBSize = byte_size(QName),
85+
Expected =
86+
<<131, %% Binary format "version"
87+
104, 4, %% 4-element tuple
88+
100, 0, 8, "resource", %% `resource` atom
89+
109, VHostBSize:32, VHost/binary, %% Vhost binary
90+
100, 0, 5, "queue", %% `queue` atom
91+
109, NameBSize:32, QName/binary>>, %% Name binary
92+
Resource = rabbit_misc:r(VHost, queue, QName),
93+
Current = term_to_binary_compat:term_to_binary_1(Resource),
94+
Current =:= Expected
95+
end).

0 commit comments

Comments
 (0)