2121using System . Net . WebSockets ;
2222using System . Text ;
2323using System . Threading ;
24- using DotNet . Testcontainers ;
25- using DotNet . Testcontainers . Builders ;
26- using DotNet . Testcontainers . Containers ;
2724using Microcks . Testcontainers . Model ;
2825using Microsoft . Extensions . Logging ;
2926
3027namespace Microcks . Testcontainers . Tests . Async ;
3128
32- [ Collection ( nameof ( WsCollection ) ) ]
33- public sealed class MicrocksAsyncFeatureTest : IAsyncLifetime
29+ [ Collection ( nameof ( MicrocksAsyncFeatureCollection ) ) ]
30+ public sealed class MicrocksAsyncFeatureTest
3431{
35- /// <summary>
36- /// Image name for the Microcks container.
37- /// </summary>
38- private const string MicrocksImage = "quay.io/microcks/microcks-uber:1.10.1-native" ;
39-
40- private const string BadPastryAsyncImage = "quay.io/microcks/contract-testing-demo-async:01" ;
41- private const string GoodPastryAsyncImage = "quay.io/microcks/contract-testing-demo-async:02" ;
42-
43- private MicrocksContainerEnsemble _microcksContainerEnsemble ;
44- private IContainer _wsGoodImplContainer ;
45- private IContainer _wsBadImplContainer ;
32+ private readonly MicrocksAsyncFeatureFixture fixture ;
4633
47- public async Task DisposeAsync ( )
34+ public MicrocksAsyncFeatureTest ( MicrocksAsyncFeatureFixture fixture )
4835 {
49- await this . _microcksContainerEnsemble . DisposeAsync ( ) ;
50- await this . _wsBadImplContainer . DisposeAsync ( ) ;
51- await this . _wsGoodImplContainer . DisposeAsync ( ) ;
52- }
53-
54- public async Task InitializeAsync ( )
55- {
56- ConsoleLogger . Instance . DebugLogLevelEnabled = true ;
57-
58- this . _microcksContainerEnsemble = new MicrocksContainerEnsemble ( MicrocksImage )
59- . WithMainArtifacts ( "pastry-orders-asyncapi.yml" )
60- . WithAsyncFeature ( ) ;
61-
62- this . _wsBadImplContainer = new ContainerBuilder ( )
63- . WithImage ( BadPastryAsyncImage )
64- . WithNetwork ( this . _microcksContainerEnsemble . Network )
65- . WithNetworkAliases ( "bad-impl" )
66- . WithExposedPort ( 4001 )
67- . WithWaitStrategy (
68- Wait . ForUnixContainer ( )
69- . UntilMessageIsLogged ( ".*Starting WebSocket server on ws://localhost:4001/websocket.*" )
70- )
71- . Build ( ) ;
72-
73- this . _wsGoodImplContainer = new ContainerBuilder ( )
74- . WithImage ( GoodPastryAsyncImage )
75- . WithNetwork ( this . _microcksContainerEnsemble . Network )
76- . WithNetworkAliases ( "good-impl" )
77- . WithExposedPort ( 4002 )
78- . WithWaitStrategy (
79- Wait . ForUnixContainer ( )
80- . UntilMessageIsLogged ( ".*Starting WebSocket server on ws://localhost:4002/websocket.*" )
81- )
82- . Build ( ) ;
83-
84- await this . _microcksContainerEnsemble . StartAsync ( ) ;
85- await this . _wsBadImplContainer . StartAsync ( ) ;
86- await this . _wsGoodImplContainer . StartAsync ( ) ;
36+ this . fixture = fixture ;
8737 }
8838
8939 [ Fact ]
9040 public void ShouldDetermineCorrectImageMessage ( )
9141 {
9242 Assert . Equal ( "quay.io/microcks/microcks-uber-async-minion:1.10.1" ,
93- this . _microcksContainerEnsemble . AsyncMinionContainer . Image . FullName ) ;
43+ this . fixture . MicrocksContainerEnsemble . AsyncMinionContainer . Image . FullName ) ;
9444 }
9545
9646 /// <summary>
@@ -101,7 +51,8 @@ public void ShouldDetermineCorrectImageMessage()
10151 public async Task ShouldReceivedWebSocketMessageWhenMessageIsEmitted ( )
10252 {
10353 // Get the WebSocket endpoint for the "Pastry orders API" with version "0.1.0" and subscription "SUBSCRIBE pastry/orders".
104- var webSocketEndpoint = _microcksContainerEnsemble
54+ var webSocketEndpoint = fixture
55+ . MicrocksContainerEnsemble
10556 . AsyncMinionContainer
10657 . GetWebSocketMockEndpoint ( "Pastry orders API" , "0.1.0" , "SUBSCRIBE pastry/orders" ) ;
10758 const string expectedMessage = "{\" id\" :\" 4dab240d-7847-4e25-8ef3-1530687650c8\" ,\" customerId\" :\" fe1088b3-9f30-4dc1-a93d-7b74f0a072b9\" ,\" status\" :\" VALIDATED\" ,\" productQuantities\" :[{\" quantity\" :2,\" pastryName\" :\" Croissant\" },{\" quantity\" :1,\" pastryName\" :\" Millefeuille\" }]}" ;
@@ -139,15 +90,15 @@ public async Task ShouldReturnsCorrectStatusContractWhenBadMessageIsEmitted()
13990 TestEndpoint = "ws://bad-impl:4001/websocket" ,
14091 } ;
14192
142- var taskTestResult = _microcksContainerEnsemble . MicrocksContainer
93+ var taskTestResult = fixture . MicrocksContainerEnsemble . MicrocksContainer
14394 . TestEndpointAsync ( testRequest ) ;
14495
14596 stopwatch . Start ( ) ;
14697 var testResult = await taskTestResult ;
14798 stopwatch . Stop ( ) ;
14899
149100 // Add logging to trace the test result
150- var logger = _microcksContainerEnsemble
101+ var logger = fixture . MicrocksContainerEnsemble
151102 . MicrocksContainer
152103 . Logger ;
153104
@@ -184,7 +135,7 @@ public async Task ShouldReturnsCorrectStatusContractWhenGoodMessageIsEmitted()
184135 TestEndpoint = "ws://good-impl:4002/websocket" ,
185136 } ;
186137
187- var taskTestResult = _microcksContainerEnsemble . MicrocksContainer
138+ var taskTestResult = fixture . MicrocksContainerEnsemble . MicrocksContainer
188139 . TestEndpointAsync ( testRequest ) ;
189140
190141 var testResult = await taskTestResult ;
@@ -197,4 +148,35 @@ public async Task ShouldReturnsCorrectStatusContractWhenGoodMessageIsEmitted()
197148 Assert . NotEmpty ( testResult . TestCaseResults . First ( ) . TestStepResults ) ;
198149 Assert . True ( string . IsNullOrEmpty ( testResult . TestCaseResults . First ( ) . TestStepResults . First ( ) . Message ) ) ;
199150 }
151+
152+ /// <summary>
153+ /// Test that verifies that the WaitForConditionAsync method throws a TaskCanceledException
154+ /// when the specified timeout is reached.
155+ /// </summary>
156+ [ Fact ]
157+ public async Task WaitForConditionAsyncShouldThrowTaskCanceledExceptionWhenTimeoutIsReached ( )
158+ {
159+ // New Test request
160+ var testRequest = new TestRequest
161+ {
162+ ServiceId = "Pastry orders API:0.1.0" ,
163+ RunnerType = TestRunnerType . ASYNC_API_SCHEMA ,
164+ Timeout = TimeSpan . FromMilliseconds ( 200 ) ,
165+ TestEndpoint = "ws://good-impl:4002/websocket" ,
166+ } ;
167+
168+ var taskTestResult = fixture . MicrocksContainerEnsemble . MicrocksContainer
169+ . TestEndpointAsync ( testRequest ) ;
170+
171+ var stopwatch = new Stopwatch ( ) ;
172+ stopwatch . Start ( ) ;
173+ var testResult = await taskTestResult ;
174+ stopwatch . Stop ( ) ;
175+
176+ // Assert
177+ Assert . True ( stopwatch . ElapsedMilliseconds > 200 ) ;
178+ Assert . True ( testResult . InProgress ) ;
179+ Assert . False ( testResult . Success ) ;
180+ Assert . Equal ( testRequest . TestEndpoint , testResult . TestedEndpoint ) ;
181+ }
200182}
0 commit comments