|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.EntityFrameworkCore.Storage.Internal; |
| 5 | +using Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider; |
| 6 | + |
| 7 | +// ReSharper disable MethodHasAsyncOverload |
| 8 | + |
| 9 | +namespace Microsoft.EntityFrameworkCore.Storage; |
| 10 | + |
| 11 | +public class RelationalDataReaderTest |
| 12 | +{ |
| 13 | + [ConditionalTheory] |
| 14 | + [MemberData(nameof(IsAsyncData))] |
| 15 | + public async Task Does_not_hold_reference_to_DbDataReader_after_dispose(bool async) |
| 16 | + { |
| 17 | + var fakeConnection = CreateConnection(); |
| 18 | + var relationalCommand = CreateRelationalCommand(commandText: "CommandText"); |
| 19 | + |
| 20 | + var reader = relationalCommand.ExecuteReader(new( |
| 21 | + fakeConnection, |
| 22 | + new Dictionary<string, object>(), |
| 23 | + readerColumns: null, |
| 24 | + context: null, |
| 25 | + logger: null)); |
| 26 | + |
| 27 | + Assert.NotNull(reader.DbDataReader); |
| 28 | + |
| 29 | + if (async) |
| 30 | + { |
| 31 | + await reader.DisposeAsync(); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + reader.Dispose(); |
| 36 | + } |
| 37 | + |
| 38 | + Assert.Null(reader.DbDataReader); |
| 39 | + } |
| 40 | + |
| 41 | + private const string ConnectionString = "Fake Connection String"; |
| 42 | + |
| 43 | + private static FakeRelationalConnection CreateConnection(IDbContextOptions options = null) |
| 44 | + => new(options ?? CreateOptions()); |
| 45 | + |
| 46 | + private static IDbContextOptions CreateOptions( |
| 47 | + RelationalOptionsExtension optionsExtension = null) |
| 48 | + { |
| 49 | + var optionsBuilder = new DbContextOptionsBuilder(); |
| 50 | + |
| 51 | + ((IDbContextOptionsBuilderInfrastructure)optionsBuilder) |
| 52 | + .AddOrUpdateExtension( |
| 53 | + optionsExtension |
| 54 | + ?? new FakeRelationalOptionsExtension().WithConnectionString(ConnectionString)); |
| 55 | + |
| 56 | + return optionsBuilder.Options; |
| 57 | + } |
| 58 | + |
| 59 | + private IRelationalCommand CreateRelationalCommand( |
| 60 | + string commandText = "Command Text", |
| 61 | + IReadOnlyList<IRelationalParameter> parameters = null) |
| 62 | + => new RelationalCommand( |
| 63 | + new RelationalCommandBuilderDependencies( |
| 64 | + new TestRelationalTypeMappingSource( |
| 65 | + TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(), |
| 66 | + TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>()), |
| 67 | + new ExceptionDetector()), |
| 68 | + commandText, |
| 69 | + parameters ?? Array.Empty<IRelationalParameter>()); |
| 70 | + |
| 71 | + public static IEnumerable<object[]> IsAsyncData = new[] { new object[] { false }, new object[] { true } }; |
| 72 | +} |
0 commit comments