コード例 #1
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);
		}
コード例 #2
0
        public async Task Should_mark_file_to_be_resolved_using_current_strategy()
        {
            var differenceChunk = new MemoryStream();
            var sw = new StreamWriter(differenceChunk);

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

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

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

            var sourceMetadata = new RavenJObject
                                     {
                                         {"SomeTest-metadata", "some-value"}
                                     };
            var destinationMetadata = new RavenJObject
                                          {
                                              {"SomeTest-metadata", "shouldnt-be-overwritten"}
                                          };

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


            var shouldBeConflict = sourceClient.Synchronization.StartAsync("test.txt", destinationClient).Result;

            Assert.Equal(string.Format("File {0} is conflicted", FileHeader.Canonize("test.txt")), shouldBeConflict.Exception.Message);

            await destinationClient.Synchronization.ResolveConflictAsync("test.txt", ConflictResolutionStrategy.CurrentVersion);
            var result = await destinationClient.Synchronization.StartAsync("test.txt", sourceClient);
            Assert.Equal(destinationContent.Length, result.BytesCopied + result.BytesTransfered);

            // check if conflict resolution has been properly set on the source
            string resultMd5;
            using (var resultFileContent = await sourceClient.DownloadAsync("test.txt"))
            {
                var metadata = await sourceClient.GetMetadataForAsync("test.txt");
                Assert.Equal("shouldnt-be-overwritten", metadata.Value<string>("SomeTest-Metadata"));
                
                resultMd5 = resultFileContent.GetMD5Hash();
            }

            destinationContent.Position = 0;
            var destinationMd5 = destinationContent.GetMD5Hash();
            sourceContent.Position = 0;

            Assert.True(resultMd5 == destinationMd5);
        }
コード例 #3
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);
		}
コード例 #4
0
		public void Synchronize_file_with_different_beginning()
		{
			const int size = 5000;
			var differenceChunk = new MemoryStream();
			var sw = new StreamWriter(differenceChunk);

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

			var sourceContent = PrepareSourceStream(size);
			sourceContent.Position = 0;
			var seedContent = new CombinedStream(differenceChunk, sourceContent);

			using (var sourceSignatureRepository = CreateSignatureRepositoryFor("test2"))
			using (var seedSignatureRepository = CreateSignatureRepositoryFor("test1"))
			{
				IList<SignatureInfo> seedSignatureInfos;
				using (var generator = new SigGenerator())
				{
					seedContent.Seek(0, SeekOrigin.Begin);
					seedSignatureInfos = generator.GenerateSignatures(seedContent, "test1", seedSignatureRepository);
				}
				IList<SignatureInfo> sourceSignatureInfos;
				using (var generator = new SigGenerator())
				{
					sourceContent.Seek(0, SeekOrigin.Begin);
					sourceSignatureInfos = generator.GenerateSignatures(sourceContent, "test2", sourceSignatureRepository);
				}
				var sourceSize = sourceContent.Length;

				using (var tested = new NeedListGenerator(seedSignatureRepository, sourceSignatureRepository))
				{
					var result = tested.CreateNeedsList(seedSignatureInfos.Last(), sourceSignatureInfos.Last());
					Assert.NotNull(result);
					Assert.Equal(2, result.Count);
					Assert.Equal(0, sourceSize - result.Sum(x => Convert.ToInt32(x.BlockLength)));
				}
			}
		}
コード例 #5
0
		public async Task Should_reuse_pages_when_data_appended(int numberOfPages)
		{
            string filename = FileHeader.Canonize("test");

			var file = SyncTestUtils.PreparePagesStream(numberOfPages);

			var sourceContent = new CombinedStream(file, SyncTestUtils.PreparePagesStream(numberOfPages));
				// add new pages at the end
			var destinationContent = file;

			sourceContent.Position = 0;
            await source.UploadAsync(filename, sourceContent);
			destinationContent.Position = 0;
            await destination.UploadAsync(filename, destinationContent);

            var contentUpdate = new ContentUpdateWorkItem(filename, "http://localhost:12345", sourceRfs.Storage, sourceRfs.SigGenerator);

			// force to upload entire file, we just want to check which pages will be reused
		    await contentUpdate.UploadToAsync(destination.Synchronization);
            await destination.Synchronization.ResolveConflictAsync(filename, ConflictResolutionStrategy.RemoteVersion);
            await contentUpdate.UploadToAsync(destination.Synchronization);

           
			FileAndPagesInformation fileAndPages = null;
            destinationRfs.Storage.Batch(accessor => fileAndPages = accessor.GetFile(filename, 0, 2 * numberOfPages));

			Assert.Equal(2*numberOfPages, fileAndPages.Pages.Count);

			for(var i = 0; i < numberOfPages; i++)
			{
				Assert.Equal(i + 1, fileAndPages.Pages[i].Id);
					// if page ids are in the original order it means that they were used the existing pages
			}

			sourceContent.Position = 0;
            Assert.Equal(sourceContent.GetMD5Hash(), destination.GetMetadataForAsync(filename).Result["Content-MD5"]);
		}