Esempio n. 1
0
 public void Constructor_should_initialize_instance(bool isMulti, bool isUpsert)
 {
     var subject = new UpdateMessage(_requestId, _collectionNamespace, _query, _update, _updateValidator, isMulti, isUpsert);
     subject.CollectionNamespace.Should().Be(_collectionNamespace);
     subject.IsMulti.Should().Be(isMulti);
     subject.IsUpsert.Should().Be(isUpsert);
     subject.Query.Should().Be(_query);
     subject.Update.Should().Be(_update);
     subject.RequestId.Should().Be(_requestId);
 }
        public void GetEncoder_should_return_encoder()
        {
            var mockEncoder = Substitute.For<IMessageEncoder<UpdateMessage>>();
            var mockEncoderFactory = Substitute.For<IMessageEncoderFactory>();
            mockEncoderFactory.GetUpdateMessageEncoder().Returns(mockEncoder);

            var subject = new UpdateMessage(_requestId, _databaseName, _collectionName, _query, _update, false, false);
            var encoder = subject.GetEncoder(mockEncoderFactory);
            encoder.Should().BeSameAs(mockEncoder);
        }
Esempio n. 3
0
        public void GetEncoder_should_return_encoder()
        {
            var subject = new UpdateMessage(_requestId, _collectionNamespace, _query, _update, _updateValidator, false, false);
            var mockEncoderFactory = new Mock<IMessageEncoderFactory>();
            var encoder = new Mock<IMessageEncoder>().Object;
            mockEncoderFactory.Setup(f => f.GetUpdateMessageEncoder()).Returns(encoder);

            var result = subject.GetEncoder(mockEncoderFactory.Object);

            result.Should().BeSameAs(encoder);
        }
        public void GetEncoder_should_return_encoder()
        {
            var subject = new UpdateMessage(_requestId, _collectionNamespace, _query, _update, _updateValidator, false, false);
            var stubEncoderFactory = Substitute.For<IMessageEncoderFactory>();
            var stubEncoder = Substitute.For<IMessageEncoder>();
            stubEncoderFactory.GetUpdateMessageEncoder().Returns(stubEncoder);

            var result = subject.GetEncoder(stubEncoderFactory);

            result.Should().BeSameAs(stubEncoder);
        }
        // static constructor
        static UpdateMessageJsonEncoderTests()
        {
            __testMessage = new UpdateMessage(__requestId, __collectionNamespace, __query, __update, _updateValidator, __isMulti, __isUpsert);

            __testMessageJson =
                "{ " +
                    "\"opcode\" : \"update\", " +
                    "\"requestId\" : 1, " +
                    "\"database\" : \"d\", " +
                    "\"collection\" : \"c\", " +
                    "\"isMulti\" : true, " +
                    "\"isUpsert\" : true, " +
                    "\"query\" : { \"x\" : 1 }, " +
                    "\"update\" : { \"y\" : 1 }" +
                " }";
        }
        // static constructor
        static UpdateMessageBinaryEncoderTests()
        {
            __testMessage = new UpdateMessage(__requestId, __collectionNamespace, __query, __update, __updateValidator, __isMulti, __isUpsert);

            __testMessageBytes = new byte[]
            {
                0, 0, 0, 0, // messageLength
                1, 0, 0, 0, // requestId
                0, 0, 0, 0, // responseTo
                209, 7, 0, 0, // opcode = 2001
                0, 0, 0, 0, // reserved
                (byte)'d', (byte)'.', (byte)'c', 0, // fullCollectionName
                3, 0, 0, 0, // flags
                12, 0, 0, 0, 0x10, (byte)'x', 0, 1, 0, 0, 0, 0, // query
                12, 0, 0, 0, 0x10, (byte)'y', 0, 1, 0, 0, 0, 0 // fields
            };
            __testMessageBytes[0] = (byte)__testMessageBytes.Length;
            __flagsOffset = 20 + (__collectionNamespace.FullName.Length + 1);
        }
 public void WriteMessage_should_throw_if_the_update_message_is_empty_when_using_the_UpdateElementNameValidator()
 {
     var message = new UpdateMessage(__requestId, __collectionNamespace, __query, new BsonDocument(), UpdateElementNameValidator.Instance, false, false);
     using (var stream = new MemoryStream())
     {
         var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
         Action act = () => subject.WriteMessage(message);
         act.ShouldThrow<BsonSerializationException>();
     }
 }
        public void WriteMessage_should_encode_flags_correctly(int flags, bool isUpsert, bool isMulti)
        {
            var message = new UpdateMessage(__requestId, __collectionNamespace, __query, __update, __updateValidator, isMulti, isUpsert);

            using (var stream = new MemoryStream())
            {
                var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
                subject.WriteMessage(message);
                var bytes = stream.ToArray();
                bytes[__flagsOffset].Should().Be((byte)flags);
            }
        }
        private void ProcessUpdateMessage(UpdateMessage originalMessage, Queue<RequestMessage> messageQueue, ConnectionId connectionId, UpdateMessageBinaryEncoder encoder, Stopwatch stopwatch)
        {
            var commandName = "update";
            int requestId = originalMessage.RequestId;
            var operationId = EventContext.OperationId;
            var expectedResponseType = ExpectedResponseType.None;
            BsonValue upsertedId = null;
            int gleRequestId;
            WriteConcern writeConcern;
            if (TryGetWriteConcernFromGLE(messageQueue, out gleRequestId, out writeConcern))
            {
                requestId = gleRequestId;
                expectedResponseType = ExpectedResponseType.GLE;
            }

            if (_startedEvent != null)
            {
                var decodedMessage = encoder.ReadMessage(RawBsonDocumentSerializer.Instance);
                try
                {
                    if (_shouldTrackState)
                    {
                        // GLE result on older versions of the server didn't return
                        // the upserted id when it wasn't an object id, so we'll
                        // attempt to get it from the messages.
                        if (!decodedMessage.Update.TryGetValue("_id", out upsertedId))
                        {
                            decodedMessage.Query.TryGetValue("_id", out upsertedId);
                        }
                    }

                    var entry = new BsonDocument
                    {
                        { "q", decodedMessage.Query },
                        { "u", decodedMessage.Update },
                        { "upsert", decodedMessage.IsUpsert },
                        { "multi", decodedMessage.IsMulti }
                    };
                    var command = new BsonDocument
                    {
                        { commandName, decodedMessage.CollectionNamespace.CollectionName },
                        { "updates", new BsonArray(new [] { entry }) }
                    };

                    if (writeConcern == null)
                    {
                        command["writeConcern"] = WriteConcern.Unacknowledged.ToBsonDocument();
                    }
                    else if (!writeConcern.IsServerDefault)
                    {
                        command["writeConcern"] = writeConcern.ToBsonDocument();
                    }

                    var @event = new CommandStartedEvent(
                        commandName,
                        command,
                        decodedMessage.CollectionNamespace.DatabaseNamespace,
                        operationId,
                        requestId,
                        connectionId);

                    _startedEvent(@event);
                }
                finally
                {
                    var disposable = decodedMessage.Query as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                    disposable = decodedMessage.Update as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
            }

            if (_shouldTrackState)
            {
                _state.TryAdd(requestId, new CommandState
                {
                    CommandName = commandName,
                    OperationId = operationId,
                    Stopwatch = Stopwatch.StartNew(),
                    ExpectedResponseType = expectedResponseType,
                    UpsertedId = upsertedId
                });
            }
        }
        public void WriteMessage_should_encode_flags_correctly(int flags, bool isUpsert, bool isMulti)
        {
            var message = new UpdateMessage(__requestId, __databaseName, __collectionName, __query, __update, isMulti, isUpsert);

            using (var stream = new MemoryStream())
            using (var binaryWriter = new BsonBinaryWriter(stream))
            {
                var subject = new UpdateMessageBinaryEncoder(null, binaryWriter);
                subject.WriteMessage(message);
                var bytes = stream.ToArray();
                bytes[__flagsOffset].Should().Be((byte)flags);
            }
        }