public async Task CurrentEndPoint_is_thread_safe() { Moq.Mock <IEndPointValidator> InProcess_Validator_Mock = new Mock <IEndPointValidator>(); InProcess_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Returns(true); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MSSQL); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); IAdaptiveClient <IDummyAPI2> client2 = container.Resolve <IAdaptiveClient <IDummyAPI2> >(); string result1 = client1.Call(x => x.GetString()); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient1", result1); int result2 = client2.Call(x => x.GetInt()); Assert.AreEqual("Application_SQL2", client2.CurrentEndPoint.Name); Assert.AreEqual(1, result2); Task t1 = Task.Run(() => { for (int i = 0; i < 1000; i++) { string r1 = client1.Call(x => x.GetString()); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient1", r1); int r2 = client2.Call(x => x.GetInt()); Assert.AreEqual("Application_SQL2", client2.CurrentEndPoint.Name); Assert.AreEqual(1, r2); } }); Task t2 = Task.Run(() => { for (int i = 0; i < 1000; i++) { int r2 = client2.Call(x => x.GetInt()); Assert.AreEqual("Application_SQL2", client2.CurrentEndPoint.Name); Assert.AreEqual(1, r2); string r1 = client1.Call(x => x.GetString()); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient1", r1); } }); try { await Task.WhenAll(t1, t2); } catch (Exception) { throw; } }
public void EndPoint_is_validated_once_only_when_override_endpoint_is_passed() { int inProcessCalls = 0; int webAPICalls = 0; Moq.Mock <IEndPointValidator> InProcess_Validator_Mock = new Mock <IEndPointValidator>(); InProcess_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Callback(() => inProcessCalls++).Returns(true); Moq.Mock <IEndPointValidator> HTTP_Validator_Mock = new Mock <IEndPointValidator>(); HTTP_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Callback(() => webAPICalls++).Returns(true); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MSSQL); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MySQL); builder.RegisterInstance(HTTP_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.HTTP + ProviderName.HTTP); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); string result = client1.Call(x => x.GetString(), "Application_SQL1"); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient1", result); Assert.AreEqual(1, inProcessCalls); Assert.AreEqual(0, webAPICalls); // do it again and use the webpai endpoint: client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); result = client1.Call(x => x.GetString(), "Application_WebAPI1"); Assert.AreEqual("Application_WebAPI1", client1.CurrentEndPoint.Name); Assert.AreEqual("WebAPIClient1", result); Assert.AreEqual(1, inProcessCalls); // validate only once Assert.AreEqual(1, webAPICalls); // do it again and use the inprocess endpoint: result = client1.Call(x => x.GetString(), "Application_SQL1"); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient1", result); Assert.AreEqual(1, inProcessCalls); Assert.AreEqual(1, webAPICalls); // do it again and use the webpai endpoint: client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); result = client1.Call(x => x.GetString(), "Application_WebAPI1"); Assert.AreEqual("Application_WebAPI1", client1.CurrentEndPoint.Name); Assert.AreEqual("WebAPIClient1", result); Assert.AreEqual(1, inProcessCalls); Assert.AreEqual(1, webAPICalls); }
public void Client_exception_is_propagated() { Moq.Mock <IEndPointValidator> InProcess_Validator_Mock = new Mock <IEndPointValidator>(); InProcess_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Returns(true); Moq.Mock <IEndPointValidator> HTTP_Validator_Mock = new Mock <IEndPointValidator>(); HTTP_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Returns(false); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MSSQL); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MySQL); builder.RegisterInstance(HTTP_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.HTTP + ProviderName.HTTP); Moq.Mock <IDummyAPI1> inProcessClientMock = new Mock <IDummyAPI1>(); inProcessClientMock.Setup(x => x.GetString()).Throws(new Exception("InProcess Exception")); IDummyAPI1 inProcessClient = inProcessClientMock.Object; builder.RegisterInstance(inProcessClient).Keyed <IDummyAPI1>(EndPointType.InProcess + ProviderName.MSSQL); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); Exception ex = Assert.Throws <Exception>(() => client1.Call(x => x.GetString())); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcess Exception", ex.Message); }
public void Uses_cached_endpoint_on_second_call() { int inProcessCalls = 0; int webAPICalls = 0; Moq.Mock <IEndPointValidator> InProcess_Validator_Mock = new Mock <IEndPointValidator>(); InProcess_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Callback(() => inProcessCalls++).Returns(false); Moq.Mock <IEndPointValidator> HTTP_Validator_Mock = new Mock <IEndPointValidator>(); HTTP_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Callback(() => webAPICalls++).Returns(true); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MSSQL); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MySQL); builder.RegisterInstance(HTTP_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.HTTP + ProviderName.HTTP); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); string result = client1.Call(x => x.GetString()); Assert.AreEqual("Application_WebAPI1", client1.CurrentEndPoint.Name); Assert.AreEqual("WebAPIClient1", result); Assert.AreEqual(2, inProcessCalls); Assert.AreEqual(1, webAPICalls); // do it again and use the cached endpoint: IAdaptiveClient <IDummyAPI1> client2 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); string result2 = client2.Call(x => x.GetString()); Assert.AreEqual("Application_WebAPI1", client2.CurrentEndPoint.Name); Assert.AreEqual("WebAPIClient1", result2); Assert.AreEqual(2, inProcessCalls); // We should not test the in process endpoint again - we go directly to the cached HTTP endpoint. Assert.AreEqual(1, webAPICalls); }
public void Uses_cached_endpoint_on_second_call() { int inProcessCalls = 0; int webAPICalls = 0; // NetworkUtilities Mock Moq.Mock <INetworkUtilities> networkUtilMock = new Mock <INetworkUtilities>(); networkUtilMock.Setup(x => x.VerifyDBServerConnectivity(Moq.It.IsAny <string>())).Callback(() => inProcessCalls++).Returns(false); networkUtilMock.Setup(x => x.VerifyHttpServerAvailability(Moq.It.IsAny <string>())).Callback(() => webAPICalls++).Returns(true); INetworkUtilities networkUtil = networkUtilMock.Object; builder.RegisterInstance(networkUtil).As <INetworkUtilities>(); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); string result = client1.Call(x => x.GetString()); Assert.AreEqual("Application_WebAPI1", client1.CurrentEndPoint.Name); Assert.AreEqual("WebAPIClient1", result); Assert.AreEqual(3, inProcessCalls); Assert.AreEqual(1, webAPICalls); // do it again and use the cached endpoint: IAdaptiveClient <IDummyAPI1> client2 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); string result2 = client2.Call(x => x.GetString()); Assert.AreEqual("Application_WebAPI1", client2.CurrentEndPoint.Name); Assert.AreEqual("WebAPIClient1", result2); Assert.AreEqual(3, inProcessCalls); // We should not test the in process endpoint again - we go directly to the cached HTTP endpoint. Assert.AreEqual(1, webAPICalls); }
/// <summary> /// Initializes each board, if any exist, with tasks and sets the current board /// to the first. /// </summary> private void InitializeBoards() { BoardList = new ObservableCollection <BoardViewModel>(); List <BoardDTO> boardDTOs = dataProvider.Call(x => x.BoardServices.GetBoards()); foreach (BoardDTO dto in boardDTOs) { PresentationBoard presBoard = new PresentationBoard(dto); // Fill board with tasks if (dto.Tasks?.Any() ?? false) { foreach (TaskDTO taskDTO in dto.Tasks.OrderBy(x => x.ColumnIndex)) { presBoard.Tasks.Add(new PresentationTask(taskDTO)); // Fill TagsCollection on Board for AutoSuggestBox foreach (var tag in taskDTO.Tags.Split(',')) { if (!string.IsNullOrEmpty(tag) && !presBoard.TagsCollection.Contains(tag)) { presBoard.TagsCollection.Add(tag); } } } } BoardList.Add(boardViewModelFactory(presBoard, _appNotificationService)); } CurrentBoard = BoardList.Any() ? BoardList.First() : null; }
public void Reslove_InProcessClient3_of_type_IDummyAPI1_when_EndPaoint_name_is_passed() { Moq.Mock <INetworkUtilities> networkUtilMock = new Mock <INetworkUtilities>(); networkUtilMock.Setup(x => x.VerifyDBServerConnectivity(Moq.It.IsAny <string>())).Returns(true); networkUtilMock.Setup(x => x.VerifyHttpServerAvailability(Moq.It.IsAny <string>())).Returns(false); INetworkUtilities networkUtil = networkUtilMock.Object; builder.RegisterInstance(networkUtil).As <INetworkUtilities>(); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); string result = client1.Call(x => x.GetString()); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient1", result); string result2 = client1.Call(x => x.GetString(), "Application_MySQL1"); Assert.AreEqual("Application_MySQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient3", result2); }
public void Reslove_InProcessClient3_of_type_IDummyAPI1_when_EndPoint_name_is_passed() { Moq.Mock <IEndPointValidator> InProcess_Validator_Mock = new Mock <IEndPointValidator>(); InProcess_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Returns(true); Moq.Mock <IEndPointValidator> HTTP_Validator_Mock = new Mock <IEndPointValidator>(); HTTP_Validator_Mock.Setup(x => x.IsInterfaceAlive(Moq.It.IsAny <IEndPointConfiguration>())).Returns(false); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MSSQL); builder.RegisterInstance(InProcess_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.InProcess + ProviderName.MySQL); builder.RegisterInstance(HTTP_Validator_Mock.Object).Keyed <IEndPointValidator>(EndPointType.HTTP + ProviderName.HTTP); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); string result = client1.Call(x => x.GetString()); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient1", result); string result2 = client1.Call(x => x.GetString(), "Application_MySQL1"); Assert.AreEqual("Application_MySQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcessClient3", result2); }
private void GetUser() { IEndPointConfiguration endPoint = EndPoints.First(x => x.Name == SelectedEndPointName); Logger.Message = null; if (SelectedEndPointName == "Prod_WCF_01") { // This end point was registered with mocks so it will fail and fall back to Prod_MSSQL_01 // If the current endpoint is Prod_MSSQL_01 you won't see an error message because AdaptiveClient // will continue to use that endpoint. DemoUser = client.Try(usersService => usersService.GetUserByID(1), endPoint.Name, "Prod_MSSQL_01"); } else { DemoUser = client.Call(usersService => usersService.GetUserByID(1), endPoint.Name); } }
public void Client_exception_is_propagated() { Moq.Mock <INetworkUtilities> networkUtilMock = new Mock <INetworkUtilities>(); networkUtilMock.Setup(x => x.VerifyDBServerConnectivity(Moq.It.IsAny <string>())).Returns(true); networkUtilMock.Setup(x => x.VerifyHttpServerAvailability(Moq.It.IsAny <string>())).Returns(false); INetworkUtilities networkUtil = networkUtilMock.Object; builder.RegisterInstance(networkUtil).As <INetworkUtilities>(); Moq.Mock <IDummyAPI1> inProcessClientMock = new Mock <IDummyAPI1>(); inProcessClientMock.Setup(x => x.GetString()).Throws(new Exception("InProcess Exception")); IDummyAPI1 inProcessClient = inProcessClientMock.Object; builder.RegisterInstance(inProcessClient).Keyed <IDummyAPI1>(EndPointType.InProcess + ProviderName.MSSQL); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); Exception ex = Assert.Throws <Exception>(() => client1.Call(x => x.GetString())); Assert.AreEqual("Application_SQL1", client1.CurrentEndPoint.Name); Assert.AreEqual("InProcess Exception", ex.Message); }
public void Single_DbContext_instance_is_injected_into_all_services() { IAdaptiveClient <ISFServiceManifest> client = Container.Resolve <IAdaptiveClient <ISFServiceManifest> >(); Assert.IsTrue(client.Call(x => x.OrdersService.AreDbContextsEqual())); }