<commit_before>/* test.c */

#include "test.h"
#include "mongo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef SEED_START_PORT
#define SEED_START_PORT 30000
#endif

int test_connect( const char *set_name ) {

    mongo conn[1];
    int res;

    INIT_SOCKETS_FOR_WINDOWS;

    mongo_replset_init( conn, set_name );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );

    res = mongo_replset_connect( conn );

    if( res != MONGO_OK ) {
        res = conn->err;
        return res;
    }

    ASSERT( conn->primary->port == SEED_START_PORT ||
       conn->primary->port == SEED_START_PORT + 1 ||
       conn->primary->port == SEED_START_PORT + 2 );

    mongo_destroy( conn );
    return res;
}

int test_reconnect( const char *set_name ) {

    mongo conn[1];
    int res;
    int e = 0;
    bson b;

    INIT_SOCKETS_FOR_WINDOWS;

    mongo_replset_init( conn, set_name );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );

    if( ( mongo_replset_connect( conn ) != MONGO_OK ) ) {
        mongo_destroy( conn );
        return res;
    } else {
        fprintf( stderr, "Disconnect now:\n" );
        sleep( 10 );
        e = 1;
        do {
            res = mongo_find_one( conn, "foo.bar", bson_empty( &b ), bson_empty( &b ), NULL );
            if( res == MONGO_ERROR && conn->err == MONGO_IO_ERROR ) {
                sleep( 2 );
                if( e++ < 30 ) {
                    fprintf( stderr, "Attempting reconnect %d.\n", e );
                    mongo_reconnect( conn );
                } else {
                    fprintf( stderr, "Fail.\n" );
                    return -1;
                }
            }
        } while( 1 );
    }

    return 0;
}

int main() {
    ASSERT( test_connect( "test-rs" ) == MONGO_OK );
    ASSERT( test_connect( "test-foobar" ) == MONGO_CONN_BAD_SET_NAME );

    /*
    ASSERT( test_reconnect( "test-rs" ) == 0 );
    */

    return 0;
}<commit_msg>CDRIVER-36 verify insert limits on replica set connections<commit_after>/* test.c */

#include "test.h"
#include "mongo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#ifndef SEED_START_PORT
#define SEED_START_PORT 30000
#endif

int test_connect( const char *set_name ) {

    mongo conn[1];
    int res;

    INIT_SOCKETS_FOR_WINDOWS;

    mongo_replset_init( conn, set_name );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );

    res = mongo_replset_connect( conn );

    if( res != MONGO_OK ) {
        res = conn->err;
        return res;
    }

    ASSERT( conn->primary->port == SEED_START_PORT ||
       conn->primary->port == SEED_START_PORT + 1 ||
       conn->primary->port == SEED_START_PORT + 2 );

    mongo_destroy( conn );
    return res;
}

int test_reconnect( const char *set_name ) {

    mongo conn[1];
    int res;
    int e = 0;
    bson b;

    INIT_SOCKETS_FOR_WINDOWS;

    mongo_replset_init( conn, set_name );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );

    if( ( mongo_replset_connect( conn ) != MONGO_OK ) ) {
        mongo_destroy( conn );
        return res;
    } else {
        fprintf( stderr, "Disconnect now:\n" );
        sleep( 10 );
        e = 1;
        do {
            res = mongo_find_one( conn, "foo.bar", bson_empty( &b ), bson_empty( &b ), NULL );
            if( res == MONGO_ERROR && conn->err == MONGO_IO_ERROR ) {
                sleep( 2 );
                if( e++ < 30 ) {
                    fprintf( stderr, "Attempting reconnect %d.\n", e );
                    mongo_reconnect( conn );
                } else {
                    fprintf( stderr, "Fail.\n" );
                    return -1;
                }
            }
        } while( 1 );
    }

    return 0;
}

int test_insert_limits( const char *set_name ) {
    char version[10];
    mongo conn[1];
    int i;
    char key[10];
    int res = 0;
    bson b[1], b2[1];
    bson *objs[2];

    /* We'll perform the full test if we're running v2.0 or later. */
    if( mongo_get_server_version( version ) != -1 && version[0] <= '1' )
        return 0;

    mongo_replset_init( conn, set_name );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
    mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
    res = mongo_replset_connect( conn );

    if( res != MONGO_OK ) {
        res = conn->err;
        return res;
    }

    ASSERT( conn->max_bson_size > MONGO_DEFAULT_MAX_BSON_SIZE );

    bson_init( b );
    for(i=0; i<1200000; i++) {
        sprintf( key, "%d", i + 10000000 );
        bson_append_int( b, key, i );
    }
    bson_finish( b );

    ASSERT( bson_size( b ) > conn->max_bson_size );

    ASSERT( mongo_insert( conn, "test.foo", b ) == MONGO_ERROR );
    ASSERT( conn->err == MONGO_BSON_TOO_LARGE );

    mongo_clear_stored_errors( conn );
    ASSERT( conn->err == 0 );

    bson_init( b2 );
    bson_append_int( b2, "foo", 1 );
    bson_finish( b2 );

    objs[0] = b;
    objs[1] = b2;

    ASSERT( mongo_insert_batch( conn, "test.foo", (const bson**)objs, 2 ) == MONGO_ERROR );
    ASSERT( conn->err == MONGO_BSON_TOO_LARGE );

    return 0;
}

int main() {
    ASSERT( test_connect( "replica-set-foo" ) == MONGO_OK );
    ASSERT( test_connect( "test-foobar" ) == MONGO_CONN_BAD_SET_NAME );
    ASSERT( test_insert_limits( "replica-set-foo" ) == MONGO_OK );

    /*
    ASSERT( test_reconnect( "test-rs" ) == 0 );
    */

    return 0;
}
{'fraction_non_alphanumeric': 0.13178158907945398, 'fraction_numerical': 0.01592579628981449, 'mean_word_length': 2.269450800915332, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3065647', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2070, 'n_words': 459}