Skip to content

Commit b240a35

Browse files
committed
docs: add comprehensive documentation for exchange examples
- Add detailed doc comments explaining the direct exchange example, including steps and expected output - Add detailed doc comments explaining the fanout exchange example, including steps and expected output Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 4f629b4 commit b240a35

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

example_test.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,32 @@ import (
99
"github.com/golang-queue/queue/core"
1010
)
1111

12-
// Direct Exchange
12+
/*
13+
Example_direct_exchange demonstrates how to use RabbitMQ with a direct exchange.
14+
This example creates two workers (w1, w2) that both listen to the same queue and exchange,
15+
and a producer that sends multiple messages to the queue. The workers process the messages
16+
in a round-robin fashion.
17+
18+
Steps:
19+
1. Create a mock message to be sent.
20+
2. Initialize worker w1 with a direct exchange and queue, and define its processing function.
21+
3. Start a queue (q1) with worker w1.
22+
4. Initialize worker w2 with the same direct exchange and queue, and define its processing function.
23+
5. Start a queue (q2) with worker w2.
24+
6. Create a producer worker (w) with the same exchange and routing key.
25+
7. Start a queue (q) with the producer worker.
26+
8. Send the mock message to the queue multiple times.
27+
9. Wait for processing, then release all queues.
28+
29+
Expected Output:
30+
- The two workers alternately print the received message.
31+
32+
Unordered Output:
33+
worker01 get data: foo
34+
worker02 get data: foo
35+
worker01 get data: foo
36+
worker02 get data: foo
37+
*/
1338
func Example_direct_exchange() {
1439
m := mockMessage{
1540
Message: "foo",
@@ -92,7 +117,30 @@ func Example_direct_exchange() {
92117
// worker02 get data: foo
93118
}
94119

95-
// Fanout Exchange
120+
/*
121+
Example_fanout_exchange demonstrates how to use RabbitMQ with a fanout exchange.
122+
This example creates two workers (w1, w2) each listening to a different queue bound to the same
123+
fanout exchange, and a producer that sends a message to the exchange. Both workers receive and
124+
process the same message.
125+
126+
Steps:
127+
1. Create a mock message to be sent.
128+
2. Initialize worker w1 with a unique queue and the fanout exchange, and define its processing function.
129+
3. Start a queue (q1) with worker w1.
130+
4. Initialize worker w2 with a different queue and the same fanout exchange, and define its processing function.
131+
5. Start a queue (q2) with worker w2.
132+
6. Create a producer worker (w) with the same fanout exchange.
133+
7. Start a queue (q) with the producer worker.
134+
8. Send the mock message to the exchange.
135+
9. Wait for processing, then release all queues.
136+
137+
Expected Output:
138+
- Both workers print the received message.
139+
140+
Unordered Output:
141+
worker01 get data: foo
142+
worker02 get data: foo
143+
*/
96144
func Example_fanout_exchange() {
97145
m := mockMessage{
98146
Message: "foo",

0 commit comments

Comments
 (0)