public async Task AdaptiveClient_falls_back_to_MySQL_on_Try() { Mock <IAccountsService> fakeMSSQLAccountsService = new Mock <IAccountsService>(); fakeMSSQLAccountsService.Setup(x => x.GetAccountByID(It.Is <int>(i => i == 1))).ReturnsAsync(new Account { Name = DataBaseProviderName.MSSQL }); fakeMSSQLAccountsService.Setup(x => x.GetAccountByID(It.Is <int>(i => i == 2))).Throws(new Exception("boo")); Mock <IAccountsService> fakeMySQLAccountsService = new Mock <IAccountsService>(); fakeMySQLAccountsService.Setup(x => x.GetAccountByID(It.IsAny <int>())).ReturnsAsync(new Account { Name = DataBaseProviderName.MySQL }); using (var scope = Container.BeginLifetimeScope(builder => { builder.RegisterInstance(fakeMSSQLAccountsService.Object).Keyed <IAccountsService>(EndPointType.DBMS + DataBaseProviderName.MSSQL); builder.RegisterInstance(fakeMySQLAccountsService.Object).Keyed <IAccountsService>(EndPointType.DBMS + DataBaseProviderName.MySQL); })) { IAdaptiveClient <IBOServiceManifest> client = scope.Resolve <IAdaptiveClient <IBOServiceManifest> >(); Account account = await client.TryAsync(async x => await x.AccountsService.GetAccountByID(1)); Assert.AreEqual(DataBaseProviderName.MSSQL, account.Name); // We are passing 2 as an ID so the call to the MSSQL instance will throw. // We fall back to the MySQL instance which returns "MySQL" account = await client.TryAsync(async x => await x.AccountsService.GetAccountByID(2)); Assert.AreEqual(DataBaseProviderName.MySQL, account.Name); } }
public async Task Inner_exceptions_are_maintained_for_each_client_call_async() { // We catch and log the error if a client throws - however we don't propagate errors unless // we run out of endpoints to try. This test asserts that errors from each client are maintained // and propagated as inner exceptions when we exhaust all endpoints. 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); builder.RegisterInstance(inProcessClient).Keyed <IDummyAPI1>(EndPointType.InProcess + ProviderName.MySQL); Moq.Mock <IDummyAPI1> webAPIClientMock = new Mock <IDummyAPI1>(); webAPIClientMock.Setup(x => x.GetString()).Throws(new Exception("WebAPI Exception")); IDummyAPI1 webAPIClient = webAPIClientMock.Object; builder.RegisterInstance(webAPIClient).Keyed <IDummyAPI1>(EndPointType.HTTP + ProviderName.HTTP); IContainer container = builder.Build(); IAdaptiveClient <IDummyAPI1> client1 = container.Resolve <IAdaptiveClient <IDummyAPI1> >(); Exception ex = Assert.ThrowsAsync <Exception>(async() => await client1.TryAsync(async x => { await Task.Delay(0); x.GetString(); })); Assert.AreEqual(4, ((AggregateException)(ex.InnerException)).InnerExceptions.Count); // Assert that the error thrown by the client is maintained Assert.AreEqual("InProcess Exception", ((AggregateException)(ex.InnerException)).InnerExceptions[0].InnerException.Message); Assert.AreEqual("InProcess Exception", ((AggregateException)(ex.InnerException)).InnerExceptions[1].InnerException.Message); Assert.AreEqual("WebAPI Exception", ((AggregateException)(ex.InnerException)).InnerExceptions[2].InnerException.Message); Assert.AreEqual("WebAPI Exception", ((AggregateException)(ex.InnerException)).InnerExceptions[3].InnerException.Message); }
public async Task AdaptiveClient_Resolves_to_MSSQL_on_Try() { Mock <IAccountsService> fakeMSSQLAccountsService = new Mock <IAccountsService>(); fakeMSSQLAccountsService.Setup(x => x.GetAccountByID(It.IsAny <int>())).ReturnsAsync(new Account { Name = DataBaseProviderName.MSSQL }); Mock <IAccountsService> fakeMySQLAccountsService = new Mock <IAccountsService>(); fakeMySQLAccountsService.Setup(x => x.GetAccountByID(It.IsAny <int>())).ReturnsAsync(new Account { Name = DataBaseProviderName.MySQL }); using (var scope = Container.BeginLifetimeScope(builder => { builder.RegisterInstance(fakeMSSQLAccountsService.Object).Keyed <IAccountsService>(EndPointType.DBMS + DataBaseProviderName.MSSQL); builder.RegisterInstance(fakeMySQLAccountsService.Object).Keyed <IAccountsService>(EndPointType.DBMS + DataBaseProviderName.MySQL); })) { IAdaptiveClient <IBOServiceManifest> client = scope.Resolve <IAdaptiveClient <IBOServiceManifest> >(); Account account = await client.TryAsync(async x => await x.AccountsService.GetAccountByID(1)); Assert.AreEqual(DataBaseProviderName.MSSQL, account.Name); } }