Skip to content

Commit 9ba397f

Browse files
authored
.Net: Further improvement to MongoDB reliability. (#11857)
### Motivation and Context When the mongodb container initialization fails, the disposal also failed, so fixing this issue. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent 89ef228 commit 9ba397f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

dotnet/src/IntegrationTests/Connectors/Memory/MongoDB/MongoDBVectorStoreFixture.cs

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using DotNet.Testcontainers.Containers;
78
using Microsoft.Extensions.VectorData;
89
using MongoDB.Driver;
910
using Testcontainers.MongoDb;
@@ -68,16 +69,22 @@ public async Task InitializeAsync()
6869

6970
public async Task DisposeAsync()
7071
{
71-
var cursor = await this.MongoDatabase.ListCollectionNamesAsync();
72-
73-
while (await cursor.MoveNextAsync().ConfigureAwait(false))
72+
if (this.MongoDatabase is not null)
7473
{
75-
foreach (var collection in cursor.Current)
74+
var cursor = await this.MongoDatabase.ListCollectionNamesAsync();
75+
76+
while (await cursor.MoveNextAsync().ConfigureAwait(false))
7677
{
77-
await this.MongoDatabase.DropCollectionAsync(collection);
78+
foreach (var collection in cursor.Current)
79+
{
80+
await this.MongoDatabase.DropCollectionAsync(collection);
81+
}
7882
}
7983
}
8084

81-
await this._container.StopAsync();
85+
if (this._container is not null && this._container.State == TestcontainersStates.Running)
86+
{
87+
await this._container.StopAsync();
88+
}
8289
}
8390
}

0 commit comments

Comments
 (0)