|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Reactive.Linq; |
| 3 | +using System.Reflection.Metadata.Ecma335; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using Bogus; |
| 6 | +using DotJEM.AdvParsers; |
| 7 | +using DotJEM.Json.Index2.Documents.Fields; |
| 8 | +using DotJEM.Json.Index2.Management.Info; |
| 9 | +using DotJEM.Json.Index2.Management.Observables; |
| 10 | +using DotJEM.Json.Index2.Management.Snapshots; |
| 11 | +using DotJEM.Json.Index2.Management.Snapshots.Zip; |
| 12 | +using DotJEM.Json.Index2.Management.Source; |
| 13 | +using DotJEM.Json.Index2.Snapshots; |
| 14 | +using DotJEM.ObservableExtensions.InfoStreams; |
| 15 | +using DotJEM.Web.Scheduler; |
| 16 | +using Newtonsoft.Json.Linq; |
| 17 | +using NUnit.Framework; |
| 18 | + |
| 19 | +namespace DotJEM.Json.Index2.Management.Test; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +[TestFixture] |
| 24 | +public class JsonIndexManagerTest |
| 25 | +{ |
| 26 | + [Test, Explicit] |
| 27 | + public async Task IndexWriterShouldNotBeDisposed() |
| 28 | + { |
| 29 | + using TestDirectory dir = new(); |
| 30 | + |
| 31 | + IJsonIndex index = new JsonIndexBuilder("Test") |
| 32 | + .UsingSimpleFileStorage(dir.Info.CreateSubdirectory("index").FullName) |
| 33 | + .WithFieldResolver(new FieldResolver("id", "type")) |
| 34 | + .WithSnapshoting() |
| 35 | + .Build(); |
| 36 | + |
| 37 | + IJsonDocumentSource source = new DummyDocumentSource(); |
| 38 | + IWebTaskScheduler scheduler = new WebTaskScheduler(); |
| 39 | + ISnapshotStrategy strategy = new ZipSnapshotStrategy(dir.Info.CreateSubdirectory("snapshot").FullName); |
| 40 | + IJsonIndexSnapshotManager snapshots = new JsonIndexSnapshotManager(index, strategy, scheduler, "60h"); |
| 41 | + IJsonIndexManager manager = new JsonIndexManager(source, snapshots, index); |
| 42 | + |
| 43 | + InfoStreamExceptionEvent? disposedEvent = null; |
| 44 | + manager.InfoStream |
| 45 | + .OfType<InfoStreamExceptionEvent>() |
| 46 | + .Where(@event => @event.Exception is ObjectDisposedException) |
| 47 | + .Subscribe(@event => |
| 48 | + { |
| 49 | + Debug.WriteLine(@event.Exception); |
| 50 | + disposedEvent = @event; |
| 51 | + }); |
| 52 | + |
| 53 | + try |
| 54 | + { |
| 55 | + await manager.RunAsync(); |
| 56 | + Debug.WriteLine("TEST STARTED"); |
| 57 | + Stopwatch sw = Stopwatch.StartNew(); |
| 58 | + while (sw.Elapsed < 10.Minutes() && disposedEvent == null) |
| 59 | + { |
| 60 | + Task result = Random.Shared.Next(100) switch |
| 61 | + { |
| 62 | + (>= 0 and <= 50) => DoAfterDelay(manager.TakeSnapshotAsync, 1.Seconds()), |
| 63 | + (> 50 and <= 100) => DoAfterDelay(manager.ResetIndexAsync, 1.Seconds()), |
| 64 | + _ => Task.CompletedTask |
| 65 | + }; |
| 66 | + await result; |
| 67 | + } |
| 68 | + |
| 69 | + async Task DoAfterDelay(Func<Task> action, TimeSpan? delay = null) |
| 70 | + { |
| 71 | + await Task.Delay(delay ?? Random.Shared.Next(1,5).Seconds()); |
| 72 | + await action(); |
| 73 | + } |
| 74 | + await manager.StopAsync(); |
| 75 | + index.Close(); |
| 76 | + } |
| 77 | + catch (Exception e) |
| 78 | + { |
| 79 | + Console.WriteLine(e); |
| 80 | + await manager.StopAsync(); |
| 81 | + index.Close(); |
| 82 | + throw; |
| 83 | + } |
| 84 | + |
| 85 | + Assert.That(disposedEvent, Is.Null, () => disposedEvent?.Exception.ToString()); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +public class TestDirectory : IDisposable |
| 92 | +{ |
| 93 | + public DirectoryInfo Info { get; } |
| 94 | + |
| 95 | + public TestDirectory() |
| 96 | + { |
| 97 | + Info = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), $"TEST-{Guid.NewGuid():N}")); |
| 98 | + Debug.WriteLine("TEST DIR: " + Info.FullName); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + public void Dispose() |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + Info.Delete(true); |
| 107 | + } |
| 108 | + catch (Exception e) |
| 109 | + { |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + ~TestDirectory() |
| 114 | + { |
| 115 | + Dispose(); |
| 116 | + } |
| 117 | +} |
| 118 | +public class DummyDocumentSource : IJsonDocumentSource |
| 119 | +{ |
| 120 | + private readonly DocumentChangesStream observable = new(); |
| 121 | + private readonly InfoStream<DummyDocumentSource> infoStream = new(); |
| 122 | + |
| 123 | + public IObservable<IJsonDocumentSourceEvent> DocumentChanges => observable; |
| 124 | + public IObservableValue<bool> Initialized { get; } = new ObservableValue<bool>(); |
| 125 | + public IInfoStream InfoStream => infoStream; |
| 126 | + |
| 127 | + private Faker faker = new Faker("en"); |
| 128 | + private long gen; |
| 129 | + |
| 130 | + private Task? runningTask; |
| 131 | + private CancellationTokenSource cancellationTokenSource = new (); |
| 132 | + private readonly string area = "Test"; |
| 133 | + |
| 134 | + public async Task StartAsync() |
| 135 | + { |
| 136 | + infoStream.WriteJsonSourceEvent(JsonSourceEventType.Starting, area, $"Ingest starting for storageArea '{area}'."); |
| 137 | + runningTask = Task.Run(async () => |
| 138 | + { |
| 139 | + while (gen < 1_000_000 && !cancellationTokenSource.IsCancellationRequested) |
| 140 | + { |
| 141 | + if (!Initialized.Value) |
| 142 | + { |
| 143 | + infoStream.WriteJsonSourceEvent(JsonSourceEventType.Initializing, area, $"Initializing for storageArea '{area}'."); |
| 144 | + RunLoop(); |
| 145 | + Initialized.Value = true; |
| 146 | + infoStream.WriteJsonSourceEvent(JsonSourceEventType.Initialized, area, $"Initializing for storageArea '{area}'."); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + infoStream.WriteJsonSourceEvent(JsonSourceEventType.Updating, area, $"Checking updates for storageArea '{area}'."); |
| 151 | + RunLoop(); |
| 152 | + infoStream.WriteJsonSourceEvent(JsonSourceEventType.Updated, area, $"Done checking updates for storageArea '{area}'."); |
| 153 | + } |
| 154 | + observable.Publish(new JsonDocumentSourceDigestCompleted(area)); |
| 155 | + await Task.Delay(Random.Shared.Next(50, 250)); |
| 156 | + } |
| 157 | + }, cancellationTokenSource.Token); |
| 158 | + |
| 159 | + void RunLoop() |
| 160 | + { |
| 161 | + foreach (JObject json in Enumerable.Repeat(0, Random.Shared.Next(1, 100)) |
| 162 | + .Select(_ => Guid.NewGuid()) |
| 163 | + .Select(id => new |
| 164 | + { |
| 165 | + id, |
| 166 | + type = "Test", |
| 167 | + area, |
| 168 | + time = DateTime.Now, |
| 169 | + user = new |
| 170 | + { |
| 171 | + name = faker.Name.FirstName() |
| 172 | + } |
| 173 | + }) |
| 174 | + .Select(JObject.FromObject)) |
| 175 | + { |
| 176 | + observable.Publish(new JsonDocumentCreated(area, json, 10, new GenerationInfo(gen++, 1_000_000))); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public async Task StopAsync() |
| 182 | + { |
| 183 | + cancellationTokenSource.Cancel(); |
| 184 | + |
| 185 | + if(runningTask != null) |
| 186 | + await runningTask.ConfigureAwait(false); |
| 187 | + |
| 188 | + runningTask = null; |
| 189 | + cancellationTokenSource = new(); |
| 190 | + infoStream.WriteJsonSourceEvent(JsonSourceEventType.Stopped, area, $"Stopping for storageArea '{area}'."); |
| 191 | + } |
| 192 | + |
| 193 | + public void UpdateGeneration(string area, long generation) |
| 194 | + { |
| 195 | + gen = generation; |
| 196 | + } |
| 197 | + |
| 198 | + public async Task ResetAsync() |
| 199 | + { |
| 200 | + gen = 0; |
| 201 | + } |
| 202 | +} |
0 commit comments