PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/WireProtocol/CommandWriteProtocolTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 162 lines | 129 code | 19 blank | 14 comment | 0 complexity | 80a352dd22f9574aba7ccb692f8cebc0 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2015-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using FluentAssertions;
  23. using MongoDB.Bson;
  24. using MongoDB.Bson.IO;
  25. using MongoDB.Bson.Serialization;
  26. using MongoDB.Bson.Serialization.Serializers;
  27. using MongoDB.Driver.Core.Bindings;
  28. using MongoDB.Driver.Core.Connections;
  29. using MongoDB.Driver.Core.Helpers;
  30. using MongoDB.Driver.Core.WireProtocol.Messages;
  31. using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
  32. using Moq;
  33. using Xunit;
  34. namespace MongoDB.Driver.Core.WireProtocol
  35. {
  36. public class CommandWriteProtocolTests
  37. {
  38. [Fact]
  39. public void Execute_should_wait_for_response_when_CommandResponseHandling_is_Return()
  40. {
  41. var messageEncoderSettings = new MessageEncoderSettings();
  42. var subject = new CommandWireProtocol<BsonDocument>(
  43. NoCoreSession.Instance,
  44. ReadPreference.Primary,
  45. new DatabaseNamespace("test"),
  46. new BsonDocument("cmd", 1),
  47. null, // commandPayloads
  48. NoOpElementNameValidator.Instance,
  49. null, // additionalOptions
  50. null, // postWriteAction
  51. CommandResponseHandling.Return,
  52. BsonDocumentSerializer.Instance,
  53. messageEncoderSettings);
  54. var mockConnection = new Mock<IConnection>();
  55. var commandResponse = MessageHelper.BuildReply(CreateRawBsonDocument(new BsonDocument("ok", 1)));
  56. mockConnection
  57. .Setup(c => c.ReceiveMessage(It.IsAny<int>(), It.IsAny<IMessageEncoderSelector>(), messageEncoderSettings, CancellationToken.None))
  58. .Returns(commandResponse);
  59. var result = subject.Execute(mockConnection.Object, CancellationToken.None);
  60. result.Should().Be("{ok: 1}");
  61. }
  62. [Fact]
  63. public void Execute_should_not_wait_for_response_when_CommandResponseHandling_is_NoResponseExpected()
  64. {
  65. var messageEncoderSettings = new MessageEncoderSettings();
  66. var subject = new CommandWireProtocol<BsonDocument>(
  67. NoCoreSession.Instance,
  68. ReadPreference.Primary,
  69. new DatabaseNamespace("test"),
  70. new BsonDocument("cmd", 1),
  71. null, // commandPayloads
  72. NoOpElementNameValidator.Instance,
  73. null, // additionalOptions
  74. null, // postWriteAction
  75. CommandResponseHandling.NoResponseExpected,
  76. BsonDocumentSerializer.Instance,
  77. messageEncoderSettings);
  78. var mockConnection = new Mock<IConnection>();
  79. var result = subject.Execute(mockConnection.Object, CancellationToken.None);
  80. result.Should().BeNull();
  81. mockConnection.Verify(
  82. c => c.ReceiveMessageAsync(It.IsAny<int>(), It.IsAny<IMessageEncoderSelector>(), messageEncoderSettings, CancellationToken.None),
  83. Times.Once);
  84. }
  85. [Fact]
  86. public void ExecuteAsync_should_wait_for_response_when_CommandResponseHandling_is_Return()
  87. {
  88. var messageEncoderSettings = new MessageEncoderSettings();
  89. var subject = new CommandWireProtocol<BsonDocument>(
  90. NoCoreSession.Instance,
  91. ReadPreference.Primary,
  92. new DatabaseNamespace("test"),
  93. new BsonDocument("cmd", 1),
  94. null, // commandPayloads
  95. NoOpElementNameValidator.Instance,
  96. null, // additionalOptions
  97. null, // postWriteAction
  98. CommandResponseHandling.Return,
  99. BsonDocumentSerializer.Instance,
  100. messageEncoderSettings);
  101. var mockConnection = new Mock<IConnection>();
  102. var commandResponse = MessageHelper.BuildReply(CreateRawBsonDocument(new BsonDocument("ok", 1)));
  103. mockConnection
  104. .Setup(c => c.ReceiveMessageAsync(It.IsAny<int>(), It.IsAny<IMessageEncoderSelector>(), messageEncoderSettings, CancellationToken.None))
  105. .Returns(Task.FromResult<ResponseMessage>(commandResponse));
  106. var result = subject.ExecuteAsync(mockConnection.Object, CancellationToken.None).GetAwaiter().GetResult();
  107. result.Should().Be("{ok: 1}");
  108. }
  109. [Fact]
  110. public void ExecuteAsync_should_not_wait_for_response_when_CommandResponseHandling_is_NoResponseExpected()
  111. {
  112. var messageEncoderSettings = new MessageEncoderSettings();
  113. var subject = new CommandWireProtocol<BsonDocument>(
  114. NoCoreSession.Instance,
  115. ReadPreference.Primary,
  116. new DatabaseNamespace("test"),
  117. new BsonDocument("cmd", 1),
  118. null, // commandPayloads
  119. NoOpElementNameValidator.Instance,
  120. null, // additionalOptions
  121. null, // postWriteAction
  122. CommandResponseHandling.NoResponseExpected,
  123. BsonDocumentSerializer.Instance,
  124. messageEncoderSettings);
  125. var mockConnection = new Mock<IConnection>();
  126. var result = subject.ExecuteAsync(mockConnection.Object, CancellationToken.None).GetAwaiter().GetResult();
  127. result.Should().BeNull();
  128. mockConnection.Verify(c => c.ReceiveMessageAsync(It.IsAny<int>(), It.IsAny<IMessageEncoderSelector>(), messageEncoderSettings, CancellationToken.None), Times.Once);
  129. }
  130. private RawBsonDocument CreateRawBsonDocument(BsonDocument doc)
  131. {
  132. using (var memoryStream = new MemoryStream())
  133. {
  134. using (var bsonWriter = new BsonBinaryWriter(memoryStream, BsonBinaryWriterSettings.Defaults))
  135. {
  136. var context = BsonSerializationContext.CreateRoot(bsonWriter);
  137. BsonDocumentSerializer.Instance.Serialize(context, doc);
  138. }
  139. return new RawBsonDocument(memoryStream.ToArray());
  140. }
  141. }
  142. }
  143. }