public async Task TestMarkingAsComplete(ConventionType seedType) { using (var db = new ReusableDB()) { List <TestSagaData> sagas; using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); sagas = await Prefill(store, seedType); } using (var store = db.NewStore()) { Console.WriteLine($"Testing saga lookups with DocumentStore initially configured for {seedType} conventions."); ApplyTestConventions(store, seedType); store.Initialize(); // Update each saga, once by id and once by correlation property foreach (var saga in sagas) { var persister = new SagaPersister(); using (var session = store.OpenAsyncSession()) { Console.WriteLine($"Retrieving SagaId {saga.Id} by Correlation Property OrderId={saga.OrderId} and completing."); var ravenDBSynchronizedStorageSession = new RavenDBSynchronizedStorageSession(session, false); var contextBag = new ContextBag(); var byOrderId = await persister.Get <TestSagaData>("OrderId", saga.OrderId, ravenDBSynchronizedStorageSession, contextBag); Assert.IsNotNull(byOrderId); await persister.Complete(byOrderId, ravenDBSynchronizedStorageSession, contextBag); await session.SaveChangesAsync(); } } db.WaitForIndexing(store); // Ensure terms are still the saga type and unique identity type var terms = store.DatabaseCommands.GetTerms("Raven/DocumentsByEntityName", "Tag", null, 1024).ToList(); Assert.AreEqual(2, terms.Count); foreach (var term in terms) { var query = new IndexQuery { Query = "Tag:" + term, PageSize = 0 }; // Ensure there are none left var queryResult = store.DatabaseCommands.Query("Raven/DocumentsByEntityName", query); Assert.AreEqual(0, queryResult.TotalResults); } } } }
public async Task EnsureMultipleExistingStrategiesWillThrow() { using (var db = new ReusableDB()) { using (var store = db.NewStore()) { ApplyPrefillConventions(store, ConventionType.RavenDefault); store.Initialize(); await Prefill(store, ConventionType.RavenDefault); } using (var store = db.NewStore()) { ApplyPrefillConventions(store, ConventionType.NSBDefault); store.Initialize(); await Prefill(store, ConventionType.NSBDefault); } using (var store = db.NewStore()) { ApplyTestConventions(store, ConventionType.RavenDefault); store.Initialize(); var exception = Assert.Throws<InvalidOperationException>(() => store.Conventions.FindTypeTagName(typeof(TimeoutPersisters.RavenDB.TimeoutData))); Console.WriteLine($"Got expected exception: {exception.Message}"); } } }
public void Specific_stores_should_mask_default() { using (var db = new ReusableDB()) { var settings = new SettingsHolder(); settings.Set("Transactions.SuppressDistributedTransactions", true); settings.Set("TypesToScan", new Type[0]); settings.Set("NServiceBus.Routing.EndpointName", "FakeEndpoint"); settings.Set("NServiceBus.Transport.TransportInfrastructure", new FakeRavenDBTransportInfrastructure(TransportTransactionMode.None)); settings.Set("Endpoint.SendOnly", true); DocumentStoreManager.SetDocumentStore <StorageType.GatewayDeduplication>(settings, db.NewStore("GatewayDeduplication")); DocumentStoreManager.SetDocumentStore <StorageType.Outbox>(settings, db.NewStore("Outbox")); DocumentStoreManager.SetDocumentStore <StorageType.Sagas>(settings, db.NewStore("Sagas")); DocumentStoreManager.SetDocumentStore <StorageType.Subscriptions>(settings, db.NewStore("Subscriptions")); DocumentStoreManager.SetDocumentStore <StorageType.Timeouts>(settings, db.NewStore("Timeouts")); DocumentStoreManager.SetDefaultStore(settings, db.NewStore("Default")); var readOnly = settings as ReadOnlySettings; Assert.AreEqual("GatewayDeduplication", DocumentStoreManager.GetDocumentStore <StorageType.GatewayDeduplication>(readOnly).Identifier); Assert.AreEqual("Outbox", DocumentStoreManager.GetDocumentStore <StorageType.Outbox>(readOnly).Identifier); Assert.AreEqual("Sagas", DocumentStoreManager.GetDocumentStore <StorageType.Sagas>(readOnly).Identifier); Assert.AreEqual("Subscriptions", DocumentStoreManager.GetDocumentStore <StorageType.Subscriptions>(readOnly).Identifier); Assert.AreEqual("Timeouts", DocumentStoreManager.GetDocumentStore <StorageType.Timeouts>(readOnly).Identifier); } }
public void EnsureMappingDocumentIsUsed() { var context = new ContextBag(); using (var db = new ReusableDB()) { for (var i = 0; i < 5; i++) { using (var store = db.NewStore()) { ApplyTestConventions(store, ConventionType.RavenDefault); store.Initialize(); if (i > 0) { // On every iteration after the first, remove the index so that operations // will throw if the mapping document does not exist. store.DatabaseCommands.DeleteIndex("Raven/DocumentsByEntityName"); } var persister = new TimeoutPersister(store); persister.Add(new TimeoutData { Destination = EndpointName, Headers = new Dictionary <string, string>(), OwningTimeoutManager = EndpointName, SagaId = Guid.NewGuid(), Time = DateTime.UtcNow }, context).Wait(); } } } }
public async Task TestStoringSagas(ConventionType seedType) { using (var db = new ReusableDB()) { List<TestSagaData> sagas; using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); sagas = await Prefill(store, seedType); } using (var store = db.NewStore()) { Console.WriteLine($"Testing saga updates with DocumentStore initially configured for {seedType} conventions."); ApplyTestConventions(store, seedType); store.Initialize(); // Update each saga, once by id and once by correlation property foreach (var saga in sagas) { var persister = new SagaPersister(); Console.WriteLine($"Retrieving SagaId {saga.Id} by SagaId"); using (var session = store.OpenAsyncSession()) { var ravenDBSynchronizedStorageSession = new RavenDBSynchronizedStorageSession(session, false); var byId = await persister.Get<TestSagaData>(saga.Id, ravenDBSynchronizedStorageSession, null); Assert.IsNotNull(byId); byId.Counter++; await persister.Update(byId, ravenDBSynchronizedStorageSession, null); await session.SaveChangesAsync(); Console.WriteLine($"Retrieving SagaId {saga.Id} by Correlation Property OrderId={saga.OrderId}"); var byOrderId = await persister.Get<TestSagaData>("OrderId", saga.OrderId, ravenDBSynchronizedStorageSession, new ContextBag()); Assert.IsNotNull(byOrderId); byOrderId.Counter++; await persister.Update(byOrderId, ravenDBSynchronizedStorageSession, null); await session.SaveChangesAsync(); } } Console.WriteLine("Retrieving each saga again by SagaId and making sure Counter == 3"); foreach (var saga in sagas) { var persister = new SagaPersister(); using (var session = store.OpenAsyncSession()) { Console.WriteLine($"Retrieving SagaId {saga.Id} by SagaId"); var byId = await persister.Get<TestSagaData>(saga.Id, new RavenDBSynchronizedStorageSession(session, false), null); Assert.IsNotNull(byId); Assert.AreEqual(3, byId.Counter); } } } } }
public void EnsureMappingFailsWithoutIndex() { var context = new ContextBag(); using (var db = new ReusableDB()) { using (var store = db.NewStore()) { ApplyTestConventions(store, ConventionType.RavenDefault); store.Initialize(); // Remove the index to make sure the conventions will throw store.DatabaseCommands.DeleteIndex("Raven/DocumentsByEntityName"); var persister = new TimeoutPersister(store); var exception = Assert.Throws <AggregateException>(() => { persister.Add(new TimeoutData { Destination = EndpointName, Headers = new Dictionary <string, string>(), OwningTimeoutManager = EndpointName, SagaId = Guid.NewGuid(), Time = DateTime.UtcNow }, context).Wait(); }); Assert.IsInstanceOf <InvalidOperationException>(exception.GetBaseException()); Console.WriteLine($"Got expected Exception: {exception.Message}"); } } }
public void EnsureMappingFailsWithoutIndex() { var context = new ContextBag(); using (var db = new ReusableDB()) { using (var store = db.NewStore()) { ApplyTestConventions(store, ConventionType.RavenDefault); store.Initialize(); // Remove the index to make sure the conventions will throw store.DatabaseCommands.DeleteIndex("Raven/DocumentsByEntityName"); var persister = new TimeoutPersister(store); var exception = Assert.Throws<AggregateException>(() => { persister.Add(new TimeoutData { Destination = EndpointName, Headers = new Dictionary<string, string>(), OwningTimeoutManager = EndpointName, SagaId = Guid.NewGuid(), Time = DateTime.UtcNow }, context).Wait(); }); Assert.IsInstanceOf<InvalidOperationException>(exception.GetBaseException()); Console.WriteLine($"Got expected Exception: {exception.Message}"); } } }
public void EnsureMappingDocumentIsUsed() { var context = new ContextBag(); using (var db = new ReusableDB()) { for (var i = 0; i < 5; i++) { using (var store = db.NewStore()) { ApplyTestConventions(store, ConventionType.RavenDefault); store.Initialize(); if (i > 0) { // On every iteration after the first, remove the index so that operations // will throw if the mapping document does not exist. store.DatabaseCommands.DeleteIndex("Raven/DocumentsByEntityName"); } var persister = new TimeoutPersister(store); persister.Add(new TimeoutData { Destination = EndpointName, Headers = new Dictionary<string, string>(), OwningTimeoutManager = EndpointName, SagaId = Guid.NewGuid(), Time = DateTime.UtcNow }, context).Wait(); } } } }
public async Task TestRetrievingSagas(ConventionType seedType) { using (var db = new ReusableDB()) { List<TestSagaData> sagas; using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); sagas = await Prefill(store, seedType); } // Need to ensure multiple runs will work, after conventions document is stored for (var i = 0; i < 3; i++) { using (var store = db.NewStore()) { Console.WriteLine($"Testing saga lookups with DocumentStore initially configured for {seedType} conventions."); ApplyTestConventions(store, seedType); store.Initialize(); foreach (var saga in sagas) { var persister = new SagaPersister(); Console.WriteLine($"Retrieving SagaId {saga.Id} by SagaId"); TestSagaData byId; using (var session = store.OpenAsyncSession()) { byId = await persister.Get<TestSagaData>(saga.Id, new RavenDBSynchronizedStorageSession(session, false), null); } Assert.IsNotNull(byId); Assert.AreEqual(byId.Id, saga.Id); Assert.AreEqual(byId.OrderId, saga.OrderId); Assert.AreEqual(byId.OriginalMessageId, saga.OriginalMessageId); Assert.AreEqual(byId.Originator, saga.Originator); Assert.AreEqual(1, saga.Counter); Console.WriteLine($"Retrieving SagaId {saga.Id} by Correlation Property OrderId={saga.OrderId}"); using (var session = store.OpenAsyncSession()) { byId = await persister.Get<TestSagaData>("OrderId", saga.OrderId, new RavenDBSynchronizedStorageSession(session, false), new ContextBag()); } Assert.IsNotNull(byId); Assert.AreEqual(byId.Id, saga.Id); Assert.AreEqual(byId.OrderId, saga.OrderId); Assert.AreEqual(byId.OriginalMessageId, saga.OriginalMessageId); Assert.AreEqual(byId.Originator, saga.Originator); Assert.AreEqual(1, saga.Counter); } } } } }
public async Task TestRetrievingSagas(ConventionType seedType) { using (var db = new ReusableDB()) { List <TestSagaData> sagas; using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); sagas = await Prefill(store, seedType); } // Need to ensure multiple runs will work, after conventions document is stored for (var i = 0; i < 3; i++) { using (var store = db.NewStore()) { Console.WriteLine($"Testing saga lookups with DocumentStore initially configured for {seedType} conventions."); ApplyTestConventions(store, seedType); store.Initialize(); foreach (var saga in sagas) { var persister = new SagaPersister(); Console.WriteLine($"Retrieving SagaId {saga.Id} by SagaId"); TestSagaData byId; using (var session = store.OpenAsyncSession()) { byId = await persister.Get <TestSagaData>(saga.Id, new RavenDBSynchronizedStorageSession(session, false), null); } Assert.IsNotNull(byId); Assert.AreEqual(byId.Id, saga.Id); Assert.AreEqual(byId.OrderId, saga.OrderId); Assert.AreEqual(byId.OriginalMessageId, saga.OriginalMessageId); Assert.AreEqual(byId.Originator, saga.Originator); Assert.AreEqual(1, saga.Counter); Console.WriteLine($"Retrieving SagaId {saga.Id} by Correlation Property OrderId={saga.OrderId}"); using (var session = store.OpenAsyncSession()) { byId = await persister.Get <TestSagaData>("OrderId", saga.OrderId, new RavenDBSynchronizedStorageSession(session, false), new ContextBag()); } Assert.IsNotNull(byId); Assert.AreEqual(byId.Id, saga.Id); Assert.AreEqual(byId.OrderId, saga.OrderId); Assert.AreEqual(byId.OriginalMessageId, saga.OriginalMessageId); Assert.AreEqual(byId.Originator, saga.Originator); Assert.AreEqual(1, saga.Counter); } } } } }
public void CanGetNormalSession() { using (var db = new ReusableDB()) { var store = db.NewStore().Initialize(); var session = store.OpenAsyncSession(); var storageSession = new RavenDBSynchronizedStorageSession(session); var session2 = storageSession.RavenSession(); Assert.AreEqual(session, session2); } }
public async Task TestReceivingTimeouts(ConventionType seedType) { using (var db = new ReusableDB()) { string prefillIndex; using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); await Prefill(store, seedType); prefillIndex = CreateTimeoutIndex(store); } // Need to ensure multiple runs will work, after conventions document is stored for (var i = 0; i < 3; i++) { using (var store = db.NewStore()) { Console.WriteLine($"Testing receives with DocumentStore initially configured for {seedType} conventions."); ApplyTestConventions(store, seedType); store.Initialize(); var index = CreateTimeoutIndex(store); db.WaitForIndexing(store); Assert.AreEqual(index, prefillIndex, "Index definitions must match or previous timeouts will not be found."); var query = new QueryTimeouts(store, EndpointName); var chunkTuples = (await query.GetNextChunk(DateTime.UtcNow.AddYears(-10))).DueTimeouts.ToArray(); Assert.AreEqual(10, chunkTuples.Length); foreach (var tuple in chunkTuples) { Console.WriteLine($"Received timeout {tuple.Id}"); Assert.AreEqual(dueTimeout, tuple.DueTime); } } } } }
public void Specific_stores_should_mask_default() { using (var db = new ReusableDB()) { var cfg = new EndpointConfiguration("FakeEndpoint"); cfg.UseTransport(new LearningTransport()); cfg.SendOnly(); var persistence = cfg.UsePersistence <RavenDBPersistence>(); var settings = persistence.GetSettings(); DocumentStoreManager.SetDocumentStore <StorageType.Outbox>(settings, db.NewStore("Outbox")); DocumentStoreManager.SetDocumentStore <StorageType.Sagas>(settings, db.NewStore("Sagas")); DocumentStoreManager.SetDocumentStore <StorageType.Subscriptions>(settings, db.NewStore("Subscriptions")); DocumentStoreManager.SetDefaultStore(settings, db.NewStore("Default")); var readOnly = settings as ReadOnlySettings; Assert.AreEqual("Outbox", DocumentStoreManager.GetDocumentStore <StorageType.Outbox>(readOnly, null).Identifier); Assert.AreEqual("Sagas", DocumentStoreManager.GetDocumentStore <StorageType.Sagas>(readOnly, null).Identifier); Assert.AreEqual("Subscriptions", DocumentStoreManager.GetDocumentStore <StorageType.Subscriptions>(readOnly, null).Identifier); } }
public async Task TestMarkingAsComplete(ConventionType seedType) { using (var db = new ReusableDB()) { List<TestSagaData> sagas; using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); sagas = await Prefill(store, seedType); } using (var store = db.NewStore()) { Console.WriteLine($"Testing saga lookups with DocumentStore initially configured for {seedType} conventions."); ApplyTestConventions(store, seedType); store.Initialize(); // Update each saga, once by id and once by correlation property foreach (var saga in sagas) { var persister = new SagaPersister(); using (var session = store.OpenAsyncSession()) { Console.WriteLine($"Retrieving SagaId {saga.Id} by Correlation Property OrderId={saga.OrderId} and completing."); var ravenDBSynchronizedStorageSession = new RavenDBSynchronizedStorageSession(session, false); var contextBag = new ContextBag(); var byOrderId = await persister.Get<TestSagaData>("OrderId", saga.OrderId, ravenDBSynchronizedStorageSession, contextBag); Assert.IsNotNull(byOrderId); await persister.Complete(byOrderId, ravenDBSynchronizedStorageSession, contextBag); await session.SaveChangesAsync(); } } db.WaitForIndexing(store); // Ensure terms are still the saga type and unique identity type var terms = store.DatabaseCommands.GetTerms("Raven/DocumentsByEntityName", "Tag", null, 1024).ToList(); Assert.AreEqual(2, terms.Count); foreach (var term in terms) { var query = new IndexQuery { Query = "Tag:" + term, PageSize = 0 }; // Ensure there are none left var queryResult = store.DatabaseCommands.Query("Raven/DocumentsByEntityName", query); Assert.AreEqual(0, queryResult.TotalResults); } } } }
public async Task EnsureOldAndNewTimeoutsCanBeReceived(ConventionType seedType) { using (var db = new ReusableDB()) { using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); await Prefill(store, seedType); } using (var store = db.NewStore()) { ApplyTestConventions(store, seedType); store.Initialize(); CreateTimeoutIndex(store); var persister = new TimeoutPersister(store); for (var i = 1; i <= 10; i++) { await persister.Add(new TimeoutData { Destination = EndpointName, Headers = new Dictionary<string, string>(), OwningTimeoutManager = EndpointName, SagaId = Guid.NewGuid(), Time = dueTimeout }, new ContextBag()); } db.WaitForIndexing(store); var query = new QueryTimeouts(store, EndpointName); var chunkTuples = (await query.GetNextChunk(DateTime.UtcNow.AddYears(-10))).DueTimeouts.ToArray(); Assert.AreEqual(20, chunkTuples.Length); foreach (var tuple in chunkTuples) { Console.WriteLine($"Received timeout {tuple.Id}"); Assert.AreEqual(dueTimeout, tuple.DueTime); } } } }
public async Task Serializer_keeps_non_matching_properties() { using (var db = new ReusableDB()) { var store = db.NewStore().Initialize(); // Create JSON doc mimicking a V4 document containing a new property var likeV4 = new LikeV4Subscriptions(); likeV4.Clients = new List <LegacyAddress> { new LegacyAddress("A", "B") }; likeV4.Subscriptions = new List <SubscriptionClient> { new SubscriptionClient { Endpoint = "C", TransportAddress = "D" } }; var docJson = RavenJObject.FromObject(likeV4); // Create metadata to make it look like an older version var docMetadata = new RavenJObject(); var fakeDocType = typeof(LikeV3Subscriptions); docMetadata["Raven-Entity-Name"] = fakeDocType.Name; // Doesn't appear to matter, but $"{fakeDocType.FullName}, {fakeDocType.Assembly.GetName().Name}" may be more accurate docMetadata["Raven-Clr-Type"] = fakeDocType.AssemblyQualifiedName; // Store document as "old" format await store.AsyncDatabaseCommands.PutAsync("TestDocument/1", Etag.Empty, docJson, docMetadata); // Load and modify the document using the "old" type that doesn't know about new property using (var session = store.OpenAsyncSession()) { var subs = await session.LoadAsync <LikeV3Subscriptions>("TestDocument/1"); subs.Clients.Add(new LegacyAddress("E", "F")); await session.SaveChangesAsync(); } var resultDoc = await store.AsyncDatabaseCommands.GetAsync("TestDocument/1"); var resultJson = resultDoc.DataAsJson; var clients = resultJson["Clients"] as RavenJArray; var subscriptions = resultJson["Subscriptions"] as RavenJArray; Assert.IsNotNull(clients); Assert.IsNotNull(subscriptions); Assert.AreEqual(1, subscriptions.Length); Assert.AreEqual(2, clients.Length); var sub = (RavenJObject)subscriptions[0]; Assert.AreEqual("C", sub["Endpoint"].Value <string>()); Assert.AreEqual("D", sub["TransportAddress"].Value <string>()); var client0 = (RavenJObject)clients[0]; var client1 = (RavenJObject)clients[1]; Assert.AreEqual("A", client0["Queue"].Value <string>()); Assert.AreEqual("B", client0["Machine"].Value <string>()); Assert.AreEqual("E", client1["Queue"].Value <string>()); Assert.AreEqual("F", client1["Machine"].Value <string>()); } }
public async Task TestStoringSagas(ConventionType seedType) { using (var db = new ReusableDB()) { List <TestSagaData> sagas; using (var store = db.NewStore()) { ApplyPrefillConventions(store, seedType); store.Initialize(); sagas = await Prefill(store, seedType); } using (var store = db.NewStore()) { Console.WriteLine($"Testing saga updates with DocumentStore initially configured for {seedType} conventions."); ApplyTestConventions(store, seedType); store.Initialize(); // Update each saga, once by id and once by correlation property foreach (var saga in sagas) { var persister = new SagaPersister(); Console.WriteLine($"Retrieving SagaId {saga.Id} by SagaId"); using (var session = store.OpenAsyncSession()) { var ravenDBSynchronizedStorageSession = new RavenDBSynchronizedStorageSession(session, false); var byId = await persister.Get <TestSagaData>(saga.Id, ravenDBSynchronizedStorageSession, null); Assert.IsNotNull(byId); byId.Counter++; await persister.Update(byId, ravenDBSynchronizedStorageSession, null); await session.SaveChangesAsync(); Console.WriteLine($"Retrieving SagaId {saga.Id} by Correlation Property OrderId={saga.OrderId}"); var byOrderId = await persister.Get <TestSagaData>("OrderId", saga.OrderId, ravenDBSynchronizedStorageSession, new ContextBag()); Assert.IsNotNull(byOrderId); byOrderId.Counter++; await persister.Update(byOrderId, ravenDBSynchronizedStorageSession, null); await session.SaveChangesAsync(); } } Console.WriteLine("Retrieving each saga again by SagaId and making sure Counter == 3"); foreach (var saga in sagas) { var persister = new SagaPersister(); using (var session = store.OpenAsyncSession()) { Console.WriteLine($"Retrieving SagaId {saga.Id} by SagaId"); var byId = await persister.Get <TestSagaData>(saga.Id, new RavenDBSynchronizedStorageSession(session, false), null); Assert.IsNotNull(byId); Assert.AreEqual(3, byId.Counter); } } } } }