-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from PHOENIXCONTACT/bugfix/db-controller-disp…
…osed-object-exception Fix `DisposedObjectException` in DatabaseController
- Loading branch information
Showing
12 changed files
with
208 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Moryx.TestTools.Test.Model/Setups/DisposedObjectSetup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Moryx.Model; | ||
using Moryx.Model.Attributes; | ||
using Moryx.Model.Repositories; | ||
|
||
namespace Moryx.TestTools.Test.Model | ||
{ | ||
[ModelSetup(typeof(TestModelContext))] | ||
public class DisposedObjectSetup : IModelSetup | ||
{ | ||
public int SortOrder => 1; | ||
|
||
public string Name => "Raw SQL Setup"; | ||
|
||
public string Description => ""; | ||
|
||
public string SupportedFileRegex => string.Empty; | ||
|
||
public async Task Execute(IUnitOfWork openContext, string setupData) | ||
{ | ||
// If the caller wouldn't await this method, | ||
// the DbContext might be disposed before it's | ||
// being used in here. | ||
// Adding a delay to ensure that the is actually | ||
// gone then. | ||
await Task.Delay(50); | ||
await openContext.DbContext.Database.ExecuteSqlRawAsync("SELECT * FROM \"Cars\""); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Tests/Moryx.Runtime.Endpoints.IntegrationTests/Databases/Controller/ModelSetupTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using Moryx.Model; | ||
using Moryx.TestTools.Test.Model; | ||
using NUnit.Framework; | ||
using System.Threading.Tasks; | ||
using Moryx.Runtime.Endpoints.Databases.Endpoint; | ||
using Moryx.AbstractionLayer.TestTools; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Moryx.Runtime.Endpoints.IntegrationTests.Databases.Controller | ||
{ | ||
internal class ModelSetupTests | ||
{ | ||
private IDbContextManager? _dbContextManager; | ||
private DatabaseController _databaseController; | ||
private readonly List<Exception> _exceptions = new List<Exception>(); | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_dbContextManager = InMemoryUnitOfWorkFactoryBuilder | ||
.SqliteDbContextManager(); | ||
|
||
_dbContextManager | ||
.Factory<TestModelContext>() | ||
.EnsureDbIsCreated(); | ||
|
||
_databaseController = new DatabaseController(_dbContextManager); | ||
|
||
_exceptions.Clear(); | ||
} | ||
|
||
[Test] | ||
public async Task ExecuteSetupDoesNotThrowDisposedObjectException() | ||
{ | ||
// Add unobserved task exceptions to a list, to be checked later. | ||
TaskScheduler.UnobservedTaskException += (sender, e) => | ||
{ | ||
_exceptions.Add(e.Exception); | ||
}; | ||
|
||
var result = await _databaseController!.ExecuteSetup("Moryx.TestTools.Test.Model.TestModelContext", new() | ||
{ | ||
Config = new() | ||
{ | ||
ConfiguratorTypename = "1", | ||
Entries = new() { { "ConnectionString", "DataSource=:memory:" } } | ||
}, | ||
Setup = new Endpoints.Databases.Endpoint.Models.SetupModel { Fullname = "Moryx.TestTools.Test.Model.DisposedObjectSetup" } | ||
}); | ||
|
||
// ExecuteSetup lead to unobserved task exceptions. We give them | ||
// some time to be thrown. | ||
Task.Delay(100).Wait(); | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
GC.Collect(); | ||
|
||
Assert.That(result?.Value?.Success, Is.True); | ||
Assert.That(_exceptions.Count, Is.EqualTo(0)); | ||
} | ||
} | ||
} |
Oops, something went wrong.