internal override void Delete(StorageTransmission item) { try { if (this.StorageFolder == null) { return; } // Initial storage size calculation. CalculateSize(); long fileSize = GetSize(item.FileName); File.Delete(Path.Combine(StorageFolder, item.FileName)); this.deletedFilesQueue.Enqueue(item.FileName); // calculate size Interlocked.Add(ref this.storageSize, -fileSize); Interlocked.Decrement(ref this.storageCountFiles); } catch (IOException e) { string msg = string.Format(CultureInfo.InvariantCulture, "Failed to delete a file. file: {0} Exception: {1}", item == null ? "null" : item.FullFilePath, e); CoreEventSource.Log.LogVerbose(msg); } }
/// <summary> /// Reads an item from the storage. Order is Last-In-First-Out. /// When the Transmission is no longer needed (it was either sent or failed with a non-retriable error) it should be disposed. /// </summary> internal override StorageTransmission Peek() { var files = this.GetFiles("*.trn", top: 50); lock (this.peekLockObj) { foreach (var file in files) { try { // if a file was peeked before, skip it (wait until it is disposed). if (this.peekedTransmissions.ContainsKey(file) == false && this.deletedFilesQueue.Contains(file) == false) { // Load the transmission from disk. StorageTransmission storageTransmissionItem = LoadTransmissionFromFileAsync(file).ConfigureAwait(false).GetAwaiter().GetResult(); // when item is disposed it should be removed from the peeked list. storageTransmissionItem.Disposing = item => this.OnPeekedItemDisposed(file); // add the transmission to the list. this.peekedTransmissions.Add(file, storageTransmissionItem.FullFilePath); return(storageTransmissionItem); } } catch (Exception e) { string msg = string.Format(CultureInfo.InvariantCulture, "Failed to load an item from the storage. file: {0} Exception: {1}", file, e); CoreEventSource.Log.LogVerbose(msg); } } } return(null); }
public void WhenStorageHasTwoItemsThenTwoCallsToPeekReturns2DifferentItems() { // Setup - create a storage with 2 items StorageService storage = new StorageService(); storage.Init(GetTemporaryPath()); Transmission firstTransmission = CreateTransmissionAndEnqueueIt(storage); Transmission secondTransmission = CreateTransmissionAndEnqueueIt(storage); // Act StorageTransmission firstPeekedTransmission = storage.Peek(); StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts firstPeekedTransmission.Should().NotBeNull(); secondPeekedTransmission.Should().NotBeNull(); string first = Encoding.UTF8.GetString(firstPeekedTransmission.Content, 0, firstPeekedTransmission.Content.Length); string second = Encoding.UTF8.GetString(secondPeekedTransmission.Content, 0, secondPeekedTransmission.Content.Length); first.Should().NotBe(second); }
private static async Task SaveTransmissionToFileAsync(Transmission transmission, string fileFullName) { try { using (Stream stream = File.OpenWrite(fileFullName)) { await StorageTransmission.SaveAsync(transmission, stream).ConfigureAwait(false); } } catch (UnauthorizedAccessException) { string message = string.Format("Failed to save transmission to file. UnauthorizedAccessException. File full path: {0}", fileFullName); CoreEventSource.Log.LogVerbose(message); throw; } }
private static async Task <StorageTransmission> LoadTransmissionFromFileAsync(FileInfo file) { try { using (Stream stream = file.OpenRead()) { StorageTransmission storageTransmissionItem = await StorageTransmission.CreateFromStreamAsync(stream, file.FullName).ConfigureAwait(false); return(storageTransmissionItem); } } catch (Exception e) { string message = string.Format("Failed to load transmission from file. File full path: {0}, Exception: {1}", file.FullName, e); CoreEventSource.Log.LogVerbose(message); throw; } }
public void PeekedItemIsNOnlyotReturnedOnce() { // Setup - create a storage with one item Storage storage = new Storage("unittest" + Guid.NewGuid().ToString()); Transmission transmissionToEnqueue = CreateTransmissionAndEnqueueIt(storage); // Act StorageTransmission firstPeekedTransmission = storage.Peek(); StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts Assert.IsNotNull(firstPeekedTransmission); Assert.IsNull(secondPeekedTransmission); }
public void EnqueuedContentIsEqualToPeekedContent() { // Setup Storage storage = new Storage("unittest" + Guid.NewGuid().ToString()); Transmission transmissionToEnqueue = CreateTransmission(new TraceTelemetry("mock_item")); // Act storage.EnqueueAsync(transmissionToEnqueue).ConfigureAwait(false).GetAwaiter().GetResult(); StorageTransmission peekedTransmission = storage.Peek(); // Asserts string enqueuedContent = Encoding.UTF8.GetString(transmissionToEnqueue.Content, 0, transmissionToEnqueue.Content.Length); string peekedContent = Encoding.UTF8.GetString(peekedTransmission.Content, 0, peekedTransmission.Content.Length); Assert.AreEqual(peekedContent, enqueuedContent); }
public void PeekedItemIsOnlyReturnedOnce() { // Setup - create a storage with one item StorageService storage = new StorageService(); storage.Init(GetTemporaryPath()); Transmission transmissionToEnqueue = CreateTransmissionAndEnqueueIt(storage); // Act StorageTransmission firstPeekedTransmission = storage.Peek(); StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts firstPeekedTransmission.Should().NotBeNull(); secondPeekedTransmission.Should().BeNull(); }
internal override void Delete(StorageTransmission item) { if (this.StorageFolder == null) { return; } try { File.Delete(item.FullFilePath); this.deletedFilesQueue.Enqueue(item.FileName); } catch (IOException e) { string msg = string.Format(CultureInfo.InvariantCulture, "Failed to delete a file. file: {0} Exception: {1}", item == null ? "null" : item.FullFilePath, e); CoreEventSource.Log.LogVerbose(msg); } catch (UnauthorizedAccessException e) { string msg = string.Format(CultureInfo.InvariantCulture, "Failed to delete a file. file: {0} Exception: {1}", item == null ? "null" : item.FullFilePath, e); CoreEventSource.Log.LogVerbose(msg); } }
private async Task <StorageTransmission> LoadTransmissionFromFileAsync(string file) { try { using (Stream stream = File.OpenRead(Path.Combine(StorageFolder, file))) { StorageTransmission storageTransmissionItem = await StorageTransmission.CreateFromStreamAsync(stream, file).ConfigureAwait(false); return(storageTransmissionItem); } } catch (Exception e) { string message = string.Format("Failed to load transmission from file. File path: {0}, FileName: {1}, Exception: {2}", "storageFolderName", file, e); CoreEventSource.Log.LogVerbose(message); throw; } }
public void WhenStorageHasTwoItemsThenTwoCallsToPeekReturns2DifferentItems() { // Setup - create a storage with 2 items Storage storage = new Storage("unittest" + Guid.NewGuid().ToString()); Transmission firstTransmission = CreateTransmissionAndEnqueueIt(storage); Transmission secondTransmission = CreateTransmissionAndEnqueueIt(storage); // Act StorageTransmission firstPeekedTransmission = storage.Peek(); StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts Assert.IsNotNull(firstPeekedTransmission); Assert.IsNotNull(secondPeekedTransmission); string first = Encoding.UTF8.GetString(firstPeekedTransmission.Content, 0, firstPeekedTransmission.Content.Length); string second = Encoding.UTF8.GetString(secondPeekedTransmission.Content, 0, secondPeekedTransmission.Content.Length); Assert.AreNotEqual(first, second); }
public void EnqueuedContentIsEqualToPeekedContent() { // Setup StorageService storage = new StorageService(); storage.Init(GetTemporaryPath()); Transmission transmissionToEnqueue = CreateTransmission(new TraceTelemetry("mock_item")); // Act storage.EnqueueAsync(transmissionToEnqueue).ConfigureAwait(false).GetAwaiter().GetResult(); StorageTransmission peekedTransmission = storage.Peek(); // Asserts string enqueuedContent = Encoding.UTF8.GetString(transmissionToEnqueue.Content, 0, transmissionToEnqueue.Content.Length); string peekedContent = Encoding.UTF8.GetString(peekedTransmission.Content, 0, peekedTransmission.Content.Length); enqueuedContent.Should().Be(peekedContent); }
public void PeekedItemIsReturnedAgainAfterTheItemInTheFirstCallToPeekIsDisposed() { // Setup - create a storage with one item Storage storage = new Storage("unittest" + Guid.NewGuid().ToString()); Transmission transmissionToEnqueue = CreateTransmission(new TraceTelemetry("mock_item")); storage.EnqueueAsync(transmissionToEnqueue).ConfigureAwait(false).GetAwaiter().GetResult(); // Act StorageTransmission firstPeekedTransmission; using (firstPeekedTransmission = storage.Peek()) { } StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts Assert.IsNotNull(firstPeekedTransmission); Assert.IsNotNull(secondPeekedTransmission); }
public void DeletedItemIsNotReturnedInCallsToPeek() { // Setup - create a storage with one item Storage storage = new Storage("unittest" + Guid.NewGuid().ToString()); Transmission transmissionToEnqueue = CreateTransmissionAndEnqueueIt(storage); // Act StorageTransmission firstPeekedTransmission; // if item is not disposed,peek will not return it (regardless of the call to delete). // So for this test to actually test something, using 'using' is required. using (firstPeekedTransmission = storage.Peek()) { storage.Delete(firstPeekedTransmission); } StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts Assert.IsNotNull(firstPeekedTransmission); Assert.IsNull(secondPeekedTransmission); }
public void DeletedItemIsNotReturnedInCallsToPeek() { // Setup - create a storage with one item StorageService storage = new StorageService(); storage.Init(GetTemporaryPath()); Transmission transmissionToEnqueue = CreateTransmissionAndEnqueueIt(storage); // Act StorageTransmission firstPeekedTransmission; // if item is not disposed,peek will not return it (regardless of the call to delete). // So for this test to actually test something, using 'using' is required. using (firstPeekedTransmission = storage.Peek()) { storage.Delete(firstPeekedTransmission); } StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts firstPeekedTransmission.Should().NotBeNull(); secondPeekedTransmission.Should().BeNull(); }
public void PeekedItemIsReturnedAgainAfterTheItemInTheFirstCallToPeekIsDisposed() { // Setup - create a storage with one item StorageService storage = new StorageService(); storage.Init(GetTemporaryPath()); Transmission transmissionToEnqueue = CreateTransmission(new TraceTelemetry("mock_item")); storage.EnqueueAsync(transmissionToEnqueue).ConfigureAwait(false).GetAwaiter().GetResult(); // Act StorageTransmission firstPeekedTransmission; using (firstPeekedTransmission = storage.Peek()) { } StorageTransmission secondPeekedTransmission = storage.Peek(); // Asserts firstPeekedTransmission.Should().NotBeNull(); secondPeekedTransmission.Should().NotBeNull(); }
internal abstract void Delete(StorageTransmission transmission);
protected override bool Send(StorageTransmission transmission, ref TimeSpan nextSendInterval) { OnSend(nextSendInterval); DelayHandler.Set(); return(base.Send(transmission, ref nextSendInterval)); }
internal override void Delete(StorageTransmission item) { try { if (this.StorageFolder == null) { return; } // Initial storage size calculation. this.EnsureSizeIsCalculatedAsync().ConfigureAwait(false).GetAwaiter().GetResult(); IStorageFile file = this.StorageFolder.GetFileAsync(item.FileName).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); long fileSize = this.GetSizeAsync(file).ConfigureAwait(false).GetAwaiter().GetResult(); file.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); this.deletedFilesQueue.Enqueue(item.FileName); // calculate size Interlocked.Add(ref this.storageSize, -fileSize); Interlocked.Decrement(ref this.storageCountFiles); } catch (IOException e) { string msg = string.Format(CultureInfo.InvariantCulture, "Failed to delete a file. file: {0} Exception: {1}", item == null ? "null" : item.FullFilePath, e); CoreEventSource.Log.LogVerbose(msg); } }
internal override void Delete(StorageTransmission transmission) { storage.Remove(transmission); }