ResolveConflictAndSynchronize() public static method

public static ResolveConflictAndSynchronize ( RavenFileSystemClient sourceClient, RavenFileSystemClient destinationClient, string fileName ) : Raven.Abstractions.FileSystem.SynchronizationReport
sourceClient RavenFileSystemClient
destinationClient RavenFileSystemClient
fileName string
return Raven.Abstractions.FileSystem.SynchronizationReport
示例#1
0
        public void Can_synchronize_file_with_less_number_of_signatures()
        {
            const int size5Mb = 1024 * 1024 * 5;
            const int size1Mb = 1024 * 1024;

            var source      = NewClient(0);
            var destination = NewClient(1);

            var buffer = new byte[size1Mb];             // 1Mb file should have 1 signature

            new Random().NextBytes(buffer);

            var sourceContent = new MemoryStream(buffer);

            source.UploadAsync("test.bin", sourceContent).Wait();

            buffer = new byte[size5Mb];             // while 5Mb file has 2 signatures
            new Random().NextBytes(buffer);

            destination.UploadAsync("test.bin", new MemoryStream(buffer)).Wait();

            var sourceSigCount      = source.Synchronization.GetRdcManifestAsync("test.bin").Result.Signatures.Count;
            var destinationSigCount = destination.Synchronization.GetRdcManifestAsync("test.bin").Result.Signatures.Count;

            Assert.True(sourceSigCount > 0, "Source file should have one signature");
            // ensure that file on source has less signatures than file on destination
            Assert.True(sourceSigCount < destinationSigCount, "File on source should be smaller in order to have less signatures");

            var result = SyncTestUtils.ResolveConflictAndSynchronize(source, destination, "test.bin");

            Assert.Null(result.Exception);
            Assert.Equal(size1Mb, result.BytesTransfered + result.BytesCopied);
            sourceContent.Position = 0;
            Assert.Equal(sourceContent.GetMD5Hash(), destination.GetMetadataForAsync("test.bin").Result.Value <string>("Content-MD5"));
        }
示例#2
0
        public async Task Can_synchronize_file_that_doesnt_have_any_signature_while_file_on_destination_has()
        {
            const int size1B  = 1;
            const int size5Mb = 1024 * 1024 * 5;

            var source      = NewClient(0);
            var destination = NewClient(1);

            var buffer = new byte[size1B];             // 1b file should have no signatures

            new Random().NextBytes(buffer);

            var sourceContent = new MemoryStream(buffer);
            await source.UploadAsync("test.bin", sourceContent);

            buffer = new byte[size5Mb];             // 5Mb file should have 2 signatures
            new Random().NextBytes(buffer);

            await destination.UploadAsync("test.bin", new MemoryStream(buffer));

            var sourceSigCount      = source.Synchronization.GetRdcManifestAsync("test.bin").Result.Signatures.Count;
            var destinationSigCount = destination.Synchronization.GetRdcManifestAsync("test.bin").Result.Signatures.Count;

            Assert.Equal(0, sourceSigCount);             // ensure that file on source has no signature
            Assert.True(destinationSigCount > 0, "File on destination should have any signature");

            var result = SyncTestUtils.ResolveConflictAndSynchronize(source, destination, "test.bin");

            Assert.Null(result.Exception);
            Assert.Equal(size1B, result.BytesTransfered);
            sourceContent.Position = 0;
            Assert.Equal(sourceContent.GetMD5Hash(), destination.GetMetadataForAsync("test.bin").Result.Value <string>("Content-MD5"));
        }
示例#3
0
        public void Should_synchronize_just_metadata()
        {
            var content = new MemoryStream(new byte[] { 1, 2, 3, 4 });

            var sourceClient      = NewClient(0);
            var destinationClient = NewClient(1);

            sourceClient.UploadAsync("test.bin", new RavenJObject {
                { "difference", "metadata" }
            }, content).Wait();
            content.Position = 0;
            destinationClient.UploadAsync("test.bin", content).Wait();

            var report = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");

            var conflictItem = destinationClient.Config.GetConfig <ConflictItem>(RavenFileNameHelper.ConflictConfigNameForFile("test.bin")).Result;

            Assert.Null(conflictItem);

            Assert.Equal(SynchronizationType.MetadataUpdate, report.Type);

            var destinationMetadata = destinationClient.GetMetadataForAsync("test.bin").Result;

            Assert.Equal("metadata", destinationMetadata.Value <string>("difference"));
        }
