public async Task Retry_policy_should_work_with_single_middleware() { var eventMessageHandler = new EventMessageHandler(new List <string>()); var clientWithRetry = HttpClientFactory.Create() .WithPolicy( Policy <HttpResponseMessage> .Handle <HttpRequestException>() .OrResult(result => result.StatusCode >= HttpStatusCode.InternalServerError || result.StatusCode == HttpStatusCode.RequestTimeout) .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))) .WithMessageHandler(eventMessageHandler) .Build(); Task <HttpResponseMessage> responseTask = null; await Assert.RaisesAsync <EventMessageHandler.RequestEventArgs>( h => eventMessageHandler.Request += h, h => eventMessageHandler.Request -= h, () => responseTask = clientWithRetry.GetAsync($"{_server.Urls[0]}{_endpointUri}")); var response = await responseTask; Assert.Equal(2, _server.LogEntries.Count()); Assert.Single(_server.LogEntries, le => (HttpStatusCode)le.ResponseMessage.StatusCode == HttpStatusCode.OK); Assert.Single(_server.LogEntries, le => (HttpStatusCode)le.ResponseMessage.StatusCode == HttpStatusCode.RequestTimeout); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("Hello world!", await response.Content.ReadAsStringAsync()); }
public async Task Client_with_retry_and_timeout_policy_should_properly_apply_policies_with_multiple_middlewares() { var visitedMiddleware = new List <string>(); var trafficRecorderMessageHandler = new TrafficRecorderMessageHandler(visitedMiddleware); var eventMessageHandler = new EventMessageHandler(visitedMiddleware); //timeout after 2 seconds, then retry var clientWithRetry = HttpClientFactory.Create() .WithPolicy( Policy <HttpResponseMessage> .Handle <HttpRequestException>() .OrResult(result => result.StatusCode >= HttpStatusCode.InternalServerError || result.StatusCode == HttpStatusCode.RequestTimeout) .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))) .WithPolicy( Policy.TimeoutAsync <HttpResponseMessage>(TimeSpan.FromSeconds(4), TimeoutStrategy.Optimistic)) .WithMessageHandler(eventMessageHandler) .WithMessageHandler(trafficRecorderMessageHandler) .Build(); Task <HttpResponseMessage> responseTask = null; var raisedEvent = await Assert.RaisesAsync <EventMessageHandler.RequestEventArgs>( h => eventMessageHandler.Request += h, h => eventMessageHandler.Request -= h, () => responseTask = clientWithRetry.GetAsync($"{_server.Urls[0]}{_endpointUriTimeout}")); var responseWithTimeout = await responseTask; Assert.True(raisedEvent.Arguments.Request.Headers.Contains("foobar")); Assert.Equal("foobar", raisedEvent.Arguments.Request.Headers.GetValues("foobar").FirstOrDefault()); Assert.Single(trafficRecorderMessageHandler.Traffic); Assert.Equal(4, _server.LogEntries.Count()); Assert.Equal(HttpStatusCode.RequestTimeout, responseWithTimeout.StatusCode); }
public void SdfDatabaseTestInit() { SqlHelper.CreateSqlCeByPath(clientSdf, true); SqlHelper.CreateSqlCeByPath(serverSdf, true); // File.Copy("db.sdf", serverSdf, true); new SchemaExport(clCfg).Execute(false, true, false); new SchemaExport(sCfg).Execute(false, true, false); ClearCache(clFactory); ClearCache(sFactory); clSession = clFactory.OpenSession(); sSession = sFactory.OpenSession(); sSession.FlushMode = FlushMode.Commit; clSession.FlushMode = FlushMode.Commit; session = clSession; handler = this.Subscribe(Event.NewSession, (e) => { var s = e.GetValue <ISession>(MessageKeys.Session); if (clSession.SessionFactory == s.SessionFactory) { clSession = s; session = s; } else if (sSession.SessionFactory == s.SessionFactory) { sSession = s; } }); }
public async Task Exception_translator_without_errors_should_not_affect_anything() { var requestEventHandler = A.Fake <EventHandler <EventMessageHandler.RequestEventArgs> >(); var responseEventHandler = A.Fake <EventHandler <EventMessageHandler.ResponseEventArgs> >(); var requestExceptionEventHandler = A.Fake <EventHandler <HttpRequestException> >(); var transformedRequestExceptionEventHandler = A.Fake <EventHandler <Exception> >(); var visitedMiddleware = new List <string>(); var trafficRecorderMessageHandler = new TrafficRecorderMessageHandler(visitedMiddleware); var eventMessageHandler = new EventMessageHandler(visitedMiddleware); eventMessageHandler.Request += requestEventHandler; eventMessageHandler.Response += responseEventHandler; var client = HttpClientFactory.Create() .WithMessageExceptionHandler(ex => true, ex => ex, requestExceptionEventHandler, transformedRequestExceptionEventHandler) .WithMessageHandler(eventMessageHandler) .WithMessageHandler(trafficRecorderMessageHandler) .Build(); await client.GetAsync($"{_server.Urls[0]}{_endpointUri}"); A.CallTo(() => requestEventHandler.Invoke(A <EventMessageHandler> .That.IsSameAs(eventMessageHandler), A <EventMessageHandler.RequestEventArgs> .That.Matches(e => e.Request.Headers.Single(h => h.Key == "foobar").Value.FirstOrDefault() == "foobar"))).MustHaveHappenedOnceExactly() .Then(A.CallTo(() => responseEventHandler.Invoke(A <EventMessageHandler> .That.IsSameAs(eventMessageHandler), A <EventMessageHandler.ResponseEventArgs> .That.Matches(e => e.Response.Headers.Single(h => h.Key == "foobar").Value.FirstOrDefault() == "foobar"))).MustHaveHappenedOnceExactly()); A.CallTo(() => requestExceptionEventHandler.Invoke(A <ExceptionTranslatorRequestMiddleware> ._, A <HttpRequestException> ._)).MustNotHaveHappened(); A.CallTo(() => transformedRequestExceptionEventHandler.Invoke(A <ExceptionTranslatorRequestMiddleware> ._, A <Exception> ._)).MustNotHaveHappened(); var traffic = Assert.Single(trafficRecorderMessageHandler.Traffic); Assert.Equal(HttpStatusCode.OK, traffic.Item2.StatusCode); Assert.Equal(new [] { nameof(TrafficRecorderMessageHandler), nameof(EventMessageHandler) }, visitedMiddleware); }
private static void SubscribeEvent <TMessage, THandler>(Processor processor, string handlerName, Func <THandler, IEvent <TMessage>, Task> handlerFunc, THandler instance = null) where THandler : class { var handler = new EventMessageHandler <TMessage, THandler>(handlerFunc, instance); processor.RegisterMessageHandler <TMessage>(handlerName, handler); }
public Connection(DiscordShardedClient client, IDiscordLogger discordLogger, ILogger logger, ICommandHandler commandHandler, IExpHandler expHandler, EventMessageHandler eventMsgHandler, IUserLeftHandler userLeftHandler) { _client = client; _discordLogger = discordLogger; _logger = logger; _commandHandler = commandHandler; _expHandler = expHandler; _eventMsgHandler = eventMsgHandler; _userLeftHandler = userLeftHandler; }
public MainWindow() { InitializeComponent(); _eventMessageHandler = this.Subscribe(Consts.StartUp, message => { Top = message.GetValue <int>("top"); Left = message.GetValue <int>("left"); Show(); this.Unsubscribe(_eventMessageHandler); }); }
public HrEditorViewModel(ISession session) { Contract.Assume(AuthorityController.CurrentDoctor != null); this.session = session; handler = this.Subscribe(Event.NewSession, (e) => { var s = e.GetValue <ISession>(MessageKeys.Session); ReplaceSession(s); }); }
public UraSettingWindow() { InitializeComponent(); handler = this.Subscribe((int)Event.OpenSelector, (e) => { var vm = e.GetValue <SelectorViewModel>(Messenger.Selector); var dialog = new SelectorDialog(); dialog.Owner = this; dialog.DataContext = vm; dialog.ShowDialog(); }); }
public SessionVMBase() { if (IsInDesignMode && AuthorityController.CurrentDoctor == null) { var doc = Nhib.GetSession().Query <Doctor>().FirstOrDefault(); AuthorityController.TryLogIn(doc); } _session = Nhib.GetSession(); handler = this.Subscribe(Event.NewSession, (e) => { var s = e.GetValue <ISession>(MessageKeys.Session); ReplaceSession(s); }); }
/// <summary> /// /// </summary> /// <param name="session">Для автокомплитов и категорий</param> /// <param name="onAutocompleteInputEnded"></param> /// <param name="options"></param> public QueryBlockViewModel(ISession session, Action onAutocompleteInputEnded, SearchOptions options = null) { Contract.Requires(session != null); this.session = session; this.onAutocompleteInputEnded = onAutocompleteInputEnded; _operator = QueryGroupOperator.All; _minAny = 1; CreateAutocompletes(); CreateMenuItems(); Children.CollectionChanged += Children_CollectionChanged; this.PropertyChanged += (s, e) => { switch (e.PropertyName) { case "IsSelected": case "IsGroup": case "IsExpanded": RefreshDescription(); OnPropertyChanged(() => DescriptionVisible); break; case "Options": if (!IsRoot) { Parent.RefreshDescription(); } break; } }; handler = this.Subscribe(Event.NewSession, (e) => { var s = e.GetValue <ISession>(MessageKeys.Session); ReplaceSession(s); }); if (options != null) { _options = options; FillFromOptions(options); } }
private static async Task MultipleMiddlewareHandlers(string endpoint, Func <TrafficRecorderMessageHandler, EventMessageHandler, IHttpClientBuilder> factory, IEnumerable <string> expectedVisitedMiddleware) { var actuallyVisitedMiddleware = new List <string>(); var trafficRecorderMessageHandler = new TrafficRecorderMessageHandler(actuallyVisitedMiddleware); var eventMessageHandler = new EventMessageHandler(actuallyVisitedMiddleware); var client = factory(trafficRecorderMessageHandler, eventMessageHandler).Build(); var raisedEvent = await Assert.RaisesAsync <EventMessageHandler.RequestEventArgs>( h => eventMessageHandler.Request += h, h => eventMessageHandler.Request -= h, () => client.GetAsync(endpoint)); Assert.True(raisedEvent.Arguments.Request.Headers.Contains("foobar")); Assert.Equal("foobar", raisedEvent.Arguments.Request.Headers.GetValues("foobar").FirstOrDefault()); Assert.Single(trafficRecorderMessageHandler.Traffic); Assert.Equal(HttpStatusCode.OK, trafficRecorderMessageHandler.Traffic[0].Item2.StatusCode); Assert.Equal(expectedVisitedMiddleware, actuallyVisitedMiddleware); }
public void Dispose(EventMessageHandler handler) { handler.Dispose(); Remove(handler); }
private void Remove(EventMessageHandler handler) { list.Remove(handler); }
public void Add(EventMessageHandler handler) { list.Add(handler); }
public EventMessageHandlersManager(EventMessageHandler handler) { Add(handler); }