|
| 1 | +module AMQPTestRPC |
| 2 | + |
| 3 | +using AMQPClient |
| 4 | +using Base.Test |
| 5 | + |
| 6 | +const QUEUE_RPC = "queue_rpc" |
| 7 | + |
| 8 | +testlog(msg) = println(msg) |
| 9 | +reply_queue_id = 0 |
| 10 | +server_id = 0 |
| 11 | + |
| 12 | +const NRPC_MSGS = 100 |
| 13 | +const NRPC_CLNTS = 4 |
| 14 | +const NRPC_SRVRS = 4 |
| 15 | + |
| 16 | +function test_rpc_client(;virtualhost="/", host="localhost", port=AMQPClient.AMQP_DEFAULT_PORT, auth_params=AMQPClient.DEFAULT_AUTH_PARAMS) |
| 17 | + # open a connection |
| 18 | + testlog("client opening connection...") |
| 19 | + conn = connection(;virtualhost=virtualhost, host=host, port=port, auth_params=auth_params) |
| 20 | + |
| 21 | + # open a channel |
| 22 | + testlog("client opening channel...") |
| 23 | + chan1 = channel(conn, AMQPClient.UNUSED_CHANNEL, true) |
| 24 | + |
| 25 | + # create a reply queue for a client |
| 26 | + global reply_queue_id |
| 27 | + reply_queue_id += 1 |
| 28 | + queue_name = QUEUE_RPC * "_" * string(reply_queue_id) * "_" * string(getpid()) |
| 29 | + testlog("client creating queue " * queue_name * "...") |
| 30 | + success, message_count, consumer_count = queue_declare(chan1, queue_name; exclusive=true) |
| 31 | + |
| 32 | + testlog("client testing rpc...") |
| 33 | + rpc_reply_count = 0 |
| 34 | + rpc_fn = (rcvd_msg) -> begin |
| 35 | + rpc_reply_count += 1 |
| 36 | + |
| 37 | + msg_str = String(rcvd_msg.data) |
| 38 | + println("client ", msg_str) |
| 39 | + |
| 40 | + basic_ack(chan1, rcvd_msg.delivery_tag) |
| 41 | + end |
| 42 | + |
| 43 | + # start a consumer task |
| 44 | + success, consumer_tag = basic_consume(chan1, queue_name, rpc_fn) |
| 45 | + @test success |
| 46 | + |
| 47 | + correlation_id = 0 |
| 48 | + |
| 49 | + # publish NRPC_MSGS messages to the queue |
| 50 | + while correlation_id < NRPC_MSGS |
| 51 | + correlation_id += 1 |
| 52 | + M = Message(convert(Vector{UInt8}, "hello from " * queue_name), content_type="text/plain", delivery_mode=PERSISTENT, reply_to=queue_name, correlation_id=string(correlation_id)) |
| 53 | + basic_publish(chan1, M; exchange=default_exchange_name(), routing_key=QUEUE_RPC) |
| 54 | + # sleep a random time between 1 and 5 seconds between requests |
| 55 | + sleep(rand()) |
| 56 | + end |
| 57 | + |
| 58 | + while (rpc_reply_count < NRPC_MSGS) |
| 59 | + sleep(1) |
| 60 | + end |
| 61 | + |
| 62 | + testlog("client closing down...") |
| 63 | + success, message_count = queue_purge(chan1, queue_name) |
| 64 | + @test success |
| 65 | + @test message_count == 0 |
| 66 | + |
| 67 | + @test basic_cancel(chan1, consumer_tag) |
| 68 | + |
| 69 | + success, message_count = queue_delete(chan1, queue_name) |
| 70 | + @test success |
| 71 | + @test message_count == 0 |
| 72 | + |
| 73 | + # close channels and connection |
| 74 | + close(chan1) |
| 75 | + AMQPClient.wait_for_state(chan1, AMQPClient.CONN_STATE_CLOSED) |
| 76 | + @test !isopen(chan1) |
| 77 | + |
| 78 | + close(conn) |
| 79 | + AMQPClient.wait_for_state(conn, AMQPClient.CONN_STATE_CLOSED) |
| 80 | + @test !isopen(conn) |
| 81 | + |
| 82 | + testlog("client done.") |
| 83 | +end |
| 84 | + |
| 85 | +function test_rpc_server(;virtualhost="/", host="localhost", port=AMQPClient.AMQP_DEFAULT_PORT, auth_params=AMQPClient.DEFAULT_AUTH_PARAMS) |
| 86 | + global server_id |
| 87 | + server_id += 1 |
| 88 | + my_server_id = server_id |
| 89 | + |
| 90 | + # open a connection |
| 91 | + testlog("server $my_server_id opening connection...") |
| 92 | + conn = connection(;virtualhost=virtualhost, host=host, port=port, auth_params=auth_params) |
| 93 | + |
| 94 | + # open a channel |
| 95 | + testlog("server $my_server_id opening channel...") |
| 96 | + chan1 = channel(conn, AMQPClient.UNUSED_CHANNEL, true) |
| 97 | + |
| 98 | + # create queues (no need to bind if we are using the default exchange) |
| 99 | + testlog("server $my_server_id creating queues...") |
| 100 | + # this is the callback queue |
| 101 | + success, message_count, consumer_count = queue_declare(chan1, QUEUE_RPC) |
| 102 | + @test success |
| 103 | + |
| 104 | + # test RPC |
| 105 | + testlog("server $my_server_id testing rpc...") |
| 106 | + global rpc_count = 0 |
| 107 | + rpc_fn = (rcvd_msg) -> begin |
| 108 | + global rpc_count |
| 109 | + rpc_count += 1 |
| 110 | + |
| 111 | + @test :reply_to in keys(rcvd_msg.properties) |
| 112 | + reply_to = convert(String, rcvd_msg.properties[:reply_to]) |
| 113 | + correlation_id = convert(String, rcvd_msg.properties[:correlation_id]) |
| 114 | + |
| 115 | + resp_str = "$(my_server_id) received msg $(rpc_count) - $(reply_to): $(String(rcvd_msg.data))" |
| 116 | + println("server ", resp_str) |
| 117 | + |
| 118 | + M = Message(convert(Vector{UInt8}, resp_str), content_type="text/plain", delivery_mode=PERSISTENT, correlation_id=correlation_id) |
| 119 | + basic_publish(chan1, M; exchange=default_exchange_name(), routing_key=reply_to) |
| 120 | + |
| 121 | + basic_ack(chan1, rcvd_msg.delivery_tag) |
| 122 | + end |
| 123 | + |
| 124 | + # start a consumer task |
| 125 | + success, consumer_tag = basic_consume(chan1, QUEUE_RPC, rpc_fn) |
| 126 | + @test success |
| 127 | + |
| 128 | + while (rpc_count < NRPC_MSGS*NRPC_CLNTS) |
| 129 | + sleep(1) |
| 130 | + end |
| 131 | + |
| 132 | + testlog("server $my_server_id closing down...") |
| 133 | + success, message_count = queue_purge(chan1, QUEUE_RPC) |
| 134 | + @test success |
| 135 | + @test message_count == 0 |
| 136 | + |
| 137 | + @test basic_cancel(chan1, consumer_tag) |
| 138 | + |
| 139 | + success, message_count = queue_delete(chan1, QUEUE_RPC) |
| 140 | + @test success |
| 141 | + @test message_count == 0 |
| 142 | + testlog("server $my_server_id deleted rpc queue") |
| 143 | + |
| 144 | + # close channels and connection |
| 145 | + close(chan1) |
| 146 | + AMQPClient.wait_for_state(chan1, AMQPClient.CONN_STATE_CLOSED) |
| 147 | + @test !isopen(chan1) |
| 148 | + |
| 149 | + close(conn) |
| 150 | + AMQPClient.wait_for_state(conn, AMQPClient.CONN_STATE_CLOSED) |
| 151 | + @test !isopen(conn) |
| 152 | + |
| 153 | + testlog("server $my_server_id done.") |
| 154 | + nothing |
| 155 | +end |
| 156 | + |
| 157 | +function runtests() |
| 158 | + testlog("testing multiple client server rpc") |
| 159 | + clients = Vector{Task}() |
| 160 | + servers = Vector{Task}() |
| 161 | + |
| 162 | + for idx in 1:NRPC_SRVRS |
| 163 | + push!(servers, @async test_rpc_server()) |
| 164 | + end |
| 165 | + |
| 166 | + for idx in 1:NRPC_CLNTS |
| 167 | + push!(clients, @async test_rpc_client()) |
| 168 | + end |
| 169 | + |
| 170 | + tasks_active = NRPC_CLNTS + NRPC_SRVRS |
| 171 | + while tasks_active > 0 |
| 172 | + tasks_active = 0 |
| 173 | + for idx in 1:NRPC_SRVRS |
| 174 | + istaskdone(servers[idx]) || (tasks_active += 1) |
| 175 | + end |
| 176 | + for idx in 1:NRPC_CLNTS |
| 177 | + istaskdone(clients[idx]) || (tasks_active += 1) |
| 178 | + end |
| 179 | + sleep(5) |
| 180 | + end |
| 181 | + testlog("done") |
| 182 | +end |
| 183 | +end # module AMQPTestRPC |
0 commit comments