示例#4
0
        public async Task Should_just_rename_file_in_synchronization_process()
        {
            var content = new MemoryStream(new byte[] { 1, 2, 3, 4 });

            var sourceClient      = NewClient(0);
            var destinationClient = NewClient(1);

            await sourceClient.UploadAsync("test.bin", new RavenJObject { { "key", "value" } }, content);

            content.Position = 0;
            await destinationClient.UploadAsync("test.bin", new RavenJObject { { "key", "value" } }, content);

            await sourceClient.RenameAsync("test.bin", "renamed.bin");

            // we need to indicate old file name, otherwise content update would be performed because renamed file does not exist on dest
            var report = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");

            Assert.Equal(SynchronizationType.Rename, report.Type);

            var conflictItem = destinationClient.Config.GetConfig <ConflictItem>(RavenFileNameHelper.ConflictConfigNameForFile("test.bin")).Result;

            Assert.Null(conflictItem);

            var testMetadata = await destinationClient.GetMetadataForAsync("test.bin");

            var renamedMetadata = await destinationClient.GetMetadataForAsync("renamed.bin");

            Assert.Null(testMetadata);
            Assert.NotNull(renamedMetadata);

            var result = await destinationClient.GetFilesAsync("/");

            Assert.Equal(1, result.FileCount);
            Assert.Equal("renamed.bin", result.Files[0].Name);
        }
示例#5
0
        public void Should_have_the_same_content(int size)
        {
            var sourceContent = SyncTestUtils.PrepareSourceStream(size);

            sourceContent.Position = 0;
            var destinationContent = new RandomlyModifiedStream(sourceContent, 0.01);
            var destinationClient  = NewClient(0);
            var sourceClient       = NewClient(1);

            destinationClient.UploadAsync("test.txt", new RavenJObject(), destinationContent).Wait();
            sourceContent.Position = 0;
            sourceClient.UploadAsync("test.txt", new RavenJObject(), sourceContent).Wait();

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

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

            string resultMd5;

            using (var resultFileContent = new MemoryStream())
            {
                destinationClient.DownloadAsync("test.txt", resultFileContent).Wait();
                resultFileContent.Position = 0;
                resultMd5 = resultFileContent.GetMD5Hash();
            }

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

            Assert.Equal(sourceMd5, resultMd5);
        }
示例#6
0
        public async void Synchronization_of_already_synchronized_file_should_detect_that_no_work_is_needed(int size, int?seed)
        {
            Random r;

            r = seed != null ? new Random(seed.Value) : new Random();

            var bytes = new byte[size];

            r.NextBytes(bytes);

            var sourceContent      = new MemoryStream(bytes);
            var destinationContent = new RandomlyModifiedStream(new RandomStream(size, 1), 0.01, seed);
            var destinationClient  = NewAsyncClient(0);
            var sourceClient       = NewAsyncClient(1);

            var srcMd5 = sourceContent.GetMD5Hash();

            sourceContent.Position = 0;
            var dstMd5 = (new RandomlyModifiedStream(new RandomStream(size, 1), 0.01, seed)).GetMD5Hash();


            await destinationClient.UploadAsync("test.bin", destinationContent, new RavenJObject());

            await sourceClient.UploadAsync("test.bin", sourceContent, new RavenJObject());

            var firstSynchronization = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");

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

            string resultMd5;

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

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

            Assert.Equal(sourceMd5, resultMd5);

            var secondSynchronization = sourceClient.Synchronization.StartAsync("test.bin", destinationClient).Result;

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

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

            Assert.Equal(sourceMd5, resultMd5);

            Assert.Equal(0, secondSynchronization.NeedListLength);
            Assert.Equal(0, secondSynchronization.BytesTransfered);
            Assert.Equal(0, secondSynchronization.BytesCopied);
            Assert.Equal("Destination server had this file in the past", secondSynchronization.Exception.Message);
        }
示例#7
0
        public async void Should_successfully_synchronize_if_last_synchronization_timeout_exceeded()
        {
            IAsyncFilesCommands destinationClient;
            IAsyncFilesCommands sourceClient;

            UploadFilesSynchronously(out sourceClient, out destinationClient);

            await destinationClient.Configuration.SetKeyAsync(SynchronizationConstants.RavenSynchronizationLockTimeout, TimeSpan.FromSeconds(0));

            Assert.DoesNotThrow(() => SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin"));
        }
示例#8
0
        public async void 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      = NewClient(0);
            var destinationClient = NewClient(1);
            var sourceMetadata    = new RavenJObject
            {
                { "SomeTest-metadata", "some-value" }
            };
            var destinationMetadata = new RavenJObject
            {
                { "SomeTest-metadata", "should-be-overwritten" }
            };

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

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

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

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

            string resultMd5;

            using (var resultFileContent = new MemoryStream())
            {
                var metadata = destinationClient.DownloadAsync("test.txt", resultFileContent).Result;

                // REVIEW: (Oren) The xxx-yyy-zzz headers are being transformed to: Xxx-Yyy-Zzz by the underlying implementation of HTTP Client. Is that OK? The old test was able to handle it "case insensitively".
                Assert.Equal("some-value", metadata.Value <string>("SomeTest-Metadata"));
                resultFileContent.Position = 0;
                resultMd5 = resultFileContent.GetMD5Hash();
                resultFileContent.Position = 0;
            }

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

            Assert.True(resultMd5 == sourceMd5);
        }
