mbdavid / LiteDB

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

[BUG] Records not kept in sync when using memory stream #1471

Open pwasiewicz opened 4 years ago

pwasiewicz commented 4 years ago

Version 5.0,1

Describe the bug I have bunch of unit tests (1000+). When I write unit test that uses memory stream ( (simplified to show core of code)):

var repository = new LiteRepository(new MemoryStream());
var id = Guid.Parse("{855E95DB-A0D1-425A-986E-6E49BEABA6E4}");
 repository .Insert(new Retry
{
     Id = id
});
var record = repository.Query<Retry>().Where(retry => retry.Id == request.Id).FirstOrDefault(); 
//tried record = repository.Database.GetCollection<Retry>().FindById(request.Id);

try{
  Assert.NotNull(record);
} finally {
 repository.Dispose();
}

The problem is that sometimes this code fails (tests are running in a parallel). It seems that Query doesn't see inserted record before. (record is null). I can say it is about 50% of cases.

Maybe I am missing some Flush after insert?

Expected behavior record should always be not null

Additional context Rememeber to run bunch of tests in a parallel.

mbdavid commented 4 years ago

Hi @pwasiewicz, there is no need to Flush in v5. This automatic execute after insert queue. As your example, you are not sharing MemoryStream across multiples tests (parallel here are using in isolated LiteDatabases and MemoryStream). I had almost half of my unit tests in LiteDB using MemoryStream too. I will try to reproduce your same example.

pwasiewicz commented 4 years ago

@mbdavid I've tried to replace MemoryStream with :memory: connection string (just like in your tests here in repo) and problem is gone. Anyway, when switched back to the MemoryStream - test fails again.