Exemplo n.º 1
0
        public async Task NotificationReceivedWhenFileAdded()
        {
            var notificationTask = store.Changes().ForFolder("/")
                                   .Timeout(TimeSpan.FromSeconds(2))
                                   .Take(1).ToTask();

            await client.UploadAsync("abc.txt", new MemoryStream());

            var fileChange = await notificationTask;

            Assert.Equal("/abc.txt", fileChange.File);
            Assert.Equal(FileChangeAction.Add, fileChange.Action);
        }
 private async Task CreateSampleData(IAsyncFilesCommands commands, int startIndex = 1, int count = 2)
 {
     for (var i = startIndex; i < startIndex + count; i++)
     {
         await commands.UploadAsync(string.Format("file{0}.bin", i), StringToStream("Secret records / " + i));
     }
 }
Exemplo n.º 3
0
        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", destinationContent, destinationMetadata);

            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);
        }
Exemplo n.º 4
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();
        }
        private static async Task RunMetadataSynchronization(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
        {
            await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("metadata-test")), new RavenJObject { { "Sample-Header", "destination" } });

            if (action != null)
            {
                action();
            }

            await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("metadata-test")), new RavenJObject { { "Sample-Header", "source" } });

            var report = await sourceClient.Synchronization.StartAsync("test", destinationClient);

            Assert.Null(report.Exception);
            Assert.Equal(SynchronizationType.MetadataUpdate, report.Type);
        }
        private static async Task RunDeleteSynchronization(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
        {
            await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("delete-test")));

            if (action != null)
            {
                action();
            }

            await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("delete-test")));

            await sourceClient.DeleteAsync("test");

            var report = await sourceClient.Synchronization.StartAsync("test", destinationClient);

            Assert.Null(report.Exception);
            Assert.Equal(SynchronizationType.Delete, report.Type);
        }
        private static async Task <string> RunContentSynchronization(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
        {
            await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("destination")));

            if (action != null)
            {
                action();
            }

            await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("source")));

            var report = await sourceClient.Synchronization.StartAsync("test", destinationClient);

            Assert.Null(report.Exception);
            Assert.Equal(SynchronizationType.ContentUpdate, report.Type);

            var stream = await destinationClient.DownloadAsync("test");

            return(StreamToString(stream));
        }
        private static async Task <string> ExecuteRawSynchronizationRequest(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
        {
            await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("destination")));

            if (action != null)
            {
                action();
            }

            await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("source")));

            var sourceStream = new MemoryStream();

            (await sourceClient.DownloadAsync("test")).CopyTo(sourceStream);

            var metadata = await sourceClient.GetMetadataForAsync("test");

            var request = new SynchronizationMultipartRequest(destinationClient.Synchronization, new ServerInfo()
            {
                FileSystemUrl = sourceClient.UrlFor(),
                Id            = sourceClient.GetServerIdAsync().Result
            }, "test", metadata, sourceStream, new[]
            {
                new RdcNeed()
                {
                    BlockLength = 6,
                    BlockType   = RdcNeedType.Source,
                    FileOffset  = 0
                }
            });

            var synchronizationReport = await request.PushChangesAsync(CancellationToken.None);

            Assert.Null(synchronizationReport.Exception);

            var stream = await destinationClient.DownloadAsync("test");

            return(StreamToString(stream));
        }
		private static async Task<string> ExecuteRawSynchronizationRequest(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
		{
			await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("destination")));

			if (action != null)
				action();

			await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("source")));

			var sourceStream = new MemoryStream();

			(await sourceClient.DownloadAsync("test")).CopyTo(sourceStream);

			var metadata = await sourceClient.GetMetadataForAsync("test");

			var request = new SynchronizationMultipartRequest(destinationClient.Synchronization, new ServerInfo()
			{
				FileSystemUrl = sourceClient.UrlFor(),
				Id = sourceClient.GetServerIdAsync().Result
			}, "test", metadata, sourceStream, new[]
			{
				new RdcNeed()
				{
					BlockLength = 6,
					BlockType = RdcNeedType.Source,
					FileOffset = 0
				}
			});

			var synchronizationReport = await request.PushChangesAsync(CancellationToken.None);

			Assert.Null(synchronizationReport.Exception);

			var stream = await destinationClient.DownloadAsync("test");

			return StreamToString(stream);
		}
