Ejemplo n.º 1
0
        public async Task Synchronize_file_with_different_beginning(int size)
        {
            var differenceChunk = new MemoryStream();
            var sw = new StreamWriter(differenceChunk);

            sw.Write("Coconut is Stupid");
            sw.Flush();

            var sourceContent = SyncTestUtils.PrepareSourceStream(size);

            sourceContent.Position = 0;
            var destinationContent = new CombinedStream(differenceChunk, sourceContent)
            {
                Position = 0
            };
            var sourceClient      = NewAsyncClient(0);
            var destinationClient = NewAsyncClient(1);
            var sourceMetadata    = new RavenJObject
            {
                { "SomeTest-metadata", "some-value" }
            };
            var destinationMetadata = new RavenJObject
            {
                { "SomeTest-metadata", "should-be-overwritten" }
            };

            await destinationClient.UploadAsync("test.txt", destinationContent, destinationMetadata);

            sourceContent.Position = 0;
            await sourceClient.UploadAsync("test.txt", sourceContent, sourceMetadata);

            var result = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.txt");

            Assert.Equal(sourceContent.Length, result.BytesCopied + result.BytesTransfered);

            string resultMd5;

            using (var resultFileContent = await destinationClient.DownloadAsync("test.txt"))
            {
                var metadata = await destinationClient.GetMetadataForAsync("test.txt");

                Assert.Equal("some-value", metadata.Value <string>("SomeTest-Metadata"));
                resultMd5 = resultFileContent.GetMD5Hash();
            }

            sourceContent.Position = 0;
            var sourceMd5 = sourceContent.GetMD5Hash();

            Assert.True(resultMd5 == sourceMd5);
        }
Ejemplo n.º 2
0
        public async Task Synchronize_file_with_appended_data(int size)
        {
            var differenceChunk = new MemoryStream();
            var sw = new StreamWriter(differenceChunk);

            sw.Write("Coconut is Stupid");
            sw.Flush();

            var sourceContent = new CombinedStream(SyncTestUtils.PrepareSourceStream(size), differenceChunk)
            {
                Position = 0
            };
            var destinationContent = SyncTestUtils.PrepareSourceStream(size);

            destinationContent.Position = 0;
            var sourceClient      = NewAsyncClient(0);
            var destinationClient = NewAsyncClient(1);

            await destinationClient.UploadAsync("test.txt", destinationContent);

            sourceContent.Position = 0;
            await sourceClient.UploadAsync("test.txt", sourceContent);

            var result = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.txt");

            Assert.Equal(sourceContent.Length, result.BytesCopied + result.BytesTransfered);

            string resultMd5;

            using (var resultFileContent = await destinationClient.DownloadAsync("test.txt"))
            {
                resultMd5 = resultFileContent.GetMD5Hash();
            }

            sourceContent.Position = 0;
            var sourceMd5 = sourceContent.GetMD5Hash();

            Assert.True(resultMd5 == sourceMd5);
        }
Ejemplo n.º 3
0
        public async Task Should_synchronize_to_all_destinations()
        {
            var canonicalFilename = FileHeader.Canonize("test.bin");

            var sourceContent = SyncTestUtils.PrepareSourceStream(10000);

            sourceContent.Position = 0;

            var sourceClient = NewAsyncClient(0);

            var destination1Client = NewAsyncClient(1);
            var destination2Client = NewAsyncClient(2);

            var destination1Content = new RandomlyModifiedStream(sourceContent, 0.01);

            sourceContent.Position = 0;
            var destination2Content = new RandomlyModifiedStream(sourceContent, 0.01);

            sourceContent.Position = 0;

            await destination1Client.UploadAsync("test.bin", destination1Content);

            await destination2Client.UploadAsync("test.bin", destination2Content);

            sourceContent.Position = 0;
            await sourceClient.UploadAsync("test.bin", sourceContent);

            sourceContent.Position = 0;

            sourceClient.Synchronization.SetDestinationsAsync(destination1Client.ToSynchronizationDestination(), destination2Client.ToSynchronizationDestination()).Wait();

            var destinationSyncResults = sourceClient.Synchronization.SynchronizeAsync().Result;

            // we expect conflicts after first attempt of synchronization
            Assert.Equal(2, destinationSyncResults.Length);
            Assert.Equal(string.Format("File {0} is conflicted", canonicalFilename), destinationSyncResults[0].Reports.ToArray()[0].Exception.Message);
            Assert.Equal(string.Format("File {0} is conflicted", canonicalFilename), destinationSyncResults[1].Reports.ToArray()[0].Exception.Message);

            await destination1Client.Synchronization.ResolveConflictAsync("test.bin", ConflictResolutionStrategy.RemoteVersion);

            await destination2Client.Synchronization.ResolveConflictAsync("test.bin", ConflictResolutionStrategy.RemoteVersion);

            destinationSyncResults = await sourceClient.Synchronization.SynchronizeAsync();

            var conflictItem = await destination1Client.Configuration.GetKeyAsync <ConflictItem>(RavenFileNameHelper.ConflictConfigNameForFile("test.bin"));

            Assert.Null(conflictItem);

            conflictItem = await destination2Client.Configuration.GetKeyAsync <ConflictItem>(RavenFileNameHelper.ConflictConfigNameForFile("test.bin"));

            Assert.Null(conflictItem);

            // check if reports match
            Assert.Equal(2, destinationSyncResults.Length);
            var result1 = destinationSyncResults[0].Reports.ToArray()[0];

            Assert.Equal(sourceContent.Length, result1.BytesCopied + result1.BytesTransfered);
            Assert.Equal(SynchronizationType.ContentUpdate, result1.Type);

            var result2 = destinationSyncResults[1].Reports.ToArray()[0];

            Assert.Equal(sourceContent.Length, result2.BytesCopied + result2.BytesTransfered);
            Assert.Equal(SynchronizationType.ContentUpdate, result2.Type);

            // check content of files
            string destination1Md5;

            using (var resultFileContent = await destination1Client.DownloadAsync("test.bin"))
            {
                destination1Md5 = resultFileContent.GetMD5Hash();
            }

            string destination2Md5;

            using (var resultFileContent = await destination2Client.DownloadAsync("test.bin"))
            {
                destination2Md5 = resultFileContent.GetMD5Hash();
            }

            sourceContent.Position = 0;
            var sourceMd5 = sourceContent.GetMD5Hash();

            Assert.Equal(sourceMd5, destination1Md5);
            Assert.Equal(sourceMd5, destination2Md5);
            Assert.Equal(destination1Md5, destination2Md5);
        }