public async Task ReplaceLatestIndexedAsync( PopularityTransferData newData, IAccessCondition accessCondition) { using (_telemetryService.TrackReplaceLatestIndexedPopularityTransfers(newData.Count)) { var blobName = GetLatestIndexedBlobName(); _logger.LogInformation("Replacing the latest indexed popularity transfers from {BlobName}.", blobName); var mappedAccessCondition = new AccessCondition { IfNoneMatchETag = accessCondition.IfNoneMatchETag, IfMatchETag = accessCondition.IfMatchETag, }; var blobReference = Container.GetBlobReference(blobName); using (var stream = await blobReference.OpenWriteAsync(mappedAccessCondition)) using (var streamWriter = new StreamWriter(stream)) using (var jsonTextWriter = new JsonTextWriter(streamWriter)) { blobReference.Properties.ContentType = "application/json"; Serializer.Serialize(jsonTextWriter, newData); } } }
public async Task SetsContentType() { var newData = new PopularityTransferData(); await Target.ReplaceLatestIndexedAsync(newData, AccessCondition.Object); Assert.Equal("application/json", CloudBlob.Object.Properties.ContentType); }
public async Task SerializesWithoutBOM() { var newData = new PopularityTransferData(); await Target.ReplaceLatestIndexedAsync(newData, AccessCondition.Object); var bytes = Assert.Single(SavedBytes); Assert.Equal((byte)'{', bytes[0]); }
public async Task SerializedWithoutIndentation() { var newData = new PopularityTransferData(); newData.AddTransfer("PackageA", "packageB"); newData.AddTransfer("PackageA", "packageC"); await Target.ReplaceLatestIndexedAsync(newData, AccessCondition.Object); var json = Assert.Single(SavedStrings); Assert.DoesNotContain("\n", json); }
public async Task <AuxiliaryFileResult <PopularityTransferData> > ReadLatestIndexedAsync( IAccessCondition accessCondition, StringCache stringCache) { var stopwatch = Stopwatch.StartNew(); var blobName = GetLatestIndexedBlobName(); var blobReference = Container.GetBlobReference(blobName); _logger.LogInformation("Reading the latest indexed popularity transfers from {BlobName}.", blobName); bool modified; var data = new PopularityTransferData(); AuxiliaryFileMetadata metadata; try { using (var stream = await blobReference.OpenReadAsync(accessCondition)) { ReadStream(stream, (from, to) => data.AddTransfer(stringCache.Dedupe(from), stringCache.Dedupe(to))); modified = true; metadata = new AuxiliaryFileMetadata( lastModified: new DateTimeOffset(blobReference.LastModifiedUtc, TimeSpan.Zero), loadDuration: stopwatch.Elapsed, fileSize: blobReference.Properties.Length, etag: blobReference.ETag); } } catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotModified) { _logger.LogInformation("The blob {BlobName} has not changed.", blobName); modified = false; data = null; metadata = null; } stopwatch.Stop(); _telemetryService.TrackReadLatestIndexedPopularityTransfers(data?.Count, modified, stopwatch.Elapsed); return(new AuxiliaryFileResult <PopularityTransferData>( modified, data, metadata)); }
public async Task SerializesVersionsSortedOrder() { var newData = new PopularityTransferData(); newData.AddTransfer("PackageB", "PackageA"); newData.AddTransfer("PackageB", "PackageB"); newData.AddTransfer("PackageA", "PackageC"); newData.AddTransfer("PackageA", "packagec"); newData.AddTransfer("PackageA", "packageC"); newData.AddTransfer("PackageA", "PackageB"); newData.AddTransfer("PackageC", "PackageZ"); await Target.ReplaceLatestIndexedAsync(newData, AccessCondition.Object); // Pretty-ify the JSON to make the assertion clearer. var json = Assert.Single(SavedStrings); json = JsonConvert.DeserializeObject <JObject>(json).ToString(); Assert.Equal(@"{ ""PackageA"": [ ""PackageB"", ""PackageC"" ], ""PackageB"": [ ""PackageA"", ""PackageB"" ], ""PackageC"": [ ""PackageZ"" ] }", json); TelemetryService.Verify( x => x.TrackReplaceLatestIndexedPopularityTransfers( /*outgoingTransfers: */ 3), Times.Once); ReplaceLatestIndexedPopularityTransfersDurationMetric.Verify(x => x.Dispose(), Times.Once); }