|
| 1 | +namespace IntBasis.DocumentOriented.RavenDB.Tests; |
| 2 | + |
| 3 | +public class RavenDbDocumentChangesTest |
| 4 | +{ |
| 5 | + // Separate entity type for change tests so we don't pick up changes from other collections |
| 6 | + class TestBook : IDocumentEntity |
| 7 | + { |
| 8 | + public string? Id {get;set;} |
| 9 | + public string? Title { get; set; } |
| 10 | + public int PageCount { get; set; } |
| 11 | + } |
| 12 | + |
| 13 | + [Theory(DisplayName = "Subscribe nothing changed"), Integration] |
| 14 | + public async Task NoChanges(IDocumentChanges subject) |
| 15 | + { |
| 16 | + var invoked = false; |
| 17 | + var subscription = subject.Subscribe<TestBook>(() => |
| 18 | + { |
| 19 | + invoked = true; |
| 20 | + return Task.CompletedTask; |
| 21 | + }); |
| 22 | + await Task.Delay(100); |
| 23 | + subscription.Dispose(); |
| 24 | + invoked.Should().BeFalse(); |
| 25 | + } |
| 26 | + |
| 27 | + [Theory(DisplayName = "Subscribe 1 Change"), Integration] |
| 28 | + public async Task OneChange(IDocumentChanges subject, IDocumentStorage documentStorage) |
| 29 | + { |
| 30 | + var entity = new TestBook(); |
| 31 | + await documentStorage.Store(entity); |
| 32 | + var invoked = false; |
| 33 | + using var subscription = subject.Subscribe<TestBook>(() => |
| 34 | + { |
| 35 | + invoked = true; |
| 36 | + return Task.CompletedTask; |
| 37 | + }); |
| 38 | + |
| 39 | + entity.Title = "New title"; |
| 40 | + await documentStorage.Store(entity); |
| 41 | + |
| 42 | + await Task.Delay(100); |
| 43 | + invoked.Should().BeTrue(); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory(DisplayName = "Subscribe 5 Changes"), Integration] |
| 47 | + public async Task FiveChanges(IDocumentChanges subject, IDocumentStorage documentStorage) |
| 48 | + { |
| 49 | + var entity = new TestBook(); |
| 50 | + await documentStorage.Store(entity); |
| 51 | + var invoked = 0; |
| 52 | + using var subscription = subject.Subscribe<TestBook>(() => |
| 53 | + { |
| 54 | + invoked++; |
| 55 | + return Task.CompletedTask; |
| 56 | + }); |
| 57 | + |
| 58 | + for (int i = 0; i < 5; i++) |
| 59 | + { |
| 60 | + entity.PageCount = i; |
| 61 | + await documentStorage.Store(entity); |
| 62 | + } |
| 63 | + |
| 64 | + await Task.Delay(500); |
| 65 | + invoked.Should().BeInRange(5, 6); // TODO: Figure out why sometimes 6 |
| 66 | + } |
| 67 | + |
| 68 | + [Theory(DisplayName = "Disposal ends subscription"), Integration] |
| 69 | + public async Task Disposal(IDocumentChanges subject, IDocumentStorage documentStorage) |
| 70 | + { |
| 71 | + var invoked = false; |
| 72 | + var subscription = subject.Subscribe<TestBook>(() => |
| 73 | + { |
| 74 | + invoked = true; |
| 75 | + return Task.CompletedTask; |
| 76 | + }); |
| 77 | + subscription.Dispose(); |
| 78 | + |
| 79 | + await documentStorage.Store(new TestBook()); |
| 80 | + |
| 81 | + await Task.Delay(200); |
| 82 | + invoked.Should().BeFalse(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +//- [Change Streams] (https://mongodb.github.io/mongo-csharp-driver/2.9/reference/driver/change_streams/) |
0 commit comments