@@ -61,15 +61,39 @@ public string GenerateId()
61
61
62
62
public async Task InitializeAsync ( )
63
63
{
64
- if ( _cosmosTestContainer . State != TestcontainersStates . Running )
64
+ ushort publicPort = 0 ;
65
+
66
+ try
67
+ {
65
68
await _cosmosTestContainer . StartAsync ( ) ;
66
69
70
+ containerExsist = false ;
71
+ }
72
+ catch ( Exception ex ) //TODO catch name already using exception
73
+ {
74
+ containerExsist = true ;
75
+ }
76
+
77
+ if ( ! containerExsist )
78
+ {
79
+ publicPort = _cosmosTestContainer . GetMappedPublicPort ( privatePort ) ;
80
+ }
81
+ else
82
+ {
83
+ var listContainers = await _dockerClient . Containers . ListContainersAsync ( new ContainersListParameters ( ) ) ;
84
+
85
+ ContainerListResponse containerListResponse = listContainers . FirstOrDefault ( container => container . Names . Contains ( $ "/{ containerName } ") ) ;
86
+
87
+ if ( containerListResponse != null )
88
+ publicPort = containerListResponse . Ports . Single ( port => port . PrivatePort == privatePort ) . PublicPort ;
89
+ }
90
+
67
91
_database = new CosmosDatabase ( new CosmosOptions
68
92
{
69
93
ConnectionString =
70
- $ "AccountEndpoint=https://localhost:8081 ;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
94
+ $ "AccountEndpoint=https://localhost:{ publicPort } / ;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
71
95
DatabaseName = "database" ,
72
- CollectionName = $ "collection { Guid . NewGuid ( ) . ToString ( "N" ) . Take ( 8 ) } ",
96
+ CollectionName = $ "testContainers ",
73
97
AllowTableCreation = true ,
74
98
CosmosClientOptions = new CosmosClientOptions ( )
75
99
{
@@ -92,8 +116,8 @@ public async Task InitializeAsync()
92
116
93
117
public async Task DisposeAsync ( )
94
118
{
95
- /// await _database.DeleteAsync();
96
- // await _database.DisposeAsync();
119
+ await _database . DeleteAsync ( ) ;
120
+ await _database . DisposeAsync ( ) ;
97
121
/*
98
122
await _cosmosTestContainer.StopAsync();
99
123
await _cosmosTestContainer.CleanUpAsync();*/
0 commit comments