public void ExecuteAsync_should_wait_for_response_when_CommandResponseHandling_is_Return() { var messageEncoderSettings = new MessageEncoderSettings(); var subject = new CommandWireProtocol <BsonDocument>( NoCoreSession.Instance, ReadPreference.Primary, new DatabaseNamespace("test"), new BsonDocument("cmd", 1), null, // commandPayloads NoOpElementNameValidator.Instance, null, // additionalOptions null, // postWriteAction CommandResponseHandling.Return, BsonDocumentSerializer.Instance, messageEncoderSettings); var mockConnection = new Mock <IConnection>(); var commandResponse = MessageHelper.BuildReply(CreateRawBsonDocument(new BsonDocument("ok", 1))); mockConnection .Setup(c => c.ReceiveMessageAsync(It.IsAny <int>(), It.IsAny <IMessageEncoderSelector>(), messageEncoderSettings, CancellationToken.None)) .Returns(Task.FromResult <ResponseMessage>(commandResponse)); var result = subject.ExecuteAsync(mockConnection.Object, CancellationToken.None).GetAwaiter().GetResult(); result.Should().Be("{ok: 1}"); }
public void ExecuteAsync_should_not_wait_for_response_when_CommandResponseHandling_is_NoResponseExpected() { var messageEncoderSettings = new MessageEncoderSettings(); var subject = new CommandWireProtocol <BsonDocument>( NoCoreSession.Instance, ReadPreference.Primary, new DatabaseNamespace("test"), new BsonDocument("cmd", 1), null, // commandPayloads NoOpElementNameValidator.Instance, null, // additionalOptions null, // postWriteAction CommandResponseHandling.NoResponseExpected, BsonDocumentSerializer.Instance, messageEncoderSettings, null); // serverApi var mockConnection = new Mock <IConnection>(); var result = subject.ExecuteAsync(mockConnection.Object, CancellationToken.None).GetAwaiter().GetResult(); result.Should().BeNull(); mockConnection.Verify(c => c.ReceiveMessageAsync(It.IsAny <int>(), It.IsAny <IMessageEncoderSelector>(), messageEncoderSettings, CancellationToken.None), Times.Once); }
public void Execute_should_use_serverApi_in_transaction( [Values(false, true)] bool useServerApi, [Values(false, true)] bool async) { var serverApi = useServerApi ? new ServerApi(ServerApiVersion.V1, true, true) : null; var connection = new MockConnection(); connection.Description = __connectionDescription; var commandResponse = MessageHelper.BuildCommandResponse(CreateRawBsonDocument(new BsonDocument("ok", 1))); connection.EnqueueCommandResponseMessage(commandResponse); var subject = new CommandWireProtocol <BsonDocument>( CreateMockSessionInTransaction(), ReadPreference.Primary, new DatabaseNamespace("test"), new BsonDocument("moreGet", 1), commandPayloads: null, NoOpElementNameValidator.Instance, additionalOptions: null, postWriteAction: null, CommandResponseHandling.Return, BsonDocumentSerializer.Instance, new MessageEncoderSettings(), serverApi); if (async) { subject.ExecuteAsync(connection, CancellationToken.None).GetAwaiter().GetResult(); } else { subject.Execute(connection, CancellationToken.None); } SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 1, TimeSpan.FromSeconds(4)).Should().BeTrue(); var sentMessages = MessageHelper.TranslateMessagesToBsonDocuments(connection.GetSentMessages()); sentMessages.Count.Should().Be(1); var actualRequestId = sentMessages[0]["requestId"].AsInt32; var expectedServerApiString = useServerApi ? ", apiVersion : \"1\", apiStrict : true, apiDeprecationErrors : true" : ""; sentMessages[0].Should().Be($"{{ opcode : \"opmsg\", requestId : {actualRequestId}, responseTo : 0, sections : [ {{ payloadType : 0, document : {{ moreGet : 1, $db : \"test\", txnNumber : NumberLong(1), autocommit : false{expectedServerApiString} }} }} ] }}"); ICoreSession CreateMockSessionInTransaction() { var transaction = new CoreTransaction(1, new TransactionOptions()); transaction.SetState(CoreTransactionState.InProgress); var mockSession = new Mock <ICoreSession>(); mockSession.SetupGet(m => m.CurrentTransaction).Returns(transaction); mockSession.SetupGet(m => m.IsInTransaction).Returns(true); return(mockSession.Object); } }
public void ExecuteAsync_should_not_wait_for_response_when_CommandResponseHandling_is_Ignore() { var subject = new CommandWireProtocol <BsonDocument>( new DatabaseNamespace("test"), new BsonDocument("cmd", 1), NoOpElementNameValidator.Instance, () => CommandResponseHandling.Ignore, true, BsonDocumentSerializer.Instance, new MessageEncoderSettings()); var connection = Substitute.For <IConnection>(); var result = subject.ExecuteAsync(connection, CancellationToken.None).GetAwaiter().GetResult(); result.Should().BeNull(); connection.ReceivedWithAnyArgs().ReceiveMessageAsync(0, null, null, CancellationToken.None); }
public void ExecuteAsync_should_not_wait_for_response_when_CommandResponseHandling_is_Ignore() { var messageEncoderSettings = new MessageEncoderSettings(); var subject = new CommandWireProtocol <BsonDocument>( new DatabaseNamespace("test"), new BsonDocument("cmd", 1), NoOpElementNameValidator.Instance, () => CommandResponseHandling.Ignore, true, BsonDocumentSerializer.Instance, messageEncoderSettings); var mockConnection = new Mock <IConnection>(); var result = subject.ExecuteAsync(mockConnection.Object, CancellationToken.None).GetAwaiter().GetResult(); result.Should().BeNull(); mockConnection.Verify(c => c.ReceiveMessageAsync(It.IsAny <int>(), It.IsAny <IMessageEncoderSelector>(), messageEncoderSettings, CancellationToken.None), Times.Once); }
public void ExecuteAsync_should_wait_for_response_when_CommandResponseHandling_is_Return() { var subject = new CommandWireProtocol <BsonDocument>( new DatabaseNamespace("test"), new BsonDocument("cmd", 1), NoOpElementNameValidator.Instance, () => CommandResponseHandling.Return, true, BsonDocumentSerializer.Instance, new MessageEncoderSettings()); var connection = Substitute.For <IConnection>(); var cmdResponse = MessageHelper.BuildReply(CreateRawBsonDocument(new BsonDocument("ok", 1))); connection.ReceiveMessageAsync(0, null, null, CancellationToken.None).ReturnsForAnyArgs(Task.FromResult <ResponseMessage>(cmdResponse)); var result = subject.ExecuteAsync(connection, CancellationToken.None).GetAwaiter().GetResult(); result.Should().Be("{ok: 1}"); }