public void PutOneLog() { var addedLog = TestLog.CreateTestLog(); _storage.PutLog(StorageTestChannelName, addedLog); var retrievedLogs = new List <Log>(); _storage.GetLogsAsync(StorageTestChannelName, 1, retrievedLogs).RunNotAsync(); var retrievedLog = retrievedLogs[0]; Assert.AreEqual(addedLog, retrievedLog); }
public async Task KnownExceptionIsThrownAsIs() { var mockStorageAdapter = Mock.Of <IStorageAdapter>(); using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter)) { var exception = new StorageException(); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.CountAsync(It.IsAny <Expression <Func <LogEntry, bool> > >())).Throws(exception); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.InsertAsync(It.IsAny <LogEntry>())).Throws(exception); try { await storage.PutLog(StorageTestChannelName, TestLog.CreateTestLog()); Assert.Fail("Should have thrown exception"); } catch (Exception e) { Assert.AreSame(exception, e); } try { await storage.CountLogsAsync(StorageTestChannelName); Assert.Fail("Should have thrown exception"); } catch (Exception e) { Assert.AreSame(exception, e); } } }
public void ShutdownSucceed() { var mockConnection = new Mock <IStorageAdapter>(); mockConnection.Setup( c => c.Insert(TableName, It.IsAny <string[]>(), It.IsAny <List <object[]> >())) .Callback(() => Task.Delay(TimeSpan.FromSeconds(2)).Wait()); var storage = new Microsoft.AppCenter.Storage.Storage(mockConnection.Object, It.IsAny <string>()); // Ignore warnings because we just want to "fire and forget" #pragma warning disable 4014 storage.PutLog(StorageTestChannelName, new TestLog()); storage.PutLog(StorageTestChannelName, new TestLog()); #pragma warning restore 4014 var result = storage.ShutdownAsync(TimeSpan.FromSeconds(100)).RunNotAsync(); Assert.IsTrue(result); }
public void ShutdownTimeout() { var mockConnection = new Mock <IStorageAdapter>(); mockConnection.Setup( c => c.InsertAsync(It.IsAny <Microsoft.AppCenter.Storage.Storage.LogEntry>())) .Callback(() => Task.Delay(TimeSpan.FromDays(1)).Wait()) .Returns(TaskExtension.GetCompletedTask(1)); var storage = new Microsoft.AppCenter.Storage.Storage(mockConnection.Object); // Ignore warnings because we just want to "fire and forget" #pragma warning disable 4014 storage.PutLog(StorageTestChannelName, new TestLog()); storage.PutLog(StorageTestChannelName, new TestLog()); #pragma warning restore 4014 var result = storage.ShutdownAsync(TimeSpan.FromTicks(1)).RunNotAsync(); Assert.IsFalse(result); }
public async Task DoNotRecreateCorruptedDatabaseOnNotCorruptException() { var mockStorageAdapter = Mock.Of<IStorageAdapter>(); using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter, _databasePath)) { var exception = new Exception("Something else"); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.Insert(It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<List<object[]>>())).Throws(exception); await Assert.ThrowsExceptionAsync<StorageException>(() => storage.PutLog(StorageTestChannelName, TestLog.CreateTestLog())); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.Dispose(), Times.Never()); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.Initialize(It.IsAny<string>()), Times.Once()); } }
public void UnknownExceptionIsConvertedToStorageException() { var mockStorageAdapter = Mock.Of<IStorageAdapter>(); using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter, _databasePath)) { var exception = new Exception(); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.Count(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<object>())).Throws(exception); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.Insert(It.IsAny<string>(), It.IsAny<string[]>(), It.IsAny<IList<object[]>>())).Throws(exception); Assert.ThrowsException<StorageException>(() => storage.PutLog(StorageTestChannelName, TestLog.CreateTestLog()).RunNotAsync()); Assert.ThrowsException<StorageException>(() => storage.CountLogsAsync(StorageTestChannelName).RunNotAsync()); } }
public async Task UnknownExceptionIsConvertedToStorageException() { var mockStorageAdapter = Mock.Of <IStorageAdapter>(); using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter)) { var exception = new Exception(); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.CountAsync(It.IsAny <Expression <Func <LogEntry, bool> > >())).Throws(exception); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.InsertAsync(It.IsAny <LogEntry>())).Throws(exception); await Assert.ThrowsExceptionAsync <StorageException>(() => storage.PutLog(StorageTestChannelName, TestLog.CreateTestLog())); await Assert.ThrowsExceptionAsync <StorageException>(() => storage.CountLogsAsync(StorageTestChannelName)); } }
public async Task DontRecreateCorruptedDatabaseOnNotCorruptException() { var mockStorageAdapter = Mock.Of <IStorageAdapter>(); using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter)) { var exception = new Exception("Something else"); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.InsertAsync(It.IsAny <LogEntry>())).Throws(exception); await Assert.ThrowsExceptionAsync <StorageException>(() => storage.PutLog(StorageTestChannelName, TestLog.CreateTestLog())); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.DeleteDatabaseFileAsync(), Times.Never()); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.InitializeStorageAsync(), Times.Once()); } }
public async Task RecreateCorruptedDatabaseOnInnerCorruptException() { var mockStorageAdapter = Mock.Of <IStorageAdapter>(); using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter)) { var exception = new StorageException(SQLiteException.New(SQLite3.Result.Corrupt, "Corrupt")); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.InsertAsync(It.IsAny <LogEntry>())).Throws(exception); await Assert.ThrowsExceptionAsync <StorageException>(() => storage.PutLog(StorageTestChannelName, TestLog.CreateTestLog())); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.DeleteDatabaseFileAsync()); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.InitializeStorageAsync(), Times.Exactly(2)); } }
public async Task RecreateCorruptedDatabaseOnInnerCorruptException() { var mockStorageAdapter = Mock.Of<IStorageAdapter>(); using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter, _databasePath)) { var exception = new StorageCorruptedException("Mock exception"); var testLog = TestLog.CreateTestLog(); var testLogString = LogSerializer.Serialize(testLog); var testLogSize = Encoding.UTF8.GetBytes(testLogString).Length; Mock.Get(mockStorageAdapter).Setup(adapter => adapter.GetMaxStorageSize()).Returns(testLogSize + 1); Mock.Get(mockStorageAdapter).Setup(adapter => adapter.Insert(TableName, It.IsAny<string[]>(), It.IsAny<List<object[]>>())).Throws(exception); await Assert.ThrowsExceptionAsync<StorageCorruptedException>(() => storage.PutLog(StorageTestChannelName, testLog)); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.Dispose()); Mock.Get(mockStorageAdapter).Verify(adapter => adapter.Initialize(It.IsAny<string>()), Times.Exactly(2)); } }
public void StorageThrowsStorageException() { var mockAdapter = new Mock <IStorageAdapter>(); mockAdapter.Setup( a => a.GetAsync(It.IsAny <PredType>(), It.IsAny <int>())) .Returns(TaskExtension.GetFaultedTask <List <Microsoft.AppCenter.Storage.Storage.LogEntry> >(new StorageException())); mockAdapter.Setup(c => c.InsertAsync(It.IsAny <Microsoft.AppCenter.Storage.Storage.LogEntry>())) .Returns(TaskExtension.GetFaultedTask <int>(new StorageException())); mockAdapter.Setup(c => c.DeleteAsync(It.IsAny <Expression <Func <Microsoft.AppCenter.Storage.Storage.LogEntry, bool> > >())) .Returns(TaskExtension.GetFaultedTask <int>(new StorageException())); mockAdapter.Setup(c => c.CountAsync(It.IsAny <Expression <Func <Microsoft.AppCenter.Storage.Storage.LogEntry, bool> > >())) .Returns(TaskExtension.GetFaultedTask <int>(new StorageException())); var fakeStorage = new Microsoft.AppCenter.Storage.Storage(mockAdapter.Object); Assert.ThrowsException <StorageException>(() => fakeStorage.PutLog("channel_name", new TestLog()).RunNotAsync()); Assert.ThrowsException <StorageException>(() => fakeStorage.DeleteLogs("channel_name", string.Empty).RunNotAsync()); Assert.ThrowsException <StorageException>(() => fakeStorage.CountLogsAsync("channel_name").RunNotAsync()); Assert.ThrowsException <StorageException>(() => fakeStorage.GetLogsAsync("channel_name", 1, new List <Log>()).RunNotAsync()); }
public void StorageThrowsStorageException() { var mockAdapter = new Mock <IStorageAdapter>(); mockAdapter.Setup( a => a.Select(TableName, It.IsAny <string>(), It.IsAny <object>(), It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <int?>(), It.IsAny <string[]>())) .Throws(new StorageException()); mockAdapter.Setup(c => c.Insert(TableName, It.IsAny <string[]>(), It.IsAny <IList <object[]> >())) .Throws(new StorageException()); mockAdapter.Setup(c => c.Delete(TableName, It.IsAny <string>())) .Throws(new StorageException()); mockAdapter.Setup(c => c.Count(TableName, It.IsAny <string>(), It.IsAny <object>())) .Throws(new StorageException()); var fakeStorage = new Microsoft.AppCenter.Storage.Storage(mockAdapter.Object, It.IsAny <string>()); Assert.ThrowsException <StorageException>(() => fakeStorage.PutLog(StorageTestChannelName, new TestLog()).RunNotAsync()); Assert.ThrowsException <StorageException>(() => fakeStorage.DeleteLogs(StorageTestChannelName, string.Empty).RunNotAsync()); Assert.ThrowsException <StorageException>(() => fakeStorage.CountLogsAsync(StorageTestChannelName).RunNotAsync()); Assert.ThrowsException <StorageException>(() => fakeStorage.GetLogsAsync(StorageTestChannelName, 1, new List <Log>()).RunNotAsync()); }