public async Task ReadingIsLazy(int byteCount)
        {
            const string dataId = "known id";

            Console.WriteLine($"Generating {byteCount/(double)(1024*1024):0.00} MB of data...");

            var data = GenerateData(byteCount);

            Console.WriteLine("Saving data...");

            await _storage.Save(dataId, new MemoryStream(data));

            Console.WriteLine("Reading data...");

            var stopwatch = Stopwatch.StartNew();

            await using var source = await _storage.Read(dataId);

            await using var destination = new MemoryStream();
            var elapsedWhenStreamIsOpen = stopwatch.Elapsed;

            Console.WriteLine($"Opening stream took {elapsedWhenStreamIsOpen.TotalSeconds:0.00} s");

            await source.CopyToAsync(destination);

            var elapsedWhenStreamHasBeenRead = stopwatch.Elapsed;

            Console.WriteLine($"Entire operation took {elapsedWhenStreamHasBeenRead.TotalSeconds:0.00} s");

            var fraction = elapsedWhenStreamHasBeenRead.TotalSeconds / 1;

            Assert.That(elapsedWhenStreamIsOpen.TotalSeconds, Is.LessThan(fraction),
                        "Expected time to open stream to be less than 1/1 of the time it takes to read the entire stream");
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task <DataBusAttachment> CreateAttachment(Stream source, Dictionary <string, string> optionalMetadata = null)
        {
            var id = Guid.NewGuid().ToString();

            await _dataBusStorage.Save(id, source, optionalMetadata).ConfigureAwait(false);

            return(new DataBusAttachment(id));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Saves the data from the given source stream under the given ID
        /// </summary>
        public async Task Save(string id, Stream source, Dictionary <string, string> metadata = null)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            metadata = metadata?.Clone() ?? new Dictionary <string, string>();

            if (_dataCompressionMode == DataCompressionMode.Explicit)
            {
                if (!metadata.TryGetValue(MetadataKeys.ContentEncoding, out var contentEncoding))
                {
                    await _innerDataBusStorage.Save(id, source, metadata);

                    return;
                }

                // who knows what the user did to the data? the user must know how to decode this data
                if (!string.Equals(contentEncoding, "gzip", StringComparison.OrdinalIgnoreCase))
                {
                    await _innerDataBusStorage.Save(id, source, metadata);

                    return;
                }
            }

            metadata[MetadataKeys.ContentEncoding] = "gzip";

            using (var destination = new MemoryStream())
            {
                using (var gzipStream = new GZipStream(destination, CompressionLevel.Optimal, true))
                {
                    await source.CopyToAsync(gzipStream);
                }

                destination.Position = 0;

                await _innerDataBusStorage.Save(id, destination, metadata);
            }
        }
Exemplo n.º 4
0
        public async Task UpdatesTimeOfLastRead()
        {
            const string knownId = "known id";

            using (var source = new MemoryStream(new byte[0]))
            {
                await _storage.Save(knownId, source);
            }

            var hadLastReadTime = (await _storage.ReadMetadata(knownId)).ContainsKey(MetadataKeys.ReadTime);

            Assert.That(hadLastReadTime, Is.False, "Did not expect the {0} key to be set", MetadataKeys.ReadTime);

            var justSomeTime = new DateTimeOffset(1.January(2016));

            RebusTimeMachine.FakeIt(justSomeTime);

            _storage.Read(knownId).Result.Dispose();

            var metadata = await _storage.ReadMetadata(knownId);

            Assert.That(metadata.ContainsKey(MetadataKeys.ReadTime), Is.True);

            var readTimeMetadata = metadata[MetadataKeys.ReadTime];
            var readTime         = DateTimeOffset.Parse(readTimeMetadata);

            Assert.That(readTime, Is.EqualTo(justSomeTime),
                        $"Expected that the '{MetadataKeys.ReadTime}' metadata value '{readTimeMetadata}' would equal {justSomeTime} when passed to DateTimeOffset.Parse(...)");
        }
Exemplo n.º 5
0
        public async Task <DataBusAttachment> CreateAttachment(Stream source, Dictionary <string, string> optionalMetadata = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var id = Guid.NewGuid().ToString();

            await _dataBusStorage.Save(id, source, optionalMetadata);

            return(new DataBusAttachment(id));
        }
Exemplo n.º 6
0
        public void Throws_When_Trying_To_Use_Metadata_Delimiter_In_Key_Part()
        {
            // Setup
            var defaultDelimiter = new AmazonS3DataBusOptions("bucket").MetadataDelimiter;
            var illegalMetadata  = new Dictionary <string, string>
            {
                [$"Illegal{defaultDelimiter}Key"] = "Any Value"
            };

            // Test
            var exception = Assert.Throws <AggregateException>(() =>
            {
                using var source = new MemoryStream(Array.Empty <byte>());

                _storage.Save(KnownId, source, illegalMetadata).Wait();
            });

            // Verify
            var rebusException = exception.GetBaseException();

            Assert.That(rebusException, Is.TypeOf <RebusApplicationException>());
            Assert.That(rebusException.InnerException, Is.TypeOf <ArgumentException>());
        }
Exemplo n.º 7
0
        public async Task CanQueryByTimeStamps_ReadTime()
        {
            if (NoManagement())
            {
                return;
            }

            async Task CreateAttachment(DateTime readTime, string attachmendId)
            {
                _factory.FakeIt(DateTimeOffset.Now);
                using (var source = new MemoryStream(new byte[0]))
                {
                    await _storage.Save(attachmendId, source);
                }

                // update read time
                _factory.FakeIt(readTime);
                using (await _storage.Read(attachmendId))
                {
                }
            }

            await CreateAttachment(new DateTime(2019, 01, 01), "id1");
            await CreateAttachment(new DateTime(2019, 02, 01), "id2");
            await CreateAttachment(new DateTime(2019, 03, 01), "id3");
            await CreateAttachment(new DateTime(2019, 04, 01), "id4");
            await CreateAttachment(new DateTime(2019, 05, 01), "id5");

            var ids1 = _management.Query(readTime: new TimeRange(from: new DateTime(2019, 02, 01))).InOrder().ToList();
            var ids2 = _management.Query(readTime: new TimeRange(from: new DateTime(2019, 03, 01))).InOrder().ToList();
            var ids3 = _management.Query(readTime: new TimeRange(from: new DateTime(2019, 03, 01), to: new DateTime(2019, 05, 01))).InOrder().ToList();

            Assert.That(ids1, Is.EqualTo(new[] { "id2", "id3", "id4", "id5" }));
            Assert.That(ids2, Is.EqualTo(new[] { "id3", "id4", "id5" }));
            Assert.That(ids3, Is.EqualTo(new[] { "id3", "id4" }));
        }
        public async Task UpdatesTimeOfLastRead()
        {
            const string knownId = "known id";

            using (var source = new MemoryStream(new byte[0]))
            {
                await _storage.Save(knownId, source);
            }

            var hadLastReadTime = (await _storage.ReadMetadata(knownId)).ContainsKey(MetadataKeys.ReadTime);

            Assert.That(hadLastReadTime, Is.False, "Did not expect the {0} key to be set", MetadataKeys.ReadTime);

            var justSomeTime = new DateTimeOffset(1.January(2016));

            RebusTimeMachine.FakeIt(justSomeTime);

            _storage.Read(knownId).Result.Dispose();

            var metadata = await _storage.ReadMetadata(knownId);

            Assert.That(metadata.ContainsKey(MetadataKeys.ReadTime), Is.True);
            Assert.That(metadata[MetadataKeys.ReadTime], Is.EqualTo(justSomeTime.ToString("O")));
        }