<commit_before># Copyright 2012-2014 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities for testing pymongo
"""

import contextlib
import os
import struct
import sys
import threading
import time
import warnings
from functools import partial

from pymongo import MongoClient
from pymongo.errors import AutoReconnect, OperationFailure
from pymongo.server_selectors import (any_server_selector,
                                      writable_server_selector)
from test import (client_context,
                  db_user,
                  db_pwd,
                  host,
                  port)
from test.version import Version


def _connection_string_noauth(h, p):
    if h.startswith("mongodb://"):
        return h
    return "mongodb://%s:%d" % (h, p)


def _connection_string(h, p):
    if h.startswith("mongodb://"):
        return h
    elif client_context.auth_enabled:
        return "mongodb://%s:%s@%s:%d" % (db_user, db_pwd, h, p)
    else:
        return _connection_string_noauth(h, p)


def single_client_noauth(h=host, p=port, **kwargs):
    """Make a direct connection. Don't authenticate."""
    return MongoClient(_connection_string_noauth(h, p), **kwargs)


def single_client(h=host, p=port, **kwargs):
    """Make a direct connection, and authenticate if necessary."""
    return MongoClient(_connection_string(h, p), **kwargs)


def rs_client_noauth(h=host, p=port, **kwargs):
    """Connect to the replica set. Don't authenticate."""
    return MongoClient(_connection_string_noauth(h, p),
                       replicaSet=client_context.replica_set_name, **kwargs)


def rs_client(h=host, p=port, **kwargs):
    """Connect to the replica set and authenticate if necessary."""
    return MongoClient(_connection_string(h, p),
                       replicaSet=client_context.replica_set_name, **kwargs)


def rs_or_single_client_noauth(h=host, p=port, **kwargs):
    """Connect to the replica set if there is one, otherwise the standalone.

    Like rs_or_single_client, but does not authenticate.
    """
    if client_context.replica_set_name:
        return rs_client_noauth(h, p, **kwargs)
    else:
        return single_client_noauth(h, p, **kwargs)


def rs_or_single_client(h=host, p=port, **kwargs):
    """Connect to the replica set if there is one, otherwise the standalone.

    Authenticates if necessary.
    """
    if client_context.replica_set_name:
        return rs_client(h, p, **kwargs)
    else:
        return single_client(h, p, **kwargs)


def one(s):
    """Get one element of a set"""
    return next(iter(s))


def oid_generated_on_client(oid):
    """Is this process's PID in this ObjectId?"""
    pid_from_doc = struct.unpack(">H", oid.binary[7:9])[0]
    return (os.getpid() % 0xFFFF) == pid_from_doc


def delay(sec):
    # Javascript sleep() only available in MongoDB since version ~1.9
    return '''function() {
        var d = new Date((new Date()).getTime() + %s * 1000);
        while (d > (new Date())) { }; return true;
    }''' % sec


def get_command_line(client):
    command_line = client.admin.command('getCmdLineOpts')
    assert command_line['ok'] == 1, "getCmdLineOpts() failed"
    return command_line


def server_started_with_option(client, cmdline_opt, config_opt):
    """Check if the server was started with a particular option.
    
    :Parameters:
      - `cmdline_opt`: The command line option (i.e. --nojournal)
      - `config_opt`: The config file option (i.e. nojournal)
    """
    command_line = get_command_line(client)
    if 'parsed' in command_line:
        parsed = command_line['parsed']
        if config_opt in parsed:
            return parsed[config_opt]
    argv = command_line['argv']<commit_msg>Tests can rely on Javascript sleep() function.<commit_after># Copyright 2012-2014 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities for testing pymongo
"""

import contextlib
import os
import struct
import sys
import threading
import time
import warnings
from functools import partial

from pymongo import MongoClient
from pymongo.errors import AutoReconnect, OperationFailure
from pymongo.server_selectors import (any_server_selector,
                                      writable_server_selector)
from test import (client_context,
                  db_user,
                  db_pwd,
                  host,
                  port)
from test.version import Version


def _connection_string_noauth(h, p):
    if h.startswith("mongodb://"):
        return h
    return "mongodb://%s:%d" % (h, p)


def _connection_string(h, p):
    if h.startswith("mongodb://"):
        return h
    elif client_context.auth_enabled:
        return "mongodb://%s:%s@%s:%d" % (db_user, db_pwd, h, p)
    else:
        return _connection_string_noauth(h, p)


def single_client_noauth(h=host, p=port, **kwargs):
    """Make a direct connection. Don't authenticate."""
    return MongoClient(_connection_string_noauth(h, p), **kwargs)


def single_client(h=host, p=port, **kwargs):
    """Make a direct connection, and authenticate if necessary."""
    return MongoClient(_connection_string(h, p), **kwargs)


def rs_client_noauth(h=host, p=port, **kwargs):
    """Connect to the replica set. Don't authenticate."""
    return MongoClient(_connection_string_noauth(h, p),
                       replicaSet=client_context.replica_set_name, **kwargs)


def rs_client(h=host, p=port, **kwargs):
    """Connect to the replica set and authenticate if necessary."""
    return MongoClient(_connection_string(h, p),
                       replicaSet=client_context.replica_set_name, **kwargs)


def rs_or_single_client_noauth(h=host, p=port, **kwargs):
    """Connect to the replica set if there is one, otherwise the standalone.

    Like rs_or_single_client, but does not authenticate.
    """
    if client_context.replica_set_name:
        return rs_client_noauth(h, p, **kwargs)
    else:
        return single_client_noauth(h, p, **kwargs)


def rs_or_single_client(h=host, p=port, **kwargs):
    """Connect to the replica set if there is one, otherwise the standalone.

    Authenticates if necessary.
    """
    if client_context.replica_set_name:
        return rs_client(h, p, **kwargs)
    else:
        return single_client(h, p, **kwargs)


def one(s):
    """Get one element of a set"""
    return next(iter(s))


def oid_generated_on_client(oid):
    """Is this process's PID in this ObjectId?"""
    pid_from_doc = struct.unpack(">H", oid.binary[7:9])[0]
    return (os.getpid() % 0xFFFF) == pid_from_doc


def delay(sec):
    return '''function() { sleep(%f * 1000); return true; }''' % sec


def get_command_line(client):
    command_line = client.admin.command('getCmdLineOpts')
    assert command_line['ok'] == 1, "getCmdLineOpts() failed"
    return command_line


def server_started_with_option(client, cmdline_opt, config_opt):
    """Check if the server was started with a particular option.
    
    :Parameters:
      - `cmdline_opt`: The command line option (i.e. --nojournal)
      - `config_opt`: The config file option (i.e. nojournal)
    """
    command_line = get_command_line(client)
    if 'parsed' in command_line:
        parsed = command_line['parsed']
        if config_opt in parsed:
            return parsed[config_opt]
    argv = command_line['argv']
{'fraction_non_alphanumeric': 0.10830020479460306, 'fraction_numerical': 0.005300566196843754, 'mean_word_length': 3.4019088016967127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2302461', 'n_tokens_mistral': 2594, 'n_tokens_neox': 2490, 'n_words': 781}