コード例 #1
0
        public async Task SaveLogPurgesOldestLogsWhenStorageIsFull()
        {
            // Set storage max size.
            var capacity = 12 * 1024;
            var setMaxStorageSizeResult = await _storage.SetMaxStorageSizeAsync(capacity);

            Assert.IsTrue(setMaxStorageSizeResult);

            // Fill databse with logs.
            await FillDatabaseWithLogs(capacity);

            // Get first log
            var initialLogs = new List <Log>();
            await _storage.GetLogsAsync(StorageTestChannelName, int.MaxValue, initialLogs);

            var firstLog  = initialLogs[0];
            var secondLog = initialLogs[1];

            // Create a new log. Verify that new log was added and old was deleted.
            var newLog = TestLog.CreateTestLog();
            await _storage.PutLog(StorageTestChannelName, newLog);

            var retrievedLogs = new List <Log>();
            await _storage.ClearPendingLogState(StorageTestChannelName);

            await _storage.GetLogsAsync(StorageTestChannelName, int.MaxValue, retrievedLogs);

            CollectionAssert.Contains(retrievedLogs, newLog);
            CollectionAssert.Contains(retrievedLogs, secondLog);
            CollectionAssert.DoesNotContain(retrievedLogs, firstLog);
        }
コード例 #2
0
 public void GetLogsFromChannelWithSimilarNames()
 {
     var fakeChannelName = StorageTestChannelName.Substring(0, StorageTestChannelName.Length - 1);
     _storage.PutLog(StorageTestChannelName, TestLog.CreateTestLog()).Wait();
     var retrievedLogs = new List<Log>();
     var batchId = _storage.GetLogsAsync(fakeChannelName, 1, retrievedLogs).RunNotAsync();
     Assert.IsNull(batchId);
 }
コード例 #3
0
 public void PutOneLog()
 {
     var addedLog = TestLog.CreateTestLog();
     _storage.PutLog(StorageTestChannelName, addedLog).Wait();
     var retrievedLogs = new List<Log>();
     _storage.GetLogsAsync(StorageTestChannelName, 1, retrievedLogs).RunNotAsync();
     var retrievedLog = retrievedLogs[0];
     Assert.AreEqual(addedLog, retrievedLog);
 }
コード例 #4
0
        private async Task FillDatabaseWithLogs(int storageCapacity)
        {
            var log           = TestLog.CreateTestLog();
            var logJsonString = LogSerializer.Serialize(log);
            var logSize       = Encoding.UTF8.GetBytes(logJsonString).Length;
            var logsCount     = storageCapacity / logSize;
            var logs          = PutNLogs(logsCount - 1);

            logs.Add(log);
            await _storage.PutLog(StorageTestChannelName, log);
        }
コード例 #5
0
        private TestLog CreateLogWithSize(int size)
        {
            var testLog = TestLog.CreateTestLog();

            // There is a limitation for string size, so we tear into pieces.
            var propsNum = size / 1000 + 1; 
            for (var i = 0; i < propsNum; i++)
            {
                testLog.Properties.Add($"largeProp{i}", new string('.', size));
            }
            return testLog;
        }
コード例 #6
0
 private List<TestLog> PutNLogs(int n)
 {
     var putLogTasks = new Task[n];
     var addedLogs = new List<TestLog>();
     for (var i = 0; i < n; ++i)
     {
         var testLog = TestLog.CreateTestLog();
         addedLogs.Add(testLog);
         putLogTasks[i] = _storage.PutLog(StorageTestChannelName, testLog);
     }
     return addedLogs;
 }
コード例 #7
0
 public void KnownExceptionIsThrownAsIs()
 {
     var mockStorageAdapter = Mock.Of<IStorageAdapter>();
     var exception = new StorageException();
     using (var storage = new Microsoft.AppCenter.Storage.Storage(mockStorageAdapter, _databasePath))
     {
         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());
     }
 }
コード例 #8
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));
     }
 }
コード例 #9
0
        public async Task SaveLogDoesntPurgesOldLogsWhenStorageIsFullOfDifferentKindOfLogs()
        {
            // Prepare constants.
            var anotherChannelName = "anotherChannelName";

            // Set storage max size.
            var capacity = 12 * 1024;
            var setMaxStorageSizeResult = await _storage.SetMaxStorageSizeAsync(capacity);

            Assert.IsTrue(setMaxStorageSizeResult);

            // Fill databse with logs.
            await FillDatabaseWithLogs(capacity);

            // Get stored collection.
            var initialLogs = new List <Log>();
            await _storage.GetLogsAsync(StorageTestChannelName, int.MaxValue, initialLogs);

            // Try to add a new log with another channel name to storage.
            // Verify that StorageException is thrown.
            var newLog = TestLog.CreateTestLog();

            try
            {
                await _storage.PutLog(anotherChannelName, newLog);

                Assert.Fail("It should be failed.");
            }
            catch (StorageException e)
            {
                Assert.AreEqual(e.Message, "Failed to add a new log. Storage is full and old logs cannot be purged.");
            }

            // Verify that existing logs were not damaged.
            var retrievedLogs = new List <Log>();
            await _storage.ClearPendingLogState(StorageTestChannelName);

            await _storage.GetLogsAsync(StorageTestChannelName, int.MaxValue, retrievedLogs);

            CollectionAssert.AreEquivalent(retrievedLogs, initialLogs);

            // Verify that new log was not added.
            await _storage.GetLogsAsync(anotherChannelName, int.MaxValue, retrievedLogs);

            Assert.AreEqual(0, retrievedLogs.Count);
        }
コード例 #10
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());
            }
        }