コード例 #1
0
        private async Task TestSuccessfulPutAbsolutePathAndGet(IContentStoreInternal store, byte[] content, FileRealizationMode ingressModes)
        {
            using (var tempDirectory = new DisposableDirectory(FileSystem))
            {
                AbsolutePath pathToFile = CreateRandomFile(tempDirectory.Path, content);

                using (var contentStream = new MemoryStream(content))
                {
                    ContentHash actualContentHash = await contentStream.HasLength().CalculateHashAsync(ContentHashType);

                    var context = new Context(Logger);
                    var result  = await store.PutFileAsync(context, pathToFile, ingressModes, ContentHashType);

                    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);

                        var bytes = await r3.Stream.GetBytes();

                        Assert.True(content.SequenceEqual(bytes));
                    }
                }
            }
        }
コード例 #2
0
        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);
        }