Skip to content

Commit

Permalink
[2.1.3] Add quirks for in-memory concurrency patch fix
Browse files Browse the repository at this point in the history
Issue #12214
  • Loading branch information
ajcvickers committed Jun 28, 2018
1 parent 23e9fc8 commit c5c9b0e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/EFCore.InMemory/Storage/Internal/InMemoryTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,22 @@ private static bool IsConcurrencyConflict(
object rowValue,
Dictionary<IProperty, object> concurrencyConflicts)
{
if (property.IsConcurrencyToken
&& !StructuralComparisons.StructuralEqualityComparer.Equals(
rowValue,
entry.GetOriginalValue(property)))
if (property.IsConcurrencyToken)
{
concurrencyConflicts.Add(property, rowValue);
var originalValue = entry.GetOriginalValue(property);

return true;
var revertPatchBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue12214", out var isEnabled)
&& isEnabled;

if ((revertPatchBehavior
&& !Equals(rowValue, originalValue))
|| (!revertPatchBehavior
&& !StructuralComparisons.StructuralEqualityComparer.Equals(rowValue, originalValue)))
{
concurrencyConflicts.Add(property, rowValue);

return true;
}
}

return false;
Expand Down
77 changes: 77 additions & 0 deletions test/EFCore.InMemory.FunctionalTests/UpdatesInMemoryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.EntityFrameworkCore.InMemory.Internal;
#endif
using Microsoft.EntityFrameworkCore.TestModels.UpdatesModel;
using Xunit;

namespace Microsoft.EntityFrameworkCore
{
Expand All @@ -20,6 +21,82 @@ protected UpdatesInMemoryTestBase(TFixture fixture)
{
}

#if !Test20
[Fact]
public virtual void Update_on_bytes_concurrency_token_original_value_matches_throws_with_quirk()
{
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");

try
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", true);

ExecuteWithStrategyInTransaction(
context =>
{
var entry = context.ProductWithBytes.Attach(
new ProductWithBytes
{
Id = productId,
Name = "MegaChips",
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
});
entry.Entity.Name = "GigaChips";
Assert.Throws<DbUpdateConcurrencyException>(
() => context.SaveChanges());
},
context =>
{
Assert.Equal("MegaChips", context.ProductWithBytes.Find(productId).Name);
});

}
finally
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", false);
}
}

[Fact]
public virtual void Remove_on_bytes_concurrency_token_original_value_matches_throws_with_quirk()
{
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");

try
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", true);

ExecuteWithStrategyInTransaction(
context =>
{
var entry = context.ProductWithBytes.Attach(
new ProductWithBytes
{
Id = productId,
Name = "MegaChips",
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
});
entry.State = EntityState.Deleted;
Assert.Throws<DbUpdateConcurrencyException>(
() => context.SaveChanges());
},
context =>
{
Assert.Equal("MegaChips", context.ProductWithBytes.Find(productId).Name);
});

}
finally
{
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", false);
}
}
#endif

protected override string UpdateConcurrencyMessage
=> InMemoryStrings.UpdateConcurrencyException;

Expand Down

0 comments on commit c5c9b0e

Please sign in to comment.