Skip to content

Commit c8a5d28

Browse files
author
DanielePalaia
committed
test for gabriele
1 parent 6bd36fd commit c8a5d28

File tree

2 files changed

+84
-21
lines changed

2 files changed

+84
-21
lines changed

examples/getting_started/main.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def main():
12-
exchange_name = "example-exchange"
12+
exchange_name = "getting-started-exchange"
1313
queue_name = "example-queue"
1414
connection = Connection("amqp://guest:guest@localhost:5672/")
1515

@@ -21,9 +21,9 @@ def main():
2121
ExchangeSpecification(name=exchange_name, arguments={})
2222
)
2323

24-
queue_info = management.declare_queue(
25-
QueueSpecification(name=queue_name, queue_type=QueueType.quorum, arguments={})
26-
)
24+
#queue_info = management.declare_queue(
25+
# QueueSpecification(name=queue_name, queue_type=QueueType.quorum, arguments={})
26+
#)
2727

2828
"""
2929
#management.bind(BindingSpecification{
@@ -59,13 +59,9 @@ def main():
5959
management.purge_queue(queue_info.name)
6060
"""
6161

62-
"""
63-
management.delete_queue(queue_info.name)
64-
"""
62+
#management.delete_queue(queue_name)
6563

66-
"""
67-
management.delete_exchange(exchange_info.name)
68-
"""
64+
#management.delete_exchange(exchange_name)
6965

7066
management.close()
7167

rabbitmq_amqp_python_client/management.py

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import uuid
22
from typing import Any, Optional
3-
3+
import json
44
from proton import Message, Receiver, Sender
55
from proton.utils import (
66
BlockingConnection,
77
BlockingReceiver,
88
BlockingSender,
99
)
1010

11+
from proton._data import Data
12+
1113
from .address_helper import exchange_address, queue_address
1214
from .common import CommonValues
1315
from .configuration_options import (
@@ -19,11 +21,12 @@
1921
QueueSpecification,
2022
)
2123

24+
import pickle
2225

2326
class Management:
2427
def __init__(self, conn: BlockingConnection):
25-
self._sender: Optional[Sender] = None
26-
self._receiver: Optional[Receiver] = None
28+
self._sender: Optional[BlockingSender] = None
29+
self._receiver: Optional[BlockingReceiver] = None
2730
self._conn = conn
2831

2932
def open(self) -> None:
@@ -67,25 +70,46 @@ def _request(
6770
method: str,
6871
expected_response_codes: list[int],
6972
) -> None:
73+
print("path is: " + path)
74+
75+
## test exchange message
7076
amq_message = Message(
71-
id=id,
77+
id='84caea92-8e38-41d4-993f-de12b2a3d9a2',
7278
body=body,
7379
reply_to="$me",
7480
address=path,
7581
subject=method,
76-
properties={"id": id, "to": path, "subject": method, "reply_to": "$me"},
82+
#properties={"id": id, "to": path, "subject": method, "reply_to": "$me"},
83+
)
84+
85+
## test empty message
86+
amq_message = Message(
87+
#id='84caea92-8e38-41d4-993f-de12b2a3d9a2',
88+
body=Data.NULL,
89+
#reply_to="$me",
90+
#address=path,
91+
#subject=method,
92+
#properties={"id": id, "to": path, "subject": method, "reply_to": "$me"},
7793
)
7894

79-
print("message: " + str(amq_message))
95+
message_bytes= amq_message.encode()
96+
97+
#print("received " + str(message_bytes.format(binary)))
98+
99+
list_bytes = list(message_bytes)
100+
print("message: " + str(list_bytes))
80101

81102
if self._sender is not None:
82-
print("sending: " + method)
83103
self._sender.send(amq_message)
84104

85105
msg = self._receiver.receive()
86106

107+
#message_bytes= msg.encode()
108+
87109
print("received " + str(msg))
88110

111+
#self._validate_reponse_code(int(msg.properties["http:response"]), expected_response_codes)
112+
89113
# TO_COMPLETE HERE
90114

91115
# TODO
@@ -96,8 +120,8 @@ def declare_exchange(self, exchange_specification: ExchangeSpecification):
96120
body["auto_delete"] = exchange_specification.is_auto_delete
97121
body["durable"] = exchange_specification.is_durable
98122
body["type"] = exchange_specification.exchange_type.value
99-
body["internal"] = False
100-
body["arguments"] = exchange_specification.arguments
123+
#body["internal"] = False
124+
body["arguments"] = {}
101125

102126
path = exchange_address(exchange_specification.name)
103127

@@ -140,8 +164,51 @@ def declare_queue(self, queue_specification: QueueSpecification):
140164
],
141165
)
142166

143-
# TODO
144-
# def delete_exchange(self, name:str):
167+
def delete_exchange(self, exchange_name:str):
168+
169+
path = exchange_address(exchange_name)
170+
171+
print(path)
172+
173+
self.request(
174+
Data.NULL,
175+
path,
176+
CommonValues.command_delete.value,
177+
[
178+
CommonValues.response_code_200.value,
179+
],
180+
)
181+
182+
183+
def delete_queue(self, queue_name:str):
184+
185+
path = queue_address(queue_name)
186+
187+
print(path)
188+
189+
self.request(
190+
None,
191+
path,
192+
CommonValues.command_delete.value,
193+
[
194+
CommonValues.response_code_200.value,
195+
],
196+
)
197+
198+
def _validate_reponse_code(self, response_code: int, expected_response_codes: list[int]) -> None:
199+
200+
print("response code: " + str(response_code))
201+
202+
if response_code == CommonValues.response_code_409:
203+
# TODO replace with a new defined Exception
204+
raise Exception("ErrPreconditionFailed")
205+
206+
for code in expected_response_codes:
207+
if code == response_code:
208+
return None
209+
210+
raise Exception("wrong response code received")
211+
145212

146213
# TODO
147214
# def bind(self, bind_specification:BindSpecification):

0 commit comments

Comments
 (0)