public async Task NotificationIsReceivedWhenConflictIsDetected()
		{
			var sourceContent = new RandomlyModifiedStream(new RandomStream(1), 0.01);
			var destinationContent = new RandomlyModifiedStream(sourceContent, 0.01);

            var sourceMetadata = new RavenJObject
				                     {
					                     {"SomeTest-metadata", "some-value"}
				                     };

            var destinationMetadata = new RavenJObject
				                          {
					                          {"SomeTest-metadata", "should-be-overwritten"}
				                          };

			await destinationClient.UploadAsync("abc.txt", destinationMetadata, destinationContent);
			await sourceClient.UploadAsync("abc.txt", sourceMetadata, sourceContent);

            var notificationTask = destinationClient.Changes().ForConflicts()
                                                         .OfType<ConflictDetectedNotification>()
				                                         .Timeout(TimeSpan.FromSeconds(5))
				                                         .Take(1)
				                                         .ToTask();

			await sourceClient.Synchronization.StartAsync("abc.txt", destinationClient);

			var conflictDetected = await notificationTask;

			Assert.Equal("abc.txt", conflictDetected.FileName);
			Assert.Equal(new Uri(sourceClient.ServerUrl).Port, new Uri(conflictDetected.SourceServerUrl).Port);
		}
		public async void 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);
		}
		public void Should_synchronize_to_all_destinations()
		{
			StartServerInstance(AddtitionalServerInstancePortNumber);

			var sourceContent = SyncTestUtils.PrepareSourceStream(10000);
			sourceContent.Position = 0;

			var sourceClient = NewClient(0);

			var destination1Client = NewClient(1);
			var destination2Client = new RavenFileSystemClient(ServerAddress(AddtitionalServerInstancePortNumber));

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

			destination1Client.UploadAsync("test.bin", destination1Content).Wait();
			destination2Client.UploadAsync("test.bin", destination2Content).Wait();

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

			sourceClient.Config.SetConfig(SynchronizationConstants.RavenSynchronizationDestinations, new NameValueCollection
				                                                                                         {
					                                                                                         {
						                                                                                         "url",
						                                                                                         destination1Client
						                                                                                         .ServerUrl
					                                                                                         },
					                                                                                         {
						                                                                                         "url",
						                                                                                         destination2Client
						                                                                                         .ServerUrl
					                                                                                         }
				                                                                                         }).Wait();

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

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

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

			destinationSyncResults = sourceClient.Synchronization.SynchronizeDestinationsAsync().Result;

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

			var result2 = destinationSyncResults[1].Reports.ToArray()[0];
			Assert.Equal(sourceContent.Length, result2.BytesCopied + result2.BytesTransfered);

			// check content of files
			string destination1Md5;
			using (var resultFileContent = new MemoryStream())
			{
				destination1Client.DownloadAsync("test.bin", resultFileContent).Wait();
				resultFileContent.Position = 0;
				destination1Md5 = resultFileContent.GetMD5Hash();
			}

			string destination2Md5;
			using (var resultFileContent = new MemoryStream())
			{
				destination2Client.DownloadAsync("test.bin", resultFileContent).Wait();
				resultFileContent.Position = 0;
				destination2Md5 = resultFileContent.GetMD5Hash();
			}

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

			Assert.Equal(sourceMd5, destination1Md5);
			Assert.Equal(sourceMd5, destination2Md5);
			Assert.Equal(destination1Md5, destination2Md5);
		}
Пример #4
0
		private void UploadFilesSynchronously(out RavenFileSystemClient sourceClient,
		                                      out RavenFileSystemClient destinationClient, string fileName = "test.bin")
		{
			sourceClient = NewClient(1);
			destinationClient = NewClient(0);

			var sourceContent = new RandomlyModifiedStream(new RandomStream(10, 1), 0.01);
			var destinationContent = new RandomlyModifiedStream(new RandomStream(10, 1), 0.01);

			destinationClient.UploadAsync(fileName, EmptyData, destinationContent).Wait();
			sourceClient.UploadAsync(fileName, EmptyData, sourceContent).Wait();
		}
Пример #5
0
        private void UploadFilesSynchronously(out IAsyncFilesCommands sourceClient,
                                              out IAsyncFilesCommands destinationClient, string fileName = "test.bin")
		{
			sourceClient = NewAsyncClient(1);
			destinationClient = NewAsyncClient(0);

			var sourceContent = new RandomlyModifiedStream(new RandomStream(10, 1), 0.01);
			var destinationContent = new RandomlyModifiedStream(new RandomStream(10, 1), 0.01);

			destinationClient.UploadAsync(fileName, destinationContent).Wait();
			sourceClient.UploadAsync(fileName, sourceContent).Wait();
		}
Пример #6
0
		public void Big_character_file_test(long size)
		{
			var sourceContent = new RandomCharacterStream(size);
			var destinationContent = new RandomlyModifiedStream(new RandomCharacterStream(size), 0.01);
			var destinationClient = NewClient(0);
			var sourceClient = NewClient(1);
			var sourceMetadata = new NameValueCollection
				                     {
					                     {"SomeTest-metadata", "some-value"}
				                     };
			var destinationMetadata = new NameValueCollection
				                          {
					                          {"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);
		}
Пример #7
0
		public 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 = NewClient(0);
			var sourceClient = NewClient(1);

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


			destinationClient.UploadAsync("test.bin", new NameValueCollection(), destinationContent).Wait();
			sourceClient.UploadAsync("test.bin", new NameValueCollection(), sourceContent).Wait();

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

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

			string resultMd5;
			using (var resultFileContent = new MemoryStream())
			{
				destinationClient.DownloadAsync("test.bin", resultFileContent).Wait();
				resultFileContent.Position = 0;
				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 = new MemoryStream())
			{
				destinationClient.DownloadAsync("test.bin", resultFileContent).Wait();
				resultFileContent.Position = 0;
				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);
		}
Пример #8
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 NameValueCollection(), destinationContent).Wait();
			sourceContent.Position = 0;
			sourceClient.UploadAsync("test.txt", new NameValueCollection(), 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);
		}
		public async Task File_content_change_should_be_propagated()
		{
			var buffer = new byte[1024*1024*2]; // 2 MB
			new Random().NextBytes(buffer);

			var content = new MemoryStream(buffer);
			var changedContent = new RandomlyModifiedStream(content, 0.02);

			var server1 = NewClient(0);
			var server2 = NewClient(1);
            var server3 = NewClient(2);

			content.Position = 0;
            await server1.UploadAsync("test.bin", new RavenJObject { { "test", "value" } }, content);
			
			Assert.Equal(1, server1.StatsAsync().Result.FileCount);

			SyncTestUtils.TurnOnSynchronization(server1, server2);

			Assert.Null(server1.Synchronization.SynchronizeDestinationsAsync().Result[0].Exception);
			Assert.Equal(1, server2.StatsAsync().Result.FileCount);

			SyncTestUtils.TurnOnSynchronization(server2, server3);

			Assert.Null(server2.Synchronization.SynchronizeDestinationsAsync().Result[0].Exception);
			Assert.Equal(1, server3.StatsAsync().Result.FileCount);

			SyncTestUtils.TurnOffSynchronization(server1);

			content.Position = 0;
            await server1.UploadAsync("test.bin", changedContent);

			SyncTestUtils.TurnOnSynchronization(server1, server2);

			var secondServer1Synchronization = await server1.Synchronization.SynchronizeDestinationsAsync();
			Assert.Null(secondServer1Synchronization[0].Exception);
			Assert.Equal(SynchronizationType.ContentUpdate, secondServer1Synchronization[0].Reports.ToArray()[0].Type);

			var secondServer2Synchronization = await server2.Synchronization.SynchronizeDestinationsAsync();
			Assert.Null(secondServer2Synchronization[0].Exception);
			Assert.Equal(SynchronizationType.ContentUpdate, secondServer2Synchronization[0].Reports.ToArray()[0].Type);

			// On all servers should have the same content of the file
			string server1Md5;
			using (var resultFileContent = new MemoryStream())
			{
				server1.DownloadAsync("test.bin", resultFileContent).Wait();
				resultFileContent.Position = 0;
				server1Md5 = resultFileContent.GetMD5Hash();
			}

			string server2Md5;
			using (var resultFileContent = new MemoryStream())
			{
				server2.DownloadAsync("test.bin", resultFileContent).Wait();
				resultFileContent.Position = 0;
				server2Md5 = resultFileContent.GetMD5Hash();
			}

			string server3Md5;
			using (var resultFileContent = new MemoryStream())
			{
				server3.DownloadAsync("test.bin", resultFileContent).Wait();
				resultFileContent.Position = 0;
				server3Md5 = resultFileContent.GetMD5Hash();
			}

			Assert.Equal(server1Md5, server2Md5);
			Assert.Equal(server2Md5, server3Md5);

			Assert.Equal(1, server1.StatsAsync().Result.FileCount);
			Assert.Equal(1, server2.StatsAsync().Result.FileCount);
			Assert.Equal(1, server3.StatsAsync().Result.FileCount);
		}