示例#1
0
        private async Task TestSuccessfulPutStreamAndGet(IContentStoreInternal store, byte[] content)
        {
            ContentHash actualContentHash = content.CalculateHash(ContentHashType);

            using (var contentStream = new MemoryStream(content))
            {
                var context = new Context(Logger);
                var result  = await store.PutStreamAsync(context, contentStream, actualContentHash);

                ContentHash hashFromPut = result.ContentHash;
                long        sizeFromPut = result.ContentSize;

                Assert.True(actualContentHash.Equals(hashFromPut));
                Assert.Equal(content.Length, sizeFromPut);

                // Get the content from the store
                using (var pinContext = store.CreatePinContext())
                {
                    var r2 = await store.PinAsync(context, hashFromPut, pinContext);

                    r2.ShouldBeSuccess();
                    var r3 = await store.OpenStreamAsync(context, hashFromPut);

                    r3.ShouldBeSuccess();
                    byte[] bytes = await r3.Stream.GetBytes();

                    Assert.True(content.SequenceEqual(bytes));
                }
            }
        }
        protected async Task <PutResult> PutStreamAsync(IContentStoreInternal store, MemoryStream content)
        {
            var r = await store.PutStreamAsync(Context, content, ContentHashType);

            Clock.Increment();
            return(r);
        }
        /// <summary>
        ///     Put a randomly-sized piece of content into the store.
        /// </summary>
        public static async Task <PutResult> PutRandomAsync(
            this IContentStoreInternal store, Context context, int size, HashType hashType = HashType.Vso0, PinRequest pinRequest = default(PinRequest))
        {
            var data = ThreadSafeRandom.GetBytes(size);

            using (var stream = new MemoryStream(data))
            {
                return(await store.PutStreamAsync(context, stream, hashType, pinRequest));
            }
        }
        protected async Task <ContentHash> PutAsync
        (
            IContentStoreInternal store,
            MemoryStream content,
            bool useFile      = false,
            HashType hashType = ContentHashType,
            bool sync         = false
        )
        {
            ContentHash contentHash;

            if (useFile)
            {
                using (var directory = new DisposableDirectory(FileSystem))
                {
                    var path = directory.CreateRandomFileName();
                    using (var stream = await FileSystem.OpenAsync(
                               path, FileAccess.Write, FileMode.CreateNew, FileShare.Delete))
                    {
                        content.WriteTo(stream);
                    }

                    var r = await store.PutFileAsync(Context, path, FileRealizationMode.Any, hashType).ShouldBeSuccess();

                    contentHash = r.ContentHash;
                }
            }
            else
            {
                var r = await store.PutStreamAsync(Context, content, hashType).ShouldBeSuccess();

                contentHash = r.ContentHash;
            }

            Clock.Increment();

            if (sync)
            {
                await store.SyncAsync(Context);
            }

            return(contentHash);
        }