public void Execute_should_create_collection_when_Capped_is_set(
            [Values(false, true)]
            bool capped,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var maxSize = capped ? (long?)10000 : null;
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped  = capped,
                MaxSize = maxSize
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var stats = GetCollectionStats(binding, async);
                stats["ns"].ToString().Should().Be(_collectionNamespace.FullName);
                stats.GetValue("capped", false).ToBoolean().Should().Be(capped);
            }
        }
        public void SocketConfigurator_can_be_used_to_set_keepAlive(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            Action <Socket> socketConfigurator = s => s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
            var             settings           = new TcpStreamSettings(socketConfigurator: socketConfigurator);
            var             subject            = new TcpStreamFactory(settings);
            var             endPoint           = CoreTestConfiguration.ConnectionString.Hosts[0];

            Stream stream;

            if (async)
            {
                stream = subject.CreateStreamAsync(endPoint, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                stream = subject.CreateStream(endPoint, CancellationToken.None);
            }

            var socketProperty = typeof(NetworkStream).GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
            var socket         = (Socket)socketProperty.GetValue(stream);
            var keepAlive      = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive);

            keepAlive.Should().NotBe(0); // .NET returns 1 but Mono returns 8
        }
        public void Execute_against_a_non_existing_document_returning_the_original(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new FindOneAndUpdateOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocument.Parse("{alkjasdf: 10}"),
                _update,
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                BypassDocumentValidation = true,
                ReturnDocument           = ReturnDocument.Before,
            };

            var result = ExecuteOperation(subject, async);

            result.Should().BeNull();

            var serverList = ReadAllFromCollection(async);

            serverList[0].Should().Be("{_id: 10, x: 1}");
        }
        public void Execute_against_a_non_existing_document_returning_the_updated_with_upsert(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var subject = new FindOneAndUpdateOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocument.Parse("{_id: 10}"),
                _update,
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                BypassDocumentValidation = true,
                IsUpsert       = true,
                ReturnDocument = ReturnDocument.After,
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be("{_id: 10, x: 2}");

            var serverList = ReadAllFromCollection(async);

            serverList[0].Should().Be("{_id: 10, x: 2}");
        }
        public void Execute_should_create_collection_when_MaxSize_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var maxSize = 10000L;
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped  = true,
                MaxSize = maxSize
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var stats = GetCollectionStats(binding, async);
                stats["ns"].ToString().Should().Be(_collectionNamespace.FullName);
                stats["capped"].ToBoolean().Should().BeTrue();
                // TODO: not sure how to verify that the maxSize took effect
            }
        }
        public void Execute_against_an_existing_document_returning_the_replacement(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new FindOneAndReplaceOperation <BsonDocument>(
                _collectionNamespace,
                _filter,
                _replacement,
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                BypassDocumentValidation = true,
                ReturnDocument           = ReturnDocument.After
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be("{_id: 10, a: 2}");

            var serverList = ReadAllFromCollection(async);

            serverList[0].Should().Be("{_id: 10, a: 2}");
        }
        public void Execute_should_create_collection_when_MaxDocuments_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var maxDocuments = 123L;
            var subject      = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped       = true,
                MaxSize      = 10000L,
                MaxDocuments = maxDocuments
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var stats = GetCollectionStats(binding, async);
                stats["ns"].ToString().Should().Be(_collectionNamespace.FullName);
                stats["capped"].ToBoolean().Should().BeTrue();
                stats["max"].ToInt64().Should().Be(maxDocuments);
            }
        }
        public void Execute_should_create_collection_when_AutoIndexId_is_set(
            [Values(false, true)]
            bool autoIndexId,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                AutoIndexId = autoIndexId
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var listIndexesOperation = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
                List <BsonDocument> indexes;
                if (async)
                {
                    var cursor = listIndexesOperation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult();
                    indexes = cursor.ToListAsync().GetAwaiter().GetResult();
                }
                else
                {
                    var cursor = listIndexesOperation.Execute(binding, CancellationToken.None);
                    indexes = cursor.ToList();
                }

                indexes.Count.Should().Be(autoIndexId ? 1 : 0);
            }
        }
Beispiel #9
0
 public void Execute_should_return_expected_result_when_maxTime_is_provided(
     [Values(false, true)]
     bool async)
 {
     RequireServer.Any();
     EnsureTestData();
     if (SupportedFeatures.AreFailPointsSupported(CoreTestConfiguration.ServerVersion))
     {
         // TODO: port FailPoint infrastructure from Driver.Tests to Core.Tests
     }
 }
Beispiel #10
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result.Should().Be(5);
        }
        public async Task ExecuteAsync_should_find_all_the_documents_matching_the_query()
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new FindOpcodeOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var cursor = await ExecuteOperationAsync(subject);

            var result = await ReadCursorToEndAsync(cursor);

            result.Should().HaveCount(5);
        }
Beispiel #12
0
        public void Execute_should_return_expected_result_when_database_does_not_exist(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropDatabase(async);
            var subject = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);
            var list   = ReadCursorToEnd(result, async);

            list.Count.Should().Be(0);
        }
Beispiel #13
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureCollectionExists(async);
            var subject = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);
            var list   = ReadCursorToEnd(result, async);

            list.Select(index => index["name"].AsString).Should().BeEquivalentTo("_id_");
        }
Beispiel #14
0
        public void Execute_should_find_all_the_documents_matching_the_query_when_read_preference_is_used(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var cursor = ExecuteOperation(subject, ReadPreference.PrimaryPreferred, async); // note: SecondaryPreferred doesn't test $readPreference because it is encoded as slaveOk = true
            var result = ReadCursorToEnd(cursor, async);

            result.Should().HaveCount(5);
        }
Beispiel #15
0
        public void Execute_should_find_all_the_documents_matching_the_query(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var cursor = ExecuteOperation(subject, async);
            var result = ReadCursorToEnd(cursor, async);

            result.Should().HaveCount(5);
        }
        public void Execute_should_return_the_expected_result_when_the_database_does_not_exist(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            var databaseNamespace = new DatabaseNamespace(_databaseNamespace.DatabaseName + "-not");
            var subject           = new ListCollectionsOperation(databaseNamespace, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);
            var list   = ReadCursorToEnd(result, async);

            list.Should().HaveCount(0);
        }
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureCollectionExists(_collectionNamespace, async);
            EnsureCollectionDoesNotExist(_newCollectionNamespace, async);
            var subject = new RenameCollectionOperation(_collectionNamespace, _newCollectionNamespace, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result["ok"].ToBoolean().Should().BeTrue();
        }
        public void Execute_should_not_throw_when_collection_does_not_exist(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var indexName = "x_1";
                var subject   = new DropIndexOperation(_collectionNamespace, indexName, _messageEncoderSettings);

                ExecuteOperation(subject, async); // should not throw
            }
        }
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            var subject = new ListDatabasesOperation(_messageEncoderSettings);

            EnsureDatabaseExists(async);

            var result = ExecuteOperation(subject, async);
            var list   = ReadCursorToEnd(result, async);

            list.Should().Contain(x => x["name"] == _databaseNamespace.DatabaseName);
        }
Beispiel #20
0
        public void Execute_should_return_expected_result_when_hint_is_provided(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);

            subject.Hint = BsonDocument.Parse("{ _id : 1 }");

            var result = ExecuteOperation(subject, async);

            result.Should().Be(5);
        }
Beispiel #21
0
        public void Execute_should_not_throw_when_collection_does_not_exist(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                var dropCollectionOperation = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                ExecuteOperation(dropCollectionOperation, binding, async);
                ExecuteOperation(subject, binding, async); // this will throw if we have a problem...
            }
        }
        public void Execute_should_return_the_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureCollectionsExist();
            var subject       = new ListCollectionsOperation(_databaseNamespace, _messageEncoderSettings);
            var expectedNames = new[] { "regular", "capped" };

            var result = ExecuteOperation(subject, async);
            var list   = ReadCursorToEnd(result, async);

            list.Count.Should().BeGreaterThan(0);
            list.Select(c => c["name"].AsString).Where(n => n != "system.indexes").Should().BeEquivalentTo(expectedNames);
        }
        public void Execute_should_insert_a_single_document(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var subject = new InsertOpcodeOperation <BsonDocument>(_collectionNamespace, _documentSource, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result.Should().HaveCount(1);

            var list = ReadAllFromCollection(async);

            list.Should().HaveCount(1);
        }
        public void Execute_should_return_the_expected_result_when_filter_is_used(string filterString, string expectedName, bool async)
        {
            RequireServer.Any();
            EnsureCollectionsExist();
            var filter  = BsonDocument.Parse(filterString);
            var subject = new ListCollectionsOperation(_databaseNamespace, _messageEncoderSettings)
            {
                Filter = filter
            };

            var result = ExecuteOperation(subject, async);
            var list   = ReadCursorToEnd(result, async);

            list.Should().HaveCount(1);
            list[0]["name"].AsString.Should().Be(expectedName);
        }
Beispiel #25
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                EnsureDatabaseExists(binding);
                var subject = new DropDatabaseOperation(_databaseNamespace, _messageEncoderSettings);

                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();
            }
        }
        public void Execute_should_work_when_creating_one_index(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var subject  = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result["ok"].ToBoolean().Should().BeTrue();

            var indexes = ListIndexes(async);

            indexes.Select(index => index["name"].AsString).Should().BeEquivalentTo(new[] { "_id_", "x_1" });
        }
        public void Execute_should_throw_when_dropTarget_is_false_and_newCollectionNamespace_exists(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            var subject = new RenameCollectionOperation(_collectionNamespace, _newCollectionNamespace, _messageEncoderSettings)
            {
                DropTarget = false
            };

            EnsureCollectionExists(_collectionNamespace, async);
            EnsureCollectionExists(_newCollectionNamespace, async);

            Action action = () => ExecuteOperation(subject, async);

            action.ShouldThrow <MongoCommandException>();
        }
Beispiel #28
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new GeoNearOperation <BsonDocument>(
                _collectionNamespace,
                new BsonArray {
                1, 2
            },
                BsonDocumentSerializer.Instance,
                _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result["results"].Should().NotBeNull();
        }
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var keys                 = new BsonDocument("x", 1);
            var requests             = new[] { new CreateIndexRequest(keys) };
            var createIndexOperation = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            ExecuteOperation(createIndexOperation, async);
            var indexName = "x_1";
            var subject   = new DropIndexOperation(_collectionNamespace, indexName, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result["ok"].ToBoolean().Should().BeTrue();
        }
Beispiel #30
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                var createCollectionOperation = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                ExecuteOperation(createCollectionOperation, binding, async);

                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();
                result["ns"].ToString().Should().Be(_collectionNamespace.FullName);
            }
        }