public PubSubTestsFixture() { TestTopicName = $"testTopic_{DateTime.Now.ToEpochTime()}"; _config = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build(); CredentialPath = _config["CredentialPath"]; ProjectId = _config["GcpProjectId"]; var messenger = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = CredentialPath, ProjectId = ProjectId, ReceiverConfig = new ReceiverConfig() { EntityName = TestTopicName, CreateEntityIfNotExists = true }, Sender = new SenderConfig() { EntityName = TestTopicName, CreateEntityIfNotExists = true } }); Messenger = messenger; ReactiveMessenger = messenger; }
public void Test_PubSubMessenger_MessageFiltering() { // Arrange var messenger = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = _fixture.CredentialPath, ProjectId = _fixture.ProjectId }); var manager = messenger.EntityManager as PubSubManager; // Act // Create the filter topic for testing. manager.CreateTopicDefaults(_fixture.MessageFilterTopic, null, "defaultsub").GetAwaiter().GetResult(); manager.CreateSubscription(_fixture.MessageFilterTopic, "filteredsub", "attributes:pickme").GetAwaiter().GetResult(); // Send two messages, one that wont be picked up by the filter subscription and the other that // will. The result is two messages to the defaultsub, one to filteredsub messenger.Send(_fixture.MessageFilterTopic, "test").GetAwaiter().GetResult(); messenger.Send(_fixture.MessageFilterTopic, "testfilter", new KeyValuePair <string, object>[] { new KeyValuePair <string, object>("pickme", "please") }).GetAwaiter().GetResult(); // Receive from both subscriptions. var nonFilteredMessages = messenger.ReceiveBatch <string>("defaultsub", 100).GetAwaiter().GetResult(); var filteredMessages = messenger.ReceiveBatch <string>("filteredsub", 100).GetAwaiter().GetResult(); var filteredMessage = messenger.ReceiveOne <string>("filteredsub").GetAwaiter().GetResult(); // Assert nonFilteredMessages.Count().Should().Be(2); filteredMessages.Count().Should().Be(1); filteredMessage.Should().NotBe(null); filteredMessage.Should().Be("testfilter"); }
public void Test_PubSubMessenger_ErrorMessage() { // Arrange var lorem = Lorem.GetSentence(5); var deadletterReader = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = _fixture.CredentialPath, ProjectId = _fixture.ProjectId, ReceiverConfig = new ReceiverConfig() { EntityName = _fixture.TestTopicName, CreateEntityIfNotExists = true, ReadFromErrorEntity = true } }); // Act _fixture.Messenger.Send(lorem).GetAwaiter().GetResult(); var msg = _fixture.Messenger.ReceiveOneEntity <string>(); _fixture.Messenger.Error(msg, "Something went wrong!").GetAwaiter().GetResult(); var deadletterMsg = deadletterReader.ReceiveOneEntity <string>(); // Assert deadletterMsg.Body.Should().NotBeNullOrEmpty(); deadletterMsg.Body.Should().BeEquivalentTo(lorem); deadletterMsg.Properties["ErrorReason"].Should().NotBeNull(); }
public void Test_PubSubMessenger_InvalidOperation() { // Arrange var messenger = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build()["CredentialPath"], ProjectId = "12345" }); // Act/Assert Assert.Throws <InvalidOperationException>(() => messenger.Receive <object>(null, null)); Assert.Throws <ArgumentException>(() => messenger.ReceiveOne <object>("").GetAwaiter().GetResult()); Assert.Throws <InvalidOperationException>(() => messenger.StartReceive <object>()); Assert.Throws <InvalidOperationException>(() => messenger.ReceiveOne <object>()); Assert.Throws <InvalidOperationException>(() => messenger.ReceiveBatchEntity <object>().GetAwaiter().GetResult()); // Additional runs here purely to increase code coverage. Not particularly useful tests but it does confirm functionality. messenger.Complete <object>(null).GetAwaiter().GetResult(); messenger.CompleteAll(new object[] { null }).GetAwaiter().GetResult(); messenger.Abandon(new object()); messenger.Error <object>(null).GetAwaiter().GetResult(); messenger.Error(new object()).GetAwaiter().GetResult(); var props = messenger.ReadProperties <object>(null); props.Should().BeNull(); }
public void Test_PubSubMessenger_StreamMessages() { // Arrange var pubsub = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = _fixture.CredentialPath, ProjectId = _fixture.ProjectId, ReceiverConfig = new ReceiverConfig { EntityName = _fixture.StreamTopicName, CreateEntityIfNotExists = true }, Sender = new SenderConfig { EntityName = _fixture.StreamTopicName, CreateEntityIfNotExists = true } }); var lorem = Lorem.GetSentences(10); var waitTimer = new Stopwatch(); var messagesProcessed = 0; var processedAfterCancelCount = 0; // Act pubsub.Send(lorem).GetAwaiter().GetResult(); pubsub.Receive <string>((m) => { pubsub.Complete(m).GetAwaiter().GetResult(); messagesProcessed++; processedAfterCancelCount++; }, (e) => { }); waitTimer.Start(); do { Task.Delay(500).GetAwaiter().GetResult(); } while (waitTimer.Elapsed.TotalSeconds < 5); pubsub.CancelReceive <string>(); Task.Delay(500).GetAwaiter().GetResult(); waitTimer.Reset(); waitTimer.Start(); processedAfterCancelCount = 0; // verify no more messages were processed after cancellation. do { Task.Delay(500).GetAwaiter().GetResult(); } while (waitTimer.Elapsed.TotalSeconds < 5); // Assert messagesProcessed.Should().BeGreaterOrEqualTo(1); processedAfterCancelCount.Should().Be(0); }
public void Test_PubSubManager_CreateEntity() { // Arrange var pubsub = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = _credentialPath, ProjectId = _projectId, }); var topicName = $"testTopic_{ DateTime.Now.ToEpochTime() }"; var createEntity = new ReceiverConfig { ProjectId = _projectId, EntityName = topicName }; // Verify does not exist pubsub.EntityManager.EntityExists(topicName).GetAwaiter().GetResult().Should().BeFalse(); // Create a topic pubsub.EntityManager.CreateEntity(createEntity).GetAwaiter().GetResult(); ((PubSubManager)pubsub.EntityManager).SubscriptionExists($"{topicName}_default").GetAwaiter().GetResult().Should().BeTrue(); // Verify the topic exists pubsub.EntityManager.EntityExists(topicName).GetAwaiter().GetResult().Should().BeTrue(); // Attempt to create if not exist ((PubSubManager)pubsub.EntityManager).CreateTopicIfNotExists(topicName).GetAwaiter().GetResult(); // Delete topic ((PubSubManager)pubsub.EntityManager).DeleteEntity(topicName).GetAwaiter().GetResult(); ((PubSubManager)pubsub.EntityManager).DeleteTopic($"{topicName}_deadletter").GetAwaiter().GetResult(); ((PubSubManager)pubsub.EntityManager).DeleteSubscription($"{topicName}_deadletter_default").GetAwaiter().GetResult(); ((PubSubManager)pubsub.EntityManager).DeleteSubscription($"{topicName}_deadletter_default").GetAwaiter().GetResult(); // done twice for branch coverage. ((PubSubManager)pubsub.EntityManager).DeleteTopic(topicName).GetAwaiter().GetResult(); // done twice for branch coverage. // Attempt to create if not exists ((PubSubManager)pubsub.EntityManager).CreateTopicIfNotExists(topicName).GetAwaiter().GetResult(); ((PubSubManager)pubsub.EntityManager).CreateTopicIfNotExists(topicName).GetAwaiter().GetResult(); // called twice for branch coverage. ((PubSubManager)pubsub.EntityManager).CreateSubscription(topicName, "OtherSub2").GetAwaiter().GetResult(); ((PubSubManager)pubsub.EntityManager).CreateSubscription(topicName, "OtherSub2").GetAwaiter().GetResult(); // called twice for coverage. ((PubSubManager)pubsub.EntityManager).SubscriptionExists("OtherSub2").GetAwaiter().GetResult().Should().BeTrue(); // Verify exists ((PubSubManager)pubsub.EntityManager).TopicExists(topicName).GetAwaiter().GetResult().Should().BeTrue(); // Finally... delete pubsub.EntityManager.DeleteEntity(topicName).GetAwaiter().GetResult(); pubsub.EntityManager.DeleteEntity($"{topicName}_deadletter").GetAwaiter().GetResult(); // done twice for branch coverage. // Verify does not exist ((PubSubManager)pubsub.EntityManager).TopicExists(topicName).GetAwaiter().GetResult().Should().BeFalse(); ((PubSubManager)pubsub.EntityManager).SubscriptionExists($"{topicName}_default").GetAwaiter().GetResult().Should().BeFalse(); ((PubSubManager)pubsub.EntityManager).SubscriptionExists("OtherSub2").GetAwaiter().GetResult().Should().BeFalse(); }
public void Test_PubSubMessenger_AbandonMessageEntity1() { // Arrange using var pubSub = new PubSubMessenger(new PubSubConfig { ProjectId = "test" }); var test = new PubSubMessageEntity <string> { Body = "test" }; // Act pubSub.Messages.AddOrUpdate(test.Body, null); pubSub.Abandon(test).GetAwaiter().GetResult(); // Act/Assert pubSub.Messages.ContainsKey(test).Should().BeFalse(); pubSub.Messages.Count.Should().Be(0); }
public void Test_PubSubMessenger_NotImplemented() { // Arrange var pubSub = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = _fixture.CredentialPath, ProjectId = _fixture.ProjectId }); // Act/Assert - Manager methods. Assert.Throws <NotImplementedException>(() => pubSub.EntityManager.GetReceiverEntityUsagePercentage().GetAwaiter().GetResult()); Assert.Throws <NotImplementedException>(() => pubSub.EntityManager.GetSenderEntityUsagePercentage().GetAwaiter().GetResult()); Assert.Throws <NotImplementedException>(() => pubSub.EntityManager.GetReceiverMessageCount().GetAwaiter().GetResult()); Assert.Throws <NotImplementedException>(() => pubSub.EntityManager.GetSenderMessageCount().GetAwaiter().GetResult()); Assert.Throws <NotImplementedException>(() => pubSub.EntityManager.IsReceiverEntityDisabled().GetAwaiter().GetResult()); Assert.Throws <NotImplementedException>(() => pubSub.EntityManager.IsSenderEntityDisabled().GetAwaiter().GetResult()); }
public void Test_PubSubMessenger_Abandon() { // Arrange using var pubSub = new PubSubMessenger(new PubSubConfig { ProjectId = "test" }); var test = "test"; // Act pubSub.Messages.AddOrUpdate(test, null); pubSub.Abandon(test).GetAwaiter().GetResult(); pubSub.Abandon <object>(null).GetAwaiter().GetResult(); // done for branch coverage. // Act/Assert pubSub.Messages.ContainsKey(test).Should().BeFalse(); pubSub.Messages.Count.Should().Be(0); pubSub.Dispose(); // done for branch coverage. }
public PerformanceTesting() { var config = new ConfigurationBuilder().AddJsonFile("appSettings.json").Build(); Messenger = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = config["CredentialPath"], ProjectId = config["GcpProjectId"], }); Manager = Messenger.EntityManager as PubSubManager; Manager.CreateEntity(new ReceiverConfig { EntityName = TestTopicName }).GetAwaiter().GetResult(); // Ensure there are messages to receive. var sentences = Lorem.GetSentences(2000); Messenger.SendBatch(TestTopicName, sentences).GetAwaiter().GetResult(); }
public void Test_PubSubManager_CreateTopicIfNotExists() { // Arrange var manager = new PubSubMessenger(new PubSubJsonAuthConfig() { JsonAuthFile = _credentialPath, ProjectId = _projectId, }).EntityManager as PubSubManager; var topicName = $"testTopic_creation"; try { // Act var topicExistsBefore = manager.TopicExists(topicName).GetAwaiter().GetResult(); var subscriptionExistsBefore = manager.SubscriptionExists($"{topicName}_default").GetAwaiter().GetResult(); var deadletterTopicExistBefore = manager.TopicExists($"{topicName}_deadletter_default").GetAwaiter().GetResult(); var deadletterSubscriptionExistsBefore = manager.SubscriptionExists($"{topicName}_deadletter_default").GetAwaiter().GetResult(); // Main action to test. manager.CreateTopicIfNotExists(topicName, true).GetAwaiter().GetResult(); var topicExistAfter = manager.TopicExists(topicName).GetAwaiter().GetResult(); var subscriptionExistsAfter = manager.SubscriptionExists($"{topicName}_default").GetAwaiter().GetResult(); var deadletterTopicExistAfter = manager.TopicExists($"{topicName}_deadletter").GetAwaiter().GetResult(); var deadletterSubscriptionExistsAfter = manager.SubscriptionExists($"{topicName}_deadletter_default").GetAwaiter().GetResult(); // Assert topicExistsBefore.Should().BeFalse(); subscriptionExistsBefore.Should().BeFalse(); deadletterTopicExistBefore.Should().BeFalse(); deadletterSubscriptionExistsBefore.Should().BeFalse(); topicExistAfter.Should().BeTrue(); subscriptionExistsAfter.Should().BeTrue(); deadletterTopicExistAfter.Should().BeTrue(); deadletterSubscriptionExistsAfter.Should().BeTrue(); } finally { manager.DeleteEntity(topicName).GetAwaiter().GetResult(); manager.DeleteEntity($"{topicName}_deadletter").GetAwaiter().GetResult(); } }
private static IServiceCollection AddNamedInstance <T>(IServiceCollection services, string key, PubSubMessenger instance) where T : INamedInstance { if (!key.IsNullOrEmpty()) { instance.Name = key; } services.AddSingleton(typeof(T), instance); // Ensure there's a NamedInstance factory to allow named collections of the messenger. services.AddFactoryIfNotAdded <T>(); return(services); }