public void Can_filter_out_postponed_actions() { using (var database = CreateDocumentDatabase()) { var alert = GetSampleAlert(); database.NotificationCenter.Add(alert); var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); using (database.NotificationCenter.TrackActions(actions, writer)) { var postponeUntil = SystemTime.UtcNow.AddDays(1); database.NotificationCenter.Postpone(alert.Id, postponeUntil); IEnumerable <NotificationTableValue> alerts; using (database.NotificationCenter.GetStored(out alerts, postponed: false)) { var jsonAlerts = alerts.ToList(); Assert.Equal(0, jsonAlerts.Count); } } } }
public void Can_dismiss_persistent_action_and_get_notified_about_it() { using (var database = CreateDocumentDatabase()) { database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue; var alert = GetSampleAlert(); database.NotificationCenter.Add(alert); var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); using (database.NotificationCenter.TrackActions(actions, writer)) { database.NotificationCenter.Dismiss(alert.Id); IEnumerable <NotificationTableValue> alerts; using (database.NotificationCenter.GetStored(out alerts)) { var jsonAlerts = alerts.ToList(); Assert.Equal(0, jsonAlerts.Count); } } Assert.Equal(1, actions.Count); var notification = actions.DequeueAsync().Result; Assert.NotNull(notification); Assert.Equal(alert.Id, notification[nameof(NotificationUpdated.NotificationId)]); Assert.Equal(NotificationUpdateType.Dismissed, notification[nameof(NotificationUpdated.UpdateType)]); } }
public void Add_WhileNotificationsAreForDifferentEnvironments_ShouldRemindTwo() { using (var database = CreateDocumentDatabase()) { var environments = database.GetAllStoragesEnvironment().ToArray(); var firstEnvironment = environments[0].Environment; var secondEnvironment = environments[1].Environment; var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); var taskList = new List <Task>(); using (database.NotificationCenter.TrackActions(actions, writer)) { for (var i = 0; i < 3; i++) { taskList.Add(Task.Run(() => database.NotificationCenter.OutOfMemory.Add(firstEnvironment, new OutOfMemoryException()))); taskList.Add(Task.Run(() => database.NotificationCenter.OutOfMemory.Add(secondEnvironment, new OutOfMemoryException()))); } Task.WaitAll(taskList.ToArray()); } using (database.NotificationCenter.GetStored(out var notifications)) { Assert.Equal(2, notifications.Count()); var keys = notifications.Select(n => n.Json["Key"].ToString()).ToArray(); Assert.Contains($"{firstEnvironment}:{typeof(OutOfMemoryException)}", keys); Assert.Contains($"{secondEnvironment}:{typeof(OutOfMemoryException)}", keys); } } }
public void Add_WhileAllHaveTheSameKey_ShouldRemindOnlyOne() { using (var database = CreateDocumentDatabase()) { var environment = database.GetAllStoragesEnvironment().First().Environment; var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); var taskList = new List <Task>(); using (database.NotificationCenter.TrackActions(actions, writer)) { taskList.Add(Task.Run(() => database.NotificationCenter.OutOfMemory.Add(environment, new OutOfMemoryException()))); taskList.Add(Task.Run(() => database.NotificationCenter.OutOfMemory.Add(environment, new OutOfMemoryException()))); taskList.Add(Task.Run(() => database.NotificationCenter.OutOfMemory.Add(environment, new OutOfMemoryException()))); Task.WaitAll(taskList.ToArray()); } using (database.NotificationCenter.GetStored(out var notifications)) { Assert.Equal(1, notifications.Count()); Assert.Equal($"Out of memory occurred for '{environment}'", notifications.First().Json["Title"].ToString()); Assert.Equal($"{environment}:{typeof(OutOfMemoryException)}", notifications.First().Json["Key"].ToString()); } } }
public async Task Add_WhileAllHaveTheSameKeyAndOneOfterUpdateFrequencyTime_ShouldBeTheLater() { using (var database = CreateDocumentDatabase()) { var environment = database.GetAllStoragesEnvironment().First().Environment; var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); var taskList = new List <Task>(); using (database.NotificationCenter.TrackActions(actions, writer)) { taskList.Add(Task.Run(() => database.NotificationCenter.OutOfMemory.Add(environment, new Exception("First Message")))); await Task.Delay(16_000); taskList.Add(Task.Run(() => database.NotificationCenter.OutOfMemory.Add(environment, new Exception("Second Message")))); Task.WaitAll(taskList.ToArray()); } using (database.NotificationCenter.GetStored(out var notifications)) { var jsonAlerts = notifications.ToList(); Assert.Equal(1, notifications.Count()); Assert.Equal("Second Message", notifications.First().Json["Message"].ToString()); } } }
public async Task Should_be_notified_about_changed_database_stats() { using (var database = CreateDocumentDatabase()) { database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.FromMilliseconds(100); var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); using (database.NotificationCenter.TrackActions(actions, writer)) { var notification = await actions.TryDequeueAsync(TimeSpan.FromMilliseconds(5000)); Assert.True(notification.Item1); using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context)) using (var doc = context.ReadObject(new DynamicJsonValue { ["Foo"] = "Bar", [Constants.Documents.Metadata.Key] = new DynamicJsonValue { [Constants.Documents.Metadata.Collection] = "Foos" } }, "")) { using (var tx = context.OpenWriteTransaction()) { database.DocumentsStorage.Put(context, "foo/bar", null, doc); tx.Commit(); } } notification = await actions.TryDequeueAsync(TimeSpan.FromMilliseconds(500)); Assert.True(notification.Item1); var databaseStatsChanged = notification.Item2; Assert.NotNull(databaseStatsChanged); Assert.Equal(1L, databaseStatsChanged[nameof(DatabaseStatsChanged.CountOfDocuments)]); Assert.Equal(0L, databaseStatsChanged[nameof(DatabaseStatsChanged.CountOfIndexes)]); Assert.Equal(0L, databaseStatsChanged[nameof(DatabaseStatsChanged.CountOfStaleIndexes)]); var collections = (databaseStatsChanged[nameof(DatabaseStatsChanged.ModifiedCollections)] as DynamicJsonArray); Assert.Equal(1L, collections.Count); Assert.Equal("Foos", (collections.First() as DynamicJsonValue)["Name"]); } } }
public void Should_get_notification() { using (var database = CreateDocumentDatabase()) { var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); using (database.NotificationCenter.TrackActions(actions, writer)) { database.NotificationCenter.Add(GetSampleAlert()); } Assert.Equal(1, actions.Count); } }
public void Can_postpone_persistent_action_and_get_notified_about_it() { using (var database = CreateDocumentDatabase()) { database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue; var alert = GetSampleAlert(); database.NotificationCenter.Add(alert); var postponeUntil = SystemTime.UtcNow.AddDays(1); var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); using (database.NotificationCenter.TrackActions(actions, writer)) { database.NotificationCenter.Postpone(alert.Id, postponeUntil); } Assert.Equal(1, actions.Count); var notification = actions.DequeueAsync().Result; Assert.NotNull(notification); Assert.Equal(alert.Id, notification[nameof(NotificationUpdated.NotificationId)]); Assert.Equal(NotificationUpdateType.Postponed, notification[nameof(NotificationUpdated.UpdateType)]); IEnumerable <NotificationTableValue> alerts; using (database.NotificationCenter.GetStored(out alerts)) { var jsonAlerts = alerts.ToList(); Assert.Equal(1, jsonAlerts.Count); var readAlert = jsonAlerts[0]; Assert.Equal(alert.CreatedAt, jsonAlerts[0].CreatedAt); Assert.Equal(alert.CreatedAt.GetDefaultRavenFormat(alert.CreatedAt.Kind == DateTimeKind.Utc), readAlert.Json[nameof(AlertRaised.CreatedAt)].ToString()); Assert.Equal(postponeUntil, jsonAlerts[0].PostponedUntil); } } }
public void Should_send_postponed_notification_when_postpone_date_reached() { using (var database = CreateDocumentDatabase()) { database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue; var alert1 = GetSampleAlert(customKey: "alert-1"); database.NotificationCenter.Add(alert1); var alert2 = GetSampleAlert(customKey: "alert-2"); database.NotificationCenter.Add(alert2); var alert3 = GetSampleAlert(customKey: "alert-3"); database.NotificationCenter.Add(alert3); var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); using (database.NotificationCenter.TrackActions(actions, writer)) { database.NotificationCenter.Postpone(alert1.Id, SystemTime.UtcNow.AddDays(1)); database.NotificationCenter.Postpone(alert2.Id, SystemTime.UtcNow.AddMilliseconds(100)); database.NotificationCenter.Postpone(alert3.Id, SystemTime.UtcNow.AddDays(1)); for (int i = 0; i < 2; i++) { var posponed = actions.DequeueAsync().Result; Assert.NotNull(posponed); Assert.Equal(NotificationUpdateType.Postponed, posponed[(nameof(NotificationUpdated.UpdateType))]); } Assert.True(SpinWait.SpinUntil(() => writer.SentNotifications.Count == 1, TimeSpan.FromSeconds(30)), $"Got: {writer.SentNotifications.Count}"); Assert.Equal(alert2.Id, writer.SentNotifications[0]); } } }
public void Can_postpone_notification_forever_then_next_notifictions_wont_be_sent() { using (var database = CreateDocumentDatabase()) { database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue; var alert = GetSampleAlert(); database.NotificationCenter.Add(alert); var notifications = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); database.NotificationCenter.Postpone(alert.Id, DateTime.MaxValue); using (database.NotificationCenter.TrackActions(notifications, writer)) { database.NotificationCenter.Add(alert); Assert.Equal(0, notifications.Count); } } }
public async Task Duplicated_notification_should_not_arrive_before_postponed_until_date() { using (var database = CreateDocumentDatabase()) { database.NotificationCenter.Options.DatabaseStatsThrottle = TimeSpan.MaxValue; var alert = GetSampleAlert(); database.NotificationCenter.Add(alert); database.NotificationCenter.Postpone(alert.Id, SystemTime.UtcNow.AddDays(1)); var actions = new AsyncQueue <DynamicJsonValue>(); var writer = new TestWebSocketWriter(); using (database.NotificationCenter.TrackActions(actions, writer)) { database.NotificationCenter.Add(alert); Assert.False((await actions.TryDequeueAsync(TimeSpan.FromMilliseconds(10))).Item1); database.NotificationCenter.Add(alert); Assert.False((await actions.TryDequeueAsync(TimeSpan.FromMilliseconds(10))).Item1); } } }