Skip to content

Commit

Permalink
Restore test behaviour, additional concurrency fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
napalu committed Jun 1, 2020
1 parent 7f3e238 commit 28be4ef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
26 changes: 13 additions & 13 deletions src/Mapster.Tests/WhenRegisteringAndMappingRace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Types_Map_Successfully_If_Mapping_Applied_First()
{
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;

var simplePoco = new WhenAddingCustomMappings.SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };
var simplePoco = new WhenAddingCustomMappings.SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

TypeAdapterConfig<WhenAddingCustomMappings.SimplePoco, WeirdPoco>.NewConfig()
.Map(dest => dest.IHaveADifferentId, src => src.Id)
Expand All @@ -32,12 +32,12 @@ public void Types_Map_Successfully_If_Mapping_Applied_First()
TypeAdapter.Adapt<WhenAddingCustomMappings.SimplePoco, WeirdPoco>(simplePoco);
}

[TestMethod, TestCategory("speed")]
[TestMethod, TestCategory("speed"), Ignore]
public void Race_Condition_Produces_Error()
{
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;

var simplePoco = new WhenAddingCustomMappings.SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };
var simplePoco = new WhenAddingCustomMappings.SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

var exception = Should.Throw<AggregateException>(() =>
{
Expand Down Expand Up @@ -72,16 +72,16 @@ public void Explicit_Mapping_Requirement_Throws_Before_Mapping_Attempted()
{
for (int i = 0; i < 100; i++)
{
Parallel.Invoke(
() =>
{
TypeAdapterConfig<WhenAddingCustomMappings.SimplePoco, WeirdPoco>.NewConfig()
.Map(dest => dest.IHaveADifferentId, src => src.Id)
.Map(dest => dest.MyNamePropertyIsDifferent, src => src.Name)
.Ignore(dest => dest.Children);
},
() => { TypeAdapter.Adapt<WeirdPoco>(simplePoco); }
);
Parallel.Invoke(
() =>
{
TypeAdapterConfig<WhenAddingCustomMappings.SimplePoco, WeirdPoco>.NewConfig()
.Map(dest => dest.IHaveADifferentId, src => src.Id)
.Map(dest => dest.MyNamePropertyIsDifferent, src => src.Name)
.Ignore(dest => dest.Children);
},
() => { TypeAdapter.Adapt<WeirdPoco>(simplePoco); }
);
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/Mapster/Settings/IgnoreDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ internal void Merge(string name, in IgnoreItem src)
var body = item.IsChildPath ? item.Condition.Body : item.Condition.Apply(param[0], param[1]);
var condition = Expression.Lambda(Expression.OrElse(src.Condition.Body, body), param);

this[name] = new IgnoreItem(condition, src.IsChildPath);
TryUpdate(name, new IgnoreItem(condition, src.IsChildPath), item);
}
else
this[name] = src;
TryAdd(name, src);

}

Expand Down
9 changes: 7 additions & 2 deletions src/Mapster/Settings/SettingStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SettingStore: IApplyable<SettingStore>
{
private readonly ConcurrentDictionary<string, object> _objectStore = new ConcurrentDictionary<string, object>();
private readonly ConcurrentDictionary<string, bool?> _booleanStore = new ConcurrentDictionary<string, bool?>();

public void Set(string key, bool? value)
{
if (value == null)
Expand Down Expand Up @@ -41,7 +41,7 @@ public T Get<T>(string key, Func<T> initializer)
var value = _objectStore.GetValueOrDefault(key);
if (value == null)
{
_objectStore.AddOrUpdate(key, value = initializer()!, (key, oldValue) => value);
_objectStore.AddOrUpdate(key, value = initializer()!, (updateKey, oldValue) => value);
}
return (T)value;
}
Expand Down Expand Up @@ -94,6 +94,11 @@ public void Apply(SettingStore other)
list.Add(item);
}
}
else
{
foreach (var item in side)
list.Add(item);
}
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Mapster.Models;
using System;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using Mapster.Adapters;
using Mapster.Models;
using Mapster.Utils;
using System.Collections.Concurrent;
using System.Collections;

namespace Mapster
{
Expand Down Expand Up @@ -588,14 +588,18 @@ private void Remove(TypeTuple key)
if (this.RuleMap.TryGetValue(key, out var rule))
{
this.RuleMap.TryRemove(key, out _);
var lockable = this.Rules as ICollection;
var lockable = Rules as ICollection;
if (!lockable.IsSynchronized)
{
lock (lockable.SyncRoot)
{
this.Rules.Remove(rule);
}
}
else
{
this.Rules.Remove(rule);
}
}
_mapDict.TryRemove(key, out _);
_mapToTargetDict.TryRemove(key, out _);
Expand Down

0 comments on commit 28be4ef

Please sign in to comment.