public void ShouldParseMessageWithTrigger() { var factoidDataStore = GetDataStore("Factoid"); InitializeSubject(); // todo: use an auto mocker so i dont have to do this shit manually const string trigger = "hello"; const string reply = "sup man"; factoidDataStore.Setup(dsm => dsm.GetRandomValue(trigger)) .Returns(new DataStoreValue(0, trigger, "<reply> " + reply)); VariableHandler.Setup( vh => vh.Substitute(It.IsAny <string>(), It.IsAny <IMessage>(), It.IsAny <VariableReplacement[]>())) .Returns <string, IMessage, VariableReplacement[]>((val, msg, repls) => val); var messageStub = new StubMessage() { Action = false, Text = trigger, Where = "some_place", Who = "SomeDude69" }; var returnValue = Subject.Process(messageStub, true); returnValue.Message.Should().Be(reply); factoidDataStore.Verify(dsm => dsm.GetRandomValue(trigger), Times.Once); VariableHandler.Verify( vh => vh.Substitute(It.IsAny <string>(), It.IsAny <IMessage>()), Times.Once); }
public void ShouldRememberMessageThatTargetDidSay() { const string name = "Dude"; const string rememberTarget = "RememberMe"; const string rememberMsg = "hello man world"; const string logMsg = "sup guys hello man world"; var expectedResponse = String.Format("Okay, {0}, remembering \"{1}\".", name, rememberMsg); var logDataStore = GetDataStore("Log"); logDataStore.Setup(ids => ids.GetAllValues(name)).Returns(new List <string> { logMsg }); var remDataStore = GetDataStore("Remember"); InitializeSubject(); var messengerMock = new Mock <IMessenger>(); var messageStub = new StubMessage() { Action = false, Text = String.Format("remember {0} {1}", rememberTarget, rememberMsg), Where = "some_place", Who = name }; var returnValue = Subject.Digest(messengerMock.Object, messageStub, true); returnValue.Should().BeFalse(); remDataStore.Verify(ids => ids.GetAllValues(name), Times.Once); remDataStore.Verify(ids => ids.Put(rememberTarget, logMsg), Times.Once); messengerMock.Verify(im => im.SendMessage(expectedResponse, messageStub.Where, false), Times.Once); }
public async Task RunNew() { var cache = new MessageHandlerRegistry(new Conventions()); cache.RegisterHandler(typeof(StubMessageHandler)); cache.RegisterHandler(typeof(StubTimeoutHandler)); var stubMessage = new StubMessage(); var stubTimeout = new StubTimeoutState(); var messageHandler = cache.GetCachedHandlerForMessage <StubMessage>(); var timeoutHandler = cache.GetCachedHandlerForMessage <StubTimeoutState>(); await messageHandler.Invoke(stubMessage, null); await timeoutHandler.Invoke(stubTimeout, null); var startNew = Stopwatch.StartNew(); for (var i = 0; i < 100000; i++) { await messageHandler.Invoke(stubMessage, null); await timeoutHandler.Invoke(stubTimeout, null); } startNew.Stop(); Trace.WriteLine(startNew.ElapsedMilliseconds); }
public void ShouldParseMessageWithNoVariables() { var factoidDataStore = GetDataStore("Factoid"); factoidDataStore.Setup( rds => rds.Put(It.IsAny <string>(), It.IsAny <string>())) .Returns(true); InitializeSubject(); // todo: use an auto mocker so i dont have to do this shit manually const string replyMsg = "hello man"; const string name = "Dude"; var expectedResponse = String.Format("Okay, {0}.", name); var messageStub = new StubMessage() { Action = false, Text = "hello <reply> " + replyMsg, Where = "some_place", Who = name }; var returnValue = Subject.Process(messageStub, true); returnValue.Message.Should().Be(expectedResponse); }
static async Task Main(string[] args) { string url = "http://localhost:2209/StubMessage"; HubConnectionBuilder hubConnectionBuilder = new HubConnectionBuilder(); hubConnectionBuilder.WithUrl(url); hubConnectionBuilder.WithAutomaticReconnect(); HubConnection hubConnection = hubConnectionBuilder.Build(); hubConnection.RegisterClosedHandler(); hubConnection.RegisterReconnectingHandler(); hubConnection.RegisterReconnectedHandler(); await hubConnection.StartAsync(); while (true) { Console.WriteLine("请输入消息标题!"); string title = Console.ReadLine(); Console.WriteLine("请输入消息内容!"); string content = Console.ReadLine(); StubMessage message = new StubMessage(title, content); await MessageMediator.SendAsync(hubConnection, message); Console.WriteLine("发送成功!"); Console.WriteLine("-------------------------------------"); } }
public void Should_have_passed_through_correct_message() { HandlerInvocationCache.CacheMethodForHandler(typeof(StubHandler), typeof(StubMessage)); var handler = new StubHandler(); var stubMessage = new StubMessage(); HandlerInvocationCache.InvokeHandle(handler, stubMessage); Assert.AreEqual(stubMessage, handler.HandledMessage); }
public void Should_have_passed_through_correct_message() { var cache = new MessageHandlerRegistry(new Conventions()); cache.CacheMethodForHandler(typeof(StubHandler), typeof(StubMessage)); var handler = new StubHandler(); var stubMessage = new StubMessage(); cache.InvokeHandle(handler, stubMessage); Assert.AreEqual(stubMessage, handler.HandledMessage); }
public async Task Should_have_passed_through_correct_message() { var cache = new MessageHandlerRegistry(new Conventions()); cache.RegisterHandler(typeof(StubHandler)); var handler = cache.GetCachedHandlerForMessage <StubMessage>(); var stubMessage = new StubMessage(); await handler.Invoke(stubMessage, null); Assert.AreEqual(stubMessage, ((StubHandler)handler.Instance).HandledMessage); }
private void TestQuoteCommand(string quoteTarget, string expectedResponse) { // Setup var messageStub = new StubMessage(String.Format("quote {0}", quoteTarget), where : "some_place", who: SendingUsersName); InitializeSubject(); // Act var returnValue = Subject.Process(messageStub, true); // Verify returnValue.Message.Should().Be(expectedResponse); }
public void RunNew() { HandlerInvocationCache.CacheMethodForHandler(typeof(StubMessageHandler), typeof(StubMessage)); HandlerInvocationCache.CacheMethodForHandler(typeof(StubTimeoutHandler), typeof(StubTimeoutState)); var handler1 = new StubMessageHandler(); var handler2 = new StubTimeoutHandler(); var stubMessage1 = new StubMessage(); var stubMessage2 = new StubTimeoutState(); HandlerInvocationCache.InvokeHandle(handler1, stubMessage1); HandlerInvocationCache.InvokeHandle(handler2, stubMessage2); var startNew = Stopwatch.StartNew(); for (var i = 0; i < 100000; i++) { HandlerInvocationCache.InvokeHandle(handler1, stubMessage1); HandlerInvocationCache.InvokeHandle(handler2, stubMessage2); } startNew.Stop(); Trace.WriteLine(startNew.ElapsedMilliseconds); }
/// <summary> /// Получить или сгенерировать TraceId и установить контекст трейсинга. /// </summary> /// <param name="properties">Метаданные сообщения.</param> /// <param name="settings">Настройки трейсинга.</param> /// <param name="logger">Логгер.</param> /// <param name="stubMessage">TraceId из тела сообщения.</param> /// <param name="loggingScope">Скоуп.</param> /// <returns>Идентификатор отслеживания.</returns> public static void EnsureTraceId( this IBasicProperties properties, TracingSettings settings, ILogger logger, ref StubMessage stubMessage, Dictionary <string, object?> loggingScope ) { if (!TryGetTraceInfo(properties, stubMessage.TraceId, out var traceId, out var traceIdSource)) { if (settings.GenerateIfNotPresent) { traceId = Guid.NewGuid(); if (settings.LogWhenGenerated) { using (logger.BeginScope(loggingScope)) { logger.LogInformation("TraceId не указан. Сгенерирован новый {TraceId}.", traceId); } } } } TraceContext.Create(traceId, traceIdSource); loggingScope["TraceId"] = traceId; loggingScope["TraceIdSource"] = traceIdSource; properties.AddTraceId(); if (traceId.HasValue) { stubMessage.TraceId = traceId.Value; } }
public void OnEvent(StubMessage @event) { this.Newsflash = @event.NewsflashTitle; }