Exemplo n.º 10
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();
		}
		private async Task CreateSampleData(IAsyncFilesCommands commands, int startIndex = 1, int count = 2)
		{
			for (var i = startIndex; i < startIndex + count; i++)
			{
				await commands.UploadAsync(string.Format("file{0}.bin", i), StringToStream("Secret records / " + i));
			}
		}
        public async Task NotificationsAreReceivedOnSourceWhenSynchronizationsAreStartedAndFinished()
        {
            // content update
            await sourceClient.UploadAsync("test.bin", new MemoryStream(new byte[] { 1, 2, 3 }));

            var notificationTask = sourceStore.Changes().ForSynchronization()
                                   .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                                   .Timeout(TimeSpan.FromSeconds(20)).Take(2).ToArray().
                                   ToTask();

            var report = await sourceClient.Synchronization.StartAsync("test.bin", destinationClient);

            Assert.Null(report.Exception);

            var synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.ContentUpdate, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.ContentUpdate, synchronizationUpdates[1].Type);

            // metadata update
            await sourceClient.UpdateMetadataAsync("test.bin", new RavenJObject { { "key", "value" } });

            notificationTask = sourceStore.Changes().ForSynchronization()
                               .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                               .Timeout(TimeSpan.FromSeconds(20))
                               .Take(2).ToArray()
                               .ToTask();

            report = await sourceClient.Synchronization.StartAsync("test.bin", destinationClient);

            Assert.Null(report.Exception);

            synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.MetadataUpdate, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.MetadataUpdate, synchronizationUpdates[1].Type);

            // rename update
            await sourceClient.RenameAsync("test.bin", "rename.bin");

            notificationTask = sourceStore.Changes().ForSynchronization()
                               .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                               .Timeout(TimeSpan.FromSeconds(20))
                               .Take(2).ToArray()
                               .ToTask();

            report = await sourceClient.Synchronization.StartAsync("test.bin", destinationClient);

            Assert.Null(report.Exception);

            synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.Rename, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.Rename, synchronizationUpdates[1].Type);

            // delete update
            await sourceClient.DeleteAsync("rename.bin");

            notificationTask = sourceStore.Changes().ForSynchronization()
                               .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                               .Timeout(TimeSpan.FromSeconds(20))
                               .Take(2).ToArray()
                               .ToTask();

            report = await sourceClient.Synchronization.StartAsync("rename.bin", destinationClient);

            Assert.Null(report.Exception);

            synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("rename.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.Delete, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("rename.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.Delete, synchronizationUpdates[1].Type);
        }
		private static async Task RunDeleteSynchronization(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
		{
			await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("delete-test")));

			if (action != null)
				action();

			await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("delete-test")));
			await sourceClient.DeleteAsync("test");

			var report = await sourceClient.Synchronization.StartAsync("test", destinationClient);

			Assert.Null(report.Exception);
			Assert.Equal(SynchronizationType.Delete, report.Type);
		}
		private static async Task RunMetadataSynchronization(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
		{
			await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("metadata-test")), new RavenJObject { { "Sample-Header", "destination" } });

			if (action != null)
				action();

			await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("metadata-test")), new RavenJObject { { "Sample-Header", "source" } });

			var report = await sourceClient.Synchronization.StartAsync("test", destinationClient);

			Assert.Null(report.Exception);
			Assert.Equal(SynchronizationType.MetadataUpdate, report.Type);
		}
		private static async Task<string> RunContentSynchronization(IAsyncFilesCommands sourceClient, IAsyncFilesCommands destinationClient, Action action = null)
		{
			await destinationClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("destination")));

			if (action != null)
				action();

			await sourceClient.UploadAsync("test", new MemoryStream(Encoding.UTF8.GetBytes("source")));

			var report = await sourceClient.Synchronization.StartAsync("test", destinationClient);

			Assert.Null(report.Exception);
			Assert.Equal(SynchronizationType.ContentUpdate, report.Type);

			var stream = await destinationClient.DownloadAsync("test");

			return StreamToString(stream);
		}