PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Tests.Core/Replication/AttachmentReplication.cs

https://github.com/barryhagan/ravendb
C# | 102 lines | 70 code | 26 blank | 6 comment | 3 complexity | a49b8c6520b6f9d8dd838dec226af662 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. // -----------------------------------------------------------------------
  2. // <copyright file="AttachmentReplication.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System.IO;
  7. using System.Threading;
  8. using Raven.Abstractions.Extensions;
  9. using Raven.Client.Exceptions;
  10. using Raven.Json.Linq;
  11. using Xunit;
  12. namespace Raven.Tests.Core.Replication
  13. {
  14. public class AttachmentReplication : RavenReplicationCoreTest
  15. {
  16. [Fact]
  17. public void CanReplicateAttachments()
  18. {
  19. using (var source = GetDocumentStore())
  20. using (var destination = GetDocumentStore())
  21. {
  22. SetupReplication(source, destinations: destination);
  23. source.DatabaseCommands.PutAttachment("attach/1", null, new MemoryStream(new byte[] { 1, 2, 3 }), new RavenJObject());
  24. var attachment = WaitForAttachment(destination, "attach/1");
  25. Assert.NotNull(attachment);
  26. Assert.Equal(new byte[] { 1, 2, 3 }, attachment.Data().ReadData());
  27. source.DatabaseCommands.PutAttachment("attach/2", null, new MemoryStream(new byte[1024]), new RavenJObject());
  28. attachment = WaitForAttachment(destination, "attach/2");
  29. Assert.NotNull(attachment);
  30. Assert.Equal(1024, attachment.Data().ReadData().Length);
  31. }
  32. }
  33. [Fact]
  34. public void CanReplicateAttachmentDeletion()
  35. {
  36. using (var source = GetDocumentStore())
  37. using (var destination = GetDocumentStore())
  38. {
  39. SetupReplication(source, destinations: destination);
  40. source.DatabaseCommands.PutAttachment("attach/1", null, new MemoryStream(new byte[] { 1, 2, 3 }), new RavenJObject());
  41. var attachment = WaitForAttachment(destination, "attach/1");
  42. Assert.NotNull(attachment);
  43. source.DatabaseCommands.DeleteAttachment("attach/1", null);
  44. for (int i = 0; i < RetriesCount; i++)
  45. {
  46. if (destination.DatabaseCommands.GetAttachment("attach/1") == null)
  47. break;
  48. Thread.Sleep(100);
  49. }
  50. Assert.Null(destination.DatabaseCommands.GetAttachment("attach/1"));
  51. }
  52. }
  53. [Fact]
  54. public void ShouldCreateConflictThenResolveIt()
  55. {
  56. using (var source = GetDocumentStore())
  57. using (var destination = GetDocumentStore())
  58. {
  59. source.DatabaseCommands.PutAttachment("attach/1", null, new MemoryStream(new byte[] { 1, 2, 3 }), new RavenJObject());
  60. destination.DatabaseCommands.PutAttachment("attach/1", null, new MemoryStream(new byte[] { 3, 2, 1 }), new RavenJObject());
  61. SetupReplication(source, destinations: destination);
  62. source.DatabaseCommands.PutAttachment("marker", null, new MemoryStream(new byte[]{}), new RavenJObject());
  63. var marker = WaitForAttachment(destination, "marker");
  64. Assert.NotNull(marker);
  65. var conflictException = Assert.Throws<ConflictException>(() => destination.DatabaseCommands.GetAttachment("attach/1"));
  66. Assert.Equal("Conflict detected on attach/1, conflict must be resolved before the attachment will be accessible", conflictException.Message);
  67. Assert.True(conflictException.ConflictedVersionIds[0].StartsWith("attach/1/conflicts/"));
  68. Assert.True(conflictException.ConflictedVersionIds[1].StartsWith("attach/1/conflicts/"));
  69. // resolve by using first
  70. var resolution = destination.DatabaseCommands.GetAttachment(conflictException.ConflictedVersionIds[0]);
  71. destination.DatabaseCommands.PutAttachment("attach/1", null, resolution.Data(), resolution.Metadata);
  72. Assert.DoesNotThrow(() => destination.DatabaseCommands.GetAttachment("attach/1"));
  73. }
  74. }
  75. }
  76. }