Пример #1
0
        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);
        }
Пример #2
0
        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);
                }
            }
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
 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());
     }
 }
Пример #6
0
 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());
     }
 }
Пример #7
0
        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));
            }
        }
Пример #8
0
        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());
            }
        }
Пример #9
0
        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));
            }
        }
Пример #10
0
 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));
     }
 }
Пример #11
0
        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());
        }
Пример #12
0
        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());
        }