Skip to content

[Bug]: SourceCache ChangeSets are sent out of order when Item is updated right after it's added #991

@HyPhil

Description

@HyPhil

Describe the bug 🐞

When an Item in a SourceCache is edited and updated as a reaction to that item being added to the source cache, then OnItemAdded and OnItemUpdated will be called out of order in other subscriptions.

The order in which the streams are subscribed to might also play a role in this behaviour.

Step to reproduce

The following test fails because OnItemUpdated is called before OnItemAdded.

internal record Item(Guid Id, string Name);

internal sealed class Tests
{
    private readonly CompositeDisposable _disposable = new();
    private readonly SourceCache<Item, Guid> _sourceCache = new(item => item.Id);

    private Item _latest;
    
    [SetUp]
    public void Setup()
    {
        _sourceCache.Connect()
            .OnItemAdded(item => _sourceCache.AddOrUpdate(item with { Name = "Updated"}))
            .Subscribe()
            .DisposeWith(_disposable);
        
        _sourceCache
            .Connect()
            .OnItemAdded(item => _latest = item)
            .OnItemUpdated((item, _) => _latest = item)
            .Subscribe()
            .DisposeWith(_disposable);
        
        _sourceCache.AddOrUpdate(new Item(Guid.NewGuid(), "Added"));
    }
    
    [TearDown]
    public void TearDown() => _disposable.Dispose();

    [Test]
    public void Then_latest_should_be_updated()
    {
        _latest.Name.Should().Be("Updated");
    }
}

Reproduction repository

https://github.com/HyPhil/BugReport.DynamicData.001

Expected behavior

Test should pass

Screenshots 🖼️

No response

IDE

Rider Windows

Operating system

Windows

Version

10

Device

No response

DynamicData Version

9.2.1

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions