1+ using System ;
2+ using System . Linq ;
3+ using System . Threading . Tasks ;
4+ using ActiveMQ . Artemis . Client . TestUtils ;
5+ using Xunit ;
6+ using Xunit . Abstractions ;
7+
8+ namespace ActiveMQ . Artemis . Client . IntegrationTests . TopologyManagement
9+ {
10+ public class AutoDeletingQueuesSpec : ActiveMQNetIntegrationSpec
11+ {
12+ public AutoDeletingQueuesSpec ( ITestOutputHelper output ) : base ( output )
13+ {
14+ }
15+
16+ [ Fact ]
17+ public async Task Should_auto_delete_queue_when_no_consumers_attached ( )
18+ {
19+ await using var connection = await CreateConnection ( ) ;
20+ await using var topologyManager = await connection . CreateTopologyManagerAsync ( ) ;
21+
22+ var address = Guid . NewGuid ( ) . ToString ( ) ;
23+ var queueName = Guid . NewGuid ( ) . ToString ( ) ;
24+ await topologyManager . CreateQueueAsync ( new QueueConfiguration
25+ {
26+ Name = queueName ,
27+ RoutingType = RoutingType . Multicast ,
28+ Address = address ,
29+ Durable = true ,
30+ AutoCreateAddress = true ,
31+ AutoDelete = true ,
32+ } , CancellationToken ) ;
33+
34+ await using var producer = await connection . CreateProducerAsync ( address ) ;
35+ var consumer = await connection . CreateConsumerAsync ( address , queueName ) ;
36+
37+ await producer . SendAsync ( new Message ( "foo" ) ) ;
38+ var msg = await consumer . ReceiveAsync ( ) ;
39+ await consumer . AcceptAsync ( msg ) ;
40+ await consumer . DisposeAsync ( ) ;
41+
42+ Assert . DoesNotContain ( queueName , await topologyManager . GetQueueNamesAsync ( ) ) ;
43+ }
44+
45+ [ Fact ]
46+ public async Task Should_delete_queue_after_specified_delay ( )
47+ {
48+ await using var connection = await CreateConnection ( ) ;
49+ await using var topologyManager = await connection . CreateTopologyManagerAsync ( ) ;
50+
51+ var address = Guid . NewGuid ( ) . ToString ( ) ;
52+ var queueName = Guid . NewGuid ( ) . ToString ( ) ;
53+ await topologyManager . CreateQueueAsync ( new QueueConfiguration
54+ {
55+ Name = queueName ,
56+ RoutingType = RoutingType . Multicast ,
57+ Address = address ,
58+ Durable = true ,
59+ AutoCreateAddress = true ,
60+ AutoDelete = true ,
61+ AutoDeleteDelay = TimeSpan . FromMilliseconds ( 500 )
62+ } , CancellationToken ) ;
63+
64+ await using var producer = await connection . CreateProducerAsync ( address ) ;
65+ var consumer = await connection . CreateConsumerAsync ( address , queueName ) ;
66+
67+ await producer . SendAsync ( new Message ( "foo" ) ) ;
68+ var msg = await consumer . ReceiveAsync ( ) ;
69+ await consumer . AcceptAsync ( msg ) ;
70+ await consumer . DisposeAsync ( ) ;
71+
72+ var queues = await Retry . RetryUntil (
73+ ( ) => topologyManager . GetQueueNamesAsync ( ) ,
74+ x => ! x . Contains ( queueName ) ,
75+ TimeSpan . FromMinutes ( 1 ) ) ;
76+
77+ Assert . DoesNotContain ( queueName , queues ) ;
78+ }
79+
80+ [ Fact ]
81+ public async Task Should_not_delete_queue_when_AutoDeleteMessageCount_not_reached ( )
82+ {
83+ await using var connection = await CreateConnection ( ) ;
84+ await using var topologyManager = await connection . CreateTopologyManagerAsync ( ) ;
85+
86+ var address = Guid . NewGuid ( ) . ToString ( ) ;
87+ var queueName = Guid . NewGuid ( ) . ToString ( ) ;
88+ await topologyManager . CreateQueueAsync ( new QueueConfiguration
89+ {
90+ Name = queueName ,
91+ RoutingType = RoutingType . Multicast ,
92+ Address = address ,
93+ Durable = true ,
94+ AutoCreateAddress = true ,
95+ AutoDelete = true ,
96+ AutoDeleteMessageCount = 1
97+ } , CancellationToken ) ;
98+
99+ await using var producer = await connection . CreateProducerAsync ( address ) ;
100+ var consumer = await connection . CreateConsumerAsync ( address , queueName ) ;
101+
102+ await producer . SendAsync ( new Message ( "foo" ) ) ;
103+ await producer . SendAsync ( new Message ( "foo1" ) ) ;
104+ await producer . SendAsync ( new Message ( "foo2" ) ) ;
105+
106+ var msg = await consumer . ReceiveAsync ( ) ;
107+ await consumer . AcceptAsync ( msg ) ;
108+ await consumer . DisposeAsync ( ) ;
109+
110+ Assert . Contains ( queueName , await topologyManager . GetQueueNamesAsync ( ) ) ;
111+
112+ consumer = await connection . CreateConsumerAsync ( address , queueName ) ;
113+ msg = await consumer . ReceiveAsync ( ) ;
114+ await consumer . AcceptAsync ( msg ) ;
115+ await consumer . DisposeAsync ( ) ;
116+
117+ // one unacknowledged message remained, thus broker should remove the queue
118+ Assert . DoesNotContain ( queueName , await topologyManager . GetQueueNamesAsync ( ) ) ;
119+ }
120+ }
121+ }
0 commit comments