示例#9
0
        public async void Should_refuse_to_synchronize_file_while_sync_configuration_exists()
        {
            IAsyncFilesCommands destinationClient;
            IAsyncFilesCommands sourceClient;

            UploadFilesSynchronously(out sourceClient, out destinationClient);

            await destinationClient.Configuration.SetKeyAsync(RavenFileNameHelper.SyncLockNameForFile("test.bin"), SynchronizationConfig(DateTime.UtcNow));

            var synchronizationReport = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");

            Assert.Equal(string.Format("File {0} is being synced", FileHeader.Canonize("test.bin")), synchronizationReport.Exception.Message);
        }
示例#10
0
        public async void Should_refuse_to_synchronize_file_while_sync_configuration_exists()
        {
            RavenFileSystemClient destinationClient;
            RavenFileSystemClient sourceClient;

            UploadFilesSynchronously(out sourceClient, out destinationClient);

            await destinationClient.Config.SetConfig(RavenFileNameHelper.SyncLockNameForFile("test.bin"), SynchronizationConfig(DateTime.UtcNow));

            var synchronizationReport = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");

            Assert.Equal("File test.bin is being synced", synchronizationReport.Exception.Message);
        }
示例#11
0
        public async void 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);
        }
示例#12
0
        public void Should_create_new_etag_for_replicated_file()
        {
            var destinationClient = NewClient(0);
            var sourceClient      = NewClient(1);

            sourceClient.UploadAsync("test.bin", new RandomStream(10)).Wait();

            destinationClient.UploadAsync("test.bin", new RandomStream(10)).Wait();
            var destinationEtag = sourceClient.GetMetadataForAsync("test.bin").Result["ETag"];

            SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");

            var result = destinationClient.GetMetadataForAsync("test.bin").Result["ETag"];

            Assert.True(destinationEtag != result, "Etag should be updated");
        }
示例#13
0
        public void 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      = NewClient(0);
            var destinationClient = NewClient(1);

            destinationClient.UploadAsync("test.txt", destinationContent).Wait();
            sourceContent.Position = 0;
            sourceClient.UploadAsync("test.txt", sourceContent).Wait();

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

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

            string resultMd5;

            using (var resultFileContent = new MemoryStream())
            {
                destinationClient.DownloadAsync("test.txt", resultFileContent).Wait();
                resultFileContent.Position = 0;
                resultMd5 = resultFileContent.GetMD5Hash();
                resultFileContent.Position = 0;
            }

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

            Assert.True(resultMd5 == sourceMd5);
        }
示例#14
0
        public void Should_get_all_finished_synchronizations()
        {
            var destinationClient = NewClient(0);
            var sourceClient      = NewClient(1);
            var files             = new[] { "test1.bin", "test2.bin", "test3.bin" };

            // make sure that returns empty list if there are no finished synchronizations yet
            var result = destinationClient.Synchronization.GetFinishedAsync().Result;

            Assert.Equal(0, result.TotalCount);

            foreach (var item in files)
            {
                var sourceContent = new MemoryStream();
                var sw            = new StreamWriter(sourceContent);

                sw.Write("abc123");
                sw.Flush();

                sourceContent.Position = 0;

                var destinationContent = new MemoryStream();
                var sw2 = new StreamWriter(destinationContent);

                sw2.Write("cba321");
                sw2.Flush();

                destinationContent.Position = 0;

                Task.WaitAll(
                    destinationClient.UploadAsync(item, destinationContent),
                    sourceClient.UploadAsync(item, sourceContent));

                SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, item);
            }

            result = destinationClient.Synchronization.GetFinishedAsync().Result;
            Assert.Equal(files.Length, result.TotalCount);
        }
示例#15
0
        public void Big_file_test(long size)
        {
            var sourceContent      = new RandomStream(size);
            var destinationContent = new RandomlyModifiedStream(new RandomStream(size), 0.01);
            var destinationClient  = NewClient(0);
            var sourceClient       = NewClient(1);
            var sourceMetadata     = new RavenJObject
            {
                { "SomeTest-metadata", "some-value" }
            };
            var destinationMetadata = new RavenJObject
            {
                { "SomeTest-metadata", "should-be-overwritten" }
            };

            destinationClient.UploadAsync("test.bin", destinationMetadata, destinationContent).Wait();
            sourceClient.UploadAsync("test.bin", sourceMetadata, sourceContent).Wait();

            SynchronizationReport result = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");

            Assert.Equal(sourceContent.Length, result.BytesCopied + result.BytesTransfered);
        }
示例#16
0
        public void Should_calculate_and_save_content_hash_after_synchronization()
        {
            var buffer = new byte[1024 * 1024 * 5 + 10];

            new Random().NextBytes(buffer);

            var sourceContent = new MemoryStream(buffer);
            var sourceClient  = NewClient(0);

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

            var destinationClient = NewClient(1);

            destinationClient.UploadAsync("test.bin", new RandomlyModifiedStream(sourceContent, 0.01)).Wait();
            sourceContent.Position = 0;

            SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");
            var resultFileMetadata = destinationClient.GetMetadataForAsync("test.bin").Result;

            Assert.Contains("Content-MD5", resultFileMetadata.Keys);
            Assert.Equal(sourceContent.GetMD5Hash(), resultFileMetadata.Value <string>("Content-MD5"));
        }