|
| 1 | +using Xunit; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace LiteDB.Tests.Issues |
| 8 | +{ |
| 9 | + public class Issue2127_Tests |
| 10 | + { |
| 11 | + public class ReproTests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void InsertItemBackToBack_Test() |
| 15 | + { |
| 16 | + var databaseDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DatabaseLocation"); |
| 17 | + var databasePath = Path.Combine(databaseDirectory, "SampleDatabase.db"); |
| 18 | + var databaseLogPath = Path.Combine(databaseDirectory, "SampleDatabase-log.db"); |
| 19 | + |
| 20 | + // Repeat in a loop to hit that scenario where queue processing thread exists the loop but the task still remains incomplete. |
| 21 | + for (var i = 0; i < 100; i++) |
| 22 | + { |
| 23 | + if (Directory.Exists(databaseDirectory)) |
| 24 | + Directory.Delete(databaseDirectory, true); |
| 25 | + |
| 26 | + Directory.CreateDirectory(databaseDirectory); |
| 27 | + |
| 28 | + using var subject = new ExampleItemRepository(databasePath); |
| 29 | + |
| 30 | + var item1 = new ExampleItem |
| 31 | + { |
| 32 | + Id = Guid.NewGuid(), |
| 33 | + SomeProperty = Guid.NewGuid().ToString() |
| 34 | + }; |
| 35 | + var item2 = new ExampleItem |
| 36 | + { |
| 37 | + Id = Guid.NewGuid(), |
| 38 | + SomeProperty = Guid.NewGuid().ToString() |
| 39 | + }; |
| 40 | + |
| 41 | + subject.Insert(item1); |
| 42 | + subject.Insert(item2); |
| 43 | + |
| 44 | + // Allow items to be processed. |
| 45 | + Thread.Sleep(1000); |
| 46 | + |
| 47 | + var liteDbPath = databasePath; |
| 48 | + var liteDbLogPath = databaseLogPath; |
| 49 | + var copiedLiteDbPath = liteDbPath + ".copy"; |
| 50 | + var copiedLiteDbLogPath = liteDbLogPath + ".copy"; |
| 51 | + File.Copy(liteDbPath, copiedLiteDbPath); |
| 52 | + File.Copy(liteDbLogPath, copiedLiteDbLogPath); |
| 53 | + var liteDbContent = File.ReadAllText(copiedLiteDbPath, Encoding.UTF8); |
| 54 | + liteDbContent += File.ReadAllText(copiedLiteDbLogPath, Encoding.UTF8); |
| 55 | + |
| 56 | + Assert.True(liteDbContent.Contains(item1.SomeProperty, StringComparison.OrdinalIgnoreCase), $"Could not find item 1 property. {item1.SomeProperty}, Iteration: {i}"); |
| 57 | + Assert.True(liteDbContent.Contains(item2.SomeProperty, StringComparison.OrdinalIgnoreCase), $"Could not find item 2 property. {item2.SomeProperty}, Iteration: {i}"); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public class ExampleItem |
| 63 | + { |
| 64 | + [BsonId] |
| 65 | + public Guid Id { get; set; } |
| 66 | + |
| 67 | + public string SomeProperty { get; set; } |
| 68 | + } |
| 69 | + |
| 70 | + public sealed class ExampleItemRepository : IDisposable |
| 71 | + { |
| 72 | + public const string DatabaseFileName = "SampleDb"; |
| 73 | + |
| 74 | + private readonly LiteDatabase _liteDb; |
| 75 | + |
| 76 | + public ExampleItemRepository(string databasePath) |
| 77 | + { |
| 78 | + var connectionString = new ConnectionString |
| 79 | + { |
| 80 | + Filename = databasePath, |
| 81 | + Connection = ConnectionType.Direct |
| 82 | + }; |
| 83 | + |
| 84 | + _liteDb = new LiteDatabase(connectionString); |
| 85 | + } |
| 86 | + |
| 87 | + public void Insert(ExampleItem item) |
| 88 | + { |
| 89 | + var collection = _liteDb.GetCollection<ExampleItem>(); |
| 90 | + collection.Insert(item); |
| 91 | + } |
| 92 | + |
| 93 | + public void Dispose() |
| 94 | + { |
| 95 | + _liteDb?.Dispose(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments