コード例 #1
0
        public void Should_process_a_query_with_modifiers()
        {
            var expectedCommand = new BsonDocument
            {
                { "find", MessageHelper.DefaultCollectionNamespace.CollectionName },
                { "projection", new BsonDocument("b", 1) },
                { "skip", 1 },
                { "batchSize", 2 },
                { "limit", 100 },
                { "awaitData", true },
                { "noCursorTimeout", true },
                { "allowPartialResults", true },
                { "tailable", true },
                { "oplogReplay", true },
                { "filter", new BsonDocument("x", 1) },
                { "sort", new BsonDocument("a", -1) },
                { "comment", "funny" },
                { "maxTimeMS", 20 },
                { "showRecordId", true },
            };
            var expectedReplyDocuments = new[]
            {
                new BsonDocument("first", 1),
                new BsonDocument("second", 2)
            };
            var expectedReply = new BsonDocument
            {
                { "cursor", new BsonDocument
                  {
                      { "id", 20L },
                      { "ns", MessageHelper.DefaultCollectionNamespace.FullName },
                      { "firstBatch", new BsonArray(expectedReplyDocuments) }
                  } },
                { "ok", 1 }
            };
            var query = new BsonDocument
            {
                { "$query", expectedCommand["filter"] },
                { "$orderby", expectedCommand["sort"] },
                { "$comment", expectedCommand["comment"] },
                { "$maxTimeMS", expectedCommand["maxTimeMS"] },
                { "$showDiskLoc", expectedCommand["showRecordId"] },
            };
            var requestMessage = MessageHelper.BuildQuery(
                query,
                fields: (BsonDocument)expectedCommand["projection"],
                requestId: 10,
                skip: expectedCommand["skip"].ToInt32(),
                batchSize: expectedCommand["batchSize"].ToInt32(),
                noCursorTimeout: expectedCommand["noCursorTimeout"].ToBoolean(),
                awaitData: expectedCommand["awaitData"].ToBoolean(),
                partialOk: expectedCommand["allowPartialResults"].ToBoolean(),
                tailableCursor: expectedCommand["tailable"].ToBoolean(),
                oplogReplay: expectedCommand["oplogReplay"].ToBoolean());

            using (EventContext.BeginFind(expectedCommand["batchSize"].ToInt32(), expectedCommand["limit"].ToInt32()))
            {
                SendMessages(requestMessage);
            }

            var replyMessage = MessageHelper.BuildReply <BsonDocument>(
                expectedReplyDocuments,
                BsonDocumentSerializer.Instance,
                responseTo: requestMessage.RequestId,
                cursorId: expectedReply["cursor"]["id"].ToInt64());

            ReceiveMessages(replyMessage);

            var commandStartedEvent   = (CommandStartedEvent)_capturedEvents.Next();
            var commandSucceededEvent = (CommandSucceededEvent)_capturedEvents.Next();

            commandStartedEvent.CommandName.Should().Be(expectedCommand.GetElement(0).Name);
            commandStartedEvent.Command.Should().Be(expectedCommand);
            commandStartedEvent.ConnectionId.Should().Be(_subject.ConnectionId);
            commandStartedEvent.DatabaseNamespace.Should().Be(MessageHelper.DefaultDatabaseNamespace);
            commandStartedEvent.OperationId.Should().Be(EventContext.OperationId);
            commandStartedEvent.RequestId.Should().Be(requestMessage.RequestId);

            commandSucceededEvent.CommandName.Should().Be(commandStartedEvent.CommandName);
            commandSucceededEvent.ConnectionId.Should().Be(commandStartedEvent.ConnectionId);
            commandSucceededEvent.Duration.Should().BeGreaterThan(TimeSpan.Zero);
            commandSucceededEvent.OperationId.Should().Be(commandStartedEvent.OperationId);
            commandSucceededEvent.Reply.Should().Be(expectedReply);
            commandSucceededEvent.RequestId.Should().Be(commandStartedEvent.RequestId);
        }