|
| 1 | +-module(rabbit_repro). |
| 2 | + |
| 3 | +-export([run/0]). |
| 4 | + |
| 5 | +-include_lib("amqp_client/include/amqp_client.hrl"). |
| 6 | + |
| 7 | +-define(Q, <<"qq">>). |
| 8 | +-define(N_MESSAGES, 10). |
| 9 | + |
| 10 | +run() -> |
| 11 | + {_, BasicGetRunnerRef} = spawn_monitor(fun() -> |
| 12 | + {ok, Conn} = connection(), |
| 13 | + link(Conn), |
| 14 | + {ok, Ch} = amqp_connection:open_channel(Conn), |
| 15 | + link(Ch), |
| 16 | + |
| 17 | + %% Get 10 messages and then exit without checking them out so that |
| 18 | + %% they are returned. |
| 19 | + basic_get(Ch, ?N_MESSAGES), |
| 20 | + ok |
| 21 | + end), |
| 22 | + |
| 23 | + receive |
| 24 | + {'DOWN', BasicGetRunnerRef, process, _, normal} -> |
| 25 | + ok; |
| 26 | + {'DOWN', BasicGetRunnerRef, process, _, _} -> |
| 27 | + exit(basic_get_runner_fail) |
| 28 | + end, |
| 29 | + |
| 30 | + %% Probably not necessary but let's let everything settle. Wait for 1/2 |
| 31 | + %% second. |
| 32 | + timer:sleep(500), |
| 33 | + |
| 34 | + {_, BasicRejectRunnerRef} = spawn_monitor(fun() -> |
| 35 | + {ok, Conn} = connection(), |
| 36 | + link(Conn), |
| 37 | + {ok, Ch} = amqp_connection:open_channel(Conn), |
| 38 | + link(Ch), |
| 39 | + |
| 40 | + %% Get and ack 9 messages. |
| 41 | + {some, AckDeliveryTag} = basic_get(Ch, ?N_MESSAGES - 1), |
| 42 | + amqp_channel:cast(Ch, #'basic.ack'{delivery_tag = AckDeliveryTag, |
| 43 | + multiple = true}), |
| 44 | + |
| 45 | + %% Then get and reject the last returned message. |
| 46 | + {some, RejectDeliveryTag} = basic_get(Ch, 1), |
| 47 | + amqp_channel:cast(Ch, #'basic.reject'{delivery_tag = RejectDeliveryTag, |
| 48 | + requeue = true}), |
| 49 | + ok |
| 50 | + end), |
| 51 | + |
| 52 | + receive |
| 53 | + {'DOWN', BasicRejectRunnerRef, process, _, normal} -> |
| 54 | + ok; |
| 55 | + {'DOWN', BasicRejectRunnerRef, process, _, _} -> |
| 56 | + exit(basic_reject_runner_fail) |
| 57 | + end, |
| 58 | + |
| 59 | + ok. |
| 60 | + |
| 61 | +connection() -> |
| 62 | + amqp_connection:start(#amqp_params_direct{virtual_host = <<"/">>, |
| 63 | + username = <<"guest">>, |
| 64 | + password = <<"guest">>}). |
| 65 | + |
| 66 | +basic_get(Ch, N) -> |
| 67 | + basic_get(Ch, N, none). |
| 68 | + |
| 69 | +basic_get(_Ch, 0, Tag) -> |
| 70 | + Tag; |
| 71 | +basic_get(Ch, N, Tag0) -> |
| 72 | + Tag = case amqp_channel:call(Ch, #'basic.get'{queue = ?Q}) of |
| 73 | + {#'basic.get_ok'{delivery_tag = DeliveryTag}, _} -> |
| 74 | + {some, DeliveryTag}; |
| 75 | + #'basic.get_empty'{} -> |
| 76 | + Tag0 |
| 77 | + end, |
| 78 | + basic_get(Ch, N - 1, Tag). |
0 commit comments