public void WriteMessage_should_throw_if_message_is_null()
 {
     using (var stream = new MemoryStream())
     {
         var    subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
         Action action  = () => subject.WriteMessage(null);
         action.ShouldThrow <ArgumentNullException>();
     }
 }
 public void WriteMessage_should_write_a_message()
 {
     using (var stream = new MemoryStream())
     {
         var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
         subject.WriteMessage(__testMessage);
         var bytes = stream.ToArray();
         bytes.Should().Equal(__testMessageBytes);
     }
 }
Example #3
0
 public void WriteMessage_should_throw_if_binaryWriter_was_not_provided()
 {
     using (var stream = new MemoryStream())
         using (var binaryReader = new BsonBinaryReader(stream))
         {
             var    subject = new UpdateMessageBinaryEncoder(binaryReader, null);
             Action action  = () => subject.WriteMessage(__testMessage);
             action.ShouldThrow <InvalidOperationException>();
         }
 }
        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);
            }
        }
Example #6
0
        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);
                }
        }
 public void ReadMessage_should_read_a_message()
 {
     using (var stream = new MemoryStream(__testMessageBytes))
     {
         var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
         var message = subject.ReadMessage();
         message.CollectionNamespace.Should().Be(__collectionNamespace);
         message.IsMulti.Should().Be(__isMulti);
         message.IsUpsert.Should().Be(__isUpsert);
         message.Query.Should().Be(__query);
         message.RequestId.Should().Be(__requestId);
         message.Update.Should().Be(__update);
     }
 }
Example #8
0
        public void ReadMessage_should_throw_when_opcode_is_invalid()
        {
            var bytes = (byte[])__testMessageBytes.Clone();

            bytes[12]++;

            using (var stream = new MemoryStream(bytes))
            {
                var subject   = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
                var exception = Record.Exception(() => subject.ReadMessage());
                exception.Should().BeOfType <FormatException>();
                exception.Message.Should().Be("Update message opcode is not OP_UPDATE.");
            }
        }
        public void ReadMessage_should_decode_flags_correctly(int flags, bool isUpsert, bool isMulti)
        {
            var bytes = (byte[])__testMessageBytes.Clone();

            bytes[__flagsOffset] = (byte)flags;

            using (var stream = new MemoryStream(bytes))
                using (var binaryReader = new BsonBinaryReader(stream))
                {
                    var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
                    var message = subject.ReadMessage();
                    message.IsMulti.Should().Be(isMulti);
                    message.IsUpsert.Should().Be(isUpsert);
                }
        }
Example #10
0
 public void ReadMessage_should_read_a_message()
 {
     using (var stream = new MemoryStream(__testMessageBytes))
         using (var binaryReader = new BsonBinaryReader(stream))
         {
             var subject = new UpdateMessageBinaryEncoder(binaryReader, null);
             var message = subject.ReadMessage();
             message.DatabaseName.Should().Be(__databaseName);
             message.CollectionName.Should().Be(__collectionName);
             message.IsMulti.Should().Be(__isMulti);
             message.IsUpsert.Should().Be(__isUpsert);
             message.Query.Should().Be(__query);
             message.RequestId.Should().Be(__requestId);
             message.Update.Should().Be(__update);
         }
 }
        public void ReadMessage_should_decode_flags_correctly(int flags, bool isUpsert, bool isMulti)
        {
            var bytes = (byte[])__testMessageBytes.Clone();
            bytes[__flagsOffset] = (byte)flags;

            using (var stream = new MemoryStream(bytes))
            using (var binaryReader = new BsonBinaryReader(stream))
            {
                var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
                var message = subject.ReadMessage();
                message.IsMulti.Should().Be(isMulti);
                message.IsUpsert.Should().Be(isUpsert);
            }
        }
 public void WriteMessage_should_write_a_message()
 {
     using (var stream = new MemoryStream())
     {
         var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
         subject.WriteMessage(__testMessage);
         var bytes = stream.ToArray();
         bytes.Should().Equal(__testMessageBytes);
     }
 }
 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);
            }
        }
 public void WriteMessage_should_throw_if_message_is_null()
 {
     using (var stream = new MemoryStream())
     {
         var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
         Action action = () => subject.WriteMessage(null);
         action.ShouldThrow<ArgumentNullException>();
     }
 }
 public void ReadMessage_should_read_a_message()
 {
     using (var stream = new MemoryStream(__testMessageBytes))
     {
         var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
         var message = subject.ReadMessage();
         message.CollectionNamespace.Should().Be(__collectionNamespace);
         message.IsMulti.Should().Be(__isMulti);
         message.IsUpsert.Should().Be(__isUpsert);
         message.Query.Should().Be(__query);
         message.RequestId.Should().Be(__requestId);
         message.Update.Should().Be(__update);
     }
 }
 public void ReadMessage_should_read_a_message()
 {
     using (var stream = new MemoryStream(__testMessageBytes))
     using (var binaryReader = new BsonBinaryReader(stream))
     {
         var subject = new UpdateMessageBinaryEncoder(binaryReader, null);
         var message = subject.ReadMessage();
         message.DatabaseName.Should().Be(__databaseName);
         message.CollectionName.Should().Be(__collectionName);
         message.IsMulti.Should().Be(__isMulti);
         message.IsUpsert.Should().Be(__isUpsert);
         message.Query.Should().Be(__query);
         message.RequestId.Should().Be(__requestId);
         message.Update.Should().Be(__update);
     }
 }
        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);
            }
        }
 public void WriteMessage_should_throw_if_binaryWriter_was_not_provided()
 {
     using (var stream = new MemoryStream())
     using (var binaryReader = new BsonBinaryReader(stream))
     {
         var subject = new UpdateMessageBinaryEncoder(binaryReader, null);
         Action action = () => subject.WriteMessage(__testMessage);
         action.ShouldThrow<InvalidOperationException>();
     }
 }
        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
                });
            }
        }