public async Task NotificationIsReceivedWhenConflictIsDetected()
        {
            using (var sourceStore = NewStore(0) )
            using (var destinationStore = NewStore(1))
            {
                var sourceClient = sourceStore.AsyncFilesCommands;
                var destinationClient = destinationStore.AsyncFilesCommands;

                var sourceContent = new MemoryStream();
                new RandomlyModifiedStream(new RandomStream(1), 0.01).CopyTo(sourceContent);

                sourceContent.Position = 0;
                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", destinationContent, destinationMetadata);
                sourceContent.Position = 0;
                await sourceClient.UploadAsync("abc.txt", sourceContent, sourceMetadata);

                var notificationTask = destinationStore.Changes()
                                            .ForConflicts()
                                            .OfType<ConflictNotification>()
                                            .Where(x => x.Status == ConflictStatus.Detected)
                                            .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(sourceStore.Url).Port, new Uri(conflictDetected.SourceServerUrl).Port);
            }
        }
		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.StartAsync().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.StartAsync();

            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);
		}
        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();
		}
		public async Task Big_character_file_test(long size)
		{
			var sourceContent = new RandomCharacterStream(size);
			var destinationContent = new RandomlyModifiedStream(new RandomCharacterStream(size), 0.01);
			var destinationClient = NewAsyncClient(0);
			var sourceClient = NewAsyncClient(1);
            var sourceMetadata = new RavenJObject
				                     {
					                     {"SomeTest-metadata", "some-value"}
				                     };
            var destinationMetadata = new RavenJObject
				                          {
					                          {"SomeTest-metadata", "should-be-overwritten"}
				                          };

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

			SynchronizationReport result = SyncTestUtils.ResolveConflictAndSynchronize(sourceClient, destinationClient, "test.bin");
			Assert.Equal(sourceContent.Length, result.BytesCopied + result.BytesTransfered);
		}
		public async Task 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);
		}
		public async Task Should_have_the_same_content(int size)
		{
			var sourceContent = SyncTestUtils.PrepareSourceStream(size);
			sourceContent.Position = 0;
			var destinationContent = new RandomlyModifiedStream(sourceContent, 0.01);
			var destinationClient = NewAsyncClient(0);
			var sourceClient = NewAsyncClient(1);

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

			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.Equal(sourceMd5, resultMd5);
		}