litedb-org / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.64k stars 1.25k forks source link

[BUG] Regression 5.0.17 -> 5.0.18, Database.Rebuild() not disposing correctly #2549

Open Blue101black opened 1 month ago

Blue101black commented 1 month ago

Version 5.0.18

Describe the bug The below unit test works in 5.0.17, but fails in 5.0.18.

I probably shouldn't be relying on Rebuild to dispose LiteDb and flush all pending writes to the stream, but our unit test does it this way and it breaks in 5.0.18 so I thought I would share.

Code to Reproduce

public class ReOpen_Tests
{
    [Fact]
    public void Should_Open()
    {
        byte[] databaseBytes;

        using (var stream = new MemoryStream())
        {
            var database = new LiteDatabase(stream);
            var collection = database.GetCollection<Item>();
            collection.Upsert(new Item(Guid.NewGuid()));
            var items = collection.Query().ToList();
            items.Should().HaveCount(1);

            database.Rebuild();
            databaseBytes = stream.ToArray();
        }

                // Opening the database with the existing stream should have the items still.
        using (var stream = new MemoryStream(databaseBytes))
        {
            var database = new LiteDatabase(stream);
            var items = database.GetCollection<Item>().Query().ToList();
            items.Should().HaveCount(1);
        }
    }

    private class Item
    {
        public Item(Guid id)
        {
            Id = id;
        }

        public Guid Id { get; set; }
    }
}

Expected behavior The test above should pass.