@@ -6,7 +6,7 @@ namespace ServiceControl.Transport.Tests;
66using System . Linq ;
77using System . Threading ;
88using System . Threading . Tasks ;
9- using Microsoft . Extensions . Logging . Abstractions ;
9+ using Microsoft . Extensions . Logging ;
1010using Microsoft . Extensions . Time . Testing ;
1111using NUnit . Framework ;
1212using Particular . Approvals ;
@@ -32,7 +32,9 @@ public void Initialise()
3232 MaxConcurrency = 1 ,
3333 EndpointName = Guid . NewGuid ( ) . ToString ( "N" )
3434 } ;
35- query = new AmazonSQSQuery ( NullLogger < AmazonSQSQuery > . Instance , provider , transportSettings ) ;
35+ var loggerFactory = LoggerFactory . Create ( builder => builder . AddSimpleConsole ( ) . SetMinimumLevel ( LogLevel . Trace ) ) ;
36+ var logger = loggerFactory . CreateLogger < AmazonSQSQuery > ( ) ;
37+ query = new AmazonSQSQuery ( logger , provider , transportSettings ) ;
3638 }
3739
3840 [ Test ]
@@ -94,11 +96,9 @@ public async Task TestConnectionWithValidSettings()
9496 }
9597
9698 [ Test ]
99+ [ CancelAfter ( 2 * 60 * 1000 ) ]
97100 public async Task RunScenario ( )
98101 {
99- // We need to wait a bit of time, to ensure AWS metrics are retrievable
100- using var cancellationTokenSource = new CancellationTokenSource ( TimeSpan . FromMinutes ( 6 ) ) ;
101- CancellationToken token = cancellationTokenSource . Token ;
102102 const int numMessagesToIngest = 15 ;
103103
104104 await CreateTestQueue ( transportSettings . EndpointName ) ;
@@ -111,37 +111,52 @@ public async Task RunScenario()
111111 {
112112 dictionary . Add ( AmazonSQSQuery . AmazonSQSSettings . AccessKey , connectionString . AccessKey ) ;
113113 }
114+
114115 if ( ! string . IsNullOrEmpty ( connectionString . SecretKey ) )
115116 {
116117 dictionary . Add ( AmazonSQSQuery . AmazonSQSSettings . SecretKey , connectionString . SecretKey ) ;
117118 }
119+
118120 if ( ! string . IsNullOrEmpty ( connectionString . Region ) )
119121 {
120122 dictionary . Add ( AmazonSQSQuery . AmazonSQSSettings . Region , connectionString . Region ) ;
121123 }
122124
123- query . Initialize ( new ReadOnlyDictionary < string , string > ( dictionary ) ) ;
125+ query . Initialize ( dictionary . AsReadOnly ( ) ) ;
124126
125- await Task . Delay ( TimeSpan . FromMinutes ( 2 ) , token ) ;
127+ var startDate = DateOnly . FromDateTime ( provider . GetUtcNow ( ) . DateTime ) ;
128+ provider . Advance ( TimeSpan . FromDays ( 1 ) ) ;
126129
127- var queueNames = new List < IBrokerQueue > ( ) ;
128- await foreach ( IBrokerQueue queueName in query . GetQueueNames ( token ) )
130+ while ( ! TestContext . CurrentContext . CancellationToken . IsCancellationRequested )
129131 {
130- queueNames . Add ( queueName ) ;
131- }
132+ await Task . Delay ( TimeSpan . FromSeconds ( 5 ) , TestContext . CurrentContext . CancellationToken ) ;
132133
133- IBrokerQueue queue = queueNames . Find ( name => name . QueueName == $ "{ connectionString . QueueNamePrefix } { transportSettings . EndpointName } ") ;
134- Assert . That ( queue , Is . Not . Null ) ;
134+ var queueNames = new List < IBrokerQueue > ( ) ;
135+ await foreach ( IBrokerQueue queueName in query . GetQueueNames ( TestContext . CurrentContext . CancellationToken ) )
136+ {
137+ queueNames . Add ( queueName ) ;
138+ }
135139
136- long total = 0L ;
140+ IBrokerQueue queue = queueNames . Find ( name => name . QueueName == $ " { connectionString . QueueNamePrefix } { transportSettings . EndpointName } " ) ;
137141
138- DateTime startDate = provider . GetUtcNow ( ) . DateTime ;
139- provider . Advance ( TimeSpan . FromDays ( 1 ) ) ;
140- await foreach ( QueueThroughput queueThroughput in query . GetThroughputPerDay ( queue , DateOnly . FromDateTime ( startDate ) , token ) )
141- {
142- total += queueThroughput . TotalThroughput ;
142+ if ( queue == null )
143+ {
144+ continue ;
145+ }
146+
147+ long total = 0L ;
148+
149+ await foreach ( QueueThroughput queueThroughput in query . GetThroughputPerDay ( queue , startDate , TestContext . CurrentContext . CancellationToken ) )
150+ {
151+ total += queueThroughput . TotalThroughput ;
152+ }
153+
154+ if ( total == numMessagesToIngest )
155+ {
156+ return ;
157+ }
143158 }
144159
145- Assert . That ( total , Is . EqualTo ( numMessagesToIngest ) ) ;
160+ Assert . Fail ( "Timeout waiting for expected throughput to be report" ) ;
146161 }
147162}
0 commit comments