Skip to content

Commit

Permalink
Fix a race condtion in inner join. Fixes #364 (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandPheasant authored Jun 4, 2020
1 parent 4492375 commit ce4e3a7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/DynamicData.Tests/Cache/InnerJoinFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Reactive;
using DynamicData.Kernel;
using Xunit;
using FluentAssertions;
Expand Down
44 changes: 44 additions & 0 deletions src/DynamicData.Tests/Cache/InnerJoinFixtureRaceCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using Xunit;

namespace DynamicData.Tests.Cache
{
public class InnerJoinFixtureRaceCondition
{
public class Thing
{
public long Id { get; set; }
public string Name { get; set; }
}

/// <summary>
/// Tests to see whether we have fixed a race condition. See https://github.com/reactiveui/DynamicData/issues/364
///
/// RP: 04-June-2020 Before the fix, this code occasionally caused a threading issue and the fix seems to have worked.
/// I am leaving it here for a short period of time to see whether it produces traffic light test results.
/// </summary>
[Fact]
public void LetsSeeWhetherWeCanRandomlyHitARaceCondition()
{
var ids = ObservableChangeSet.Create<long, long>(sourceCache =>
{
return Observable.Range(1, 1000000, Scheduler.Default)
.Subscribe(x => sourceCache.AddOrUpdate(x));
}, x => x);

var itemsCache = new SourceCache<Thing, long>(x => x.Id);
itemsCache.AddOrUpdate(new[]
{
new Thing {Id = 300, Name = "Quick"},
new Thing {Id = 600, Name = "Brown"},
new Thing {Id = 900, Name = "Fox"},
new Thing {Id = 1200, Name = "Hello"},
});

ids.InnerJoin(itemsCache.Connect(), x => x.Id, (_, thing) => thing)
.Subscribe((z)=>{},ex=>{},()=>{});
}
}
}
4 changes: 2 additions & 2 deletions src/DynamicData/Cache/Internal/InnerJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public IObservable<IChangeSet<TDestination, TLeftKey>> Run()
var locker = new object();

//create local backing stores
var leftCache = _left.Synchronize(locker).AsObservableCache(false);
var rightCache = _right.Synchronize(locker).ChangeKey(_rightKeySelector).AsObservableCache(false);
var leftCache = _left.Synchronize(locker).AsObservableCache();
var rightCache = _right.Synchronize(locker).ChangeKey(_rightKeySelector).AsObservableCache();

//joined is the final cache
var joinedCache = new LockFreeObservableCache<TDestination, TLeftKey>();
Expand Down

0 comments on commit ce4e3a7

Please sign in to comment.