1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
#!/usr/bin/env python
#
# Copyright 2015 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.
"""Generate test functions for use with mock_server_t.
Defines functions like future_cursor_next in future-functions.h and
future-functions.c, which defer a libmongoc operation to a background thread
via functions like background_cursor_next. Also defines functions like
future_value_set_bson_ptr and future_value_get_bson_ptr which support the
future / background functions, and functions like future_get_bson_ptr which
wait for a future to resolve, then return its value.
These future functions are used in conjunction with mock_server_t to
conveniently test libmongoc wire protocol operations.
Written for Python 2.6+, requires Jinja 2 for templating.
"""
import glob
from collections import namedtuple
from os.path import basename, dirname, join as joinpath, normpath
from jinja2 import Environment, FileSystemLoader # Please "pip install jinja2".
this_dir = dirname(__file__)
template_dir = joinpath(this_dir, 'future_function_templates')
mock_server_dir = normpath(joinpath(this_dir, '../tests/mock_server'))
# Add additional types here. Use typedefs for derived types so they can
# be named with one symbol.
typedef = namedtuple("typedef", ["name", "typedef"])
# These are typedef'ed if necessary in future-value.h, and added to the union
# of possible future_value_t.value types. future_value_t getters and setters
# are generated for all types, as well as future_t getters.
typedef_list = [
# Fundamental.
typedef("bool", None),
typedef("char_ptr", "char *"),
typedef("char_ptr_ptr", "char **"),
typedef("int", None),
typedef("int64_t", None),
typedef("size_t", None),
typedef("ssize_t", None),
typedef("uint32_t", None),
# Const fundamental.
typedef("const_char_ptr", "const char *"),
# libbson.
typedef("bson_error_ptr", "bson_error_t *"),
typedef("bson_ptr", "bson_t *"),
# Const libbson.
typedef("const_bson_ptr", "const bson_t *"),
typedef("const_bson_ptr_ptr", "const bson_t **"),
# libmongoc.
typedef("mongoc_bulk_operation_ptr", "mongoc_bulk_operation_t *"),
typedef("mongoc_client_ptr", "mongoc_client_t *"),
typedef("mongoc_collection_ptr", "mongoc_collection_t *"),
typedef("mongoc_cursor_ptr", "mongoc_cursor_t *"),
typedef("mongoc_database_ptr", "mongoc_database_t *"),
typedef("mongoc_gridfs_file_ptr", "mongoc_gridfs_file_t *"),
typedef("mongoc_gridfs_ptr", "mongoc_gridfs_t *"),
typedef("mongoc_insert_flags_t", None),
typedef("mongoc_iovec_ptr", "mongoc_iovec_t *"),
typedef("mongoc_query_flags_t", None),
typedef("mongoc_server_description_ptr", "mongoc_server_description_t *"),
typedef("mongoc_ss_optype_t", None),
typedef("mongoc_topology_ptr", "mongoc_topology_t *"),
# Const libmongoc.
typedef("const_mongoc_find_and_modify_opts_ptr", "const mongoc_find_and_modify_opts_t *"),
typedef("const_mongoc_read_prefs_ptr", "const mongoc_read_prefs_t *"),
typedef("const_mongoc_write_concern_ptr", "const mongoc_write_concern_t *"),
]
type_list = [T.name for T in typedef_list]
type_list_with_void = type_list + ['void']
param = namedtuple("param", ["type_name", "name"])
future_function = namedtuple("future_function", ["ret_type", "name", "params"])
# Add additional functions to be tested here. For a name like "cursor_next", we
# generate two functions: future_cursor_next to prepare the future_t and launch
# a background thread, and background_cursor_next to run on the thread and
# resolve the future.
future_functions = [
future_function("uint32_t",
"mongoc_bulk_operation_execute",
[param("mongoc_bulk_operation_ptr", "bulk"),
param("bson_ptr", "reply"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_client_command_simple",
[param("mongoc_client_ptr", "client"),
param("const_char_ptr", "db_name"),
param("const_bson_ptr", "command"),
param("const_mongoc_read_prefs_ptr", "read_prefs"),
param("bson_ptr", "reply"),
param("bson_error_ptr", "error")]),
future_function("void",
"mongoc_client_kill_cursor",
[param("mongoc_client_ptr", "client"),
param("int64_t", "cursor_id")]),
future_function("mongoc_cursor_ptr",
"mongoc_collection_aggregate",
[param("mongoc_collection_ptr", "collection"),
param("mongoc_query_flags_t", "flags"),
param("const_bson_ptr", "pipeline"),
param("const_bson_ptr", "options"),
param("const_mongoc_read_prefs_ptr", "read_prefs")]),
future_function("int64_t",
"mongoc_collection_count",
[param("mongoc_collection_ptr", "collection"),
param("mongoc_query_flags_t", "flags"),
param("const_bson_ptr", "query"),
param("int64_t", "skip"),
param("int64_t", "limit"),
param("const_mongoc_read_prefs_ptr", "read_prefs"),
param("bson_error_ptr", "error")]),
future_function("int64_t",
"mongoc_collection_count_with_opts",
[param("mongoc_collection_ptr", "collection"),
param("mongoc_query_flags_t", "flags"),
param("const_bson_ptr", "query"),
param("int64_t", "skip"),
param("int64_t", "limit"),
param("const_bson_ptr", "opts"),
param("const_mongoc_read_prefs_ptr", "read_prefs"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_collection_find_and_modify_with_opts",
[param("mongoc_collection_ptr", "collection"),
param("const_bson_ptr", "query"),
param("const_mongoc_find_and_modify_opts_ptr", "opts"),
param("bson_ptr", "reply"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_collection_find_and_modify",
[param("mongoc_collection_ptr", "collection"),
param("const_bson_ptr", "query"),
param("const_bson_ptr", "sort"),
param("const_bson_ptr", "update"),
param("const_bson_ptr", "fields"),
param("bool", "_remove"),
param("bool", "upsert"),
param("bool", "_new"),
param("bson_ptr", "reply"),
param("bson_error_ptr", "error")]),
future_function("mongoc_cursor_ptr",
"mongoc_collection_find_indexes",
[param("mongoc_collection_ptr", "collection"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_collection_stats",
[param("mongoc_collection_ptr", "collection"),
param("const_bson_ptr", "options"),
param("bson_ptr", "stats"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_collection_insert",
[param("mongoc_collection_ptr", "collection"),
param("mongoc_insert_flags_t", "flags"),
param("const_bson_ptr", "document"),
param("const_mongoc_write_concern_ptr", "write_concern"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_collection_insert_bulk",
[param("mongoc_collection_ptr", "collection"),
param("mongoc_insert_flags_t", "flags"),
param("const_bson_ptr_ptr", "documents"),
param("uint32_t", "n_documents"),
param("const_mongoc_write_concern_ptr", "write_concern"),
param("bson_error_ptr", "error")]),
future_function("void",
"mongoc_cursor_destroy",
[param("mongoc_cursor_ptr", "cursor")]),
future_function("bool",
"mongoc_cursor_next",
[param("mongoc_cursor_ptr", "cursor"),
param("const_bson_ptr_ptr", "doc")]),
future_function("char_ptr_ptr",
"mongoc_client_get_database_names",
[param("mongoc_client_ptr", "client"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_database_command_simple",
[param("mongoc_database_ptr", "database"),
param("bson_ptr", "command"),
param("const_mongoc_read_prefs_ptr", "read_prefs"),
param("bson_ptr", "reply"),
param("bson_error_ptr", "error")]),
future_function("char_ptr_ptr",
"mongoc_database_get_collection_names",
[param("mongoc_database_ptr", "database"),
param("bson_error_ptr", "error")]),
future_function("ssize_t",
"mongoc_gridfs_file_readv",
[param("mongoc_gridfs_file_ptr", "file"),
param("mongoc_iovec_ptr", "iov"),
param("size_t", "iovcnt"),
param("size_t", "min_bytes"),
param("uint32_t", "timeout_msec")]),
future_function("mongoc_gridfs_file_ptr",
"mongoc_gridfs_find_one",
[param("mongoc_gridfs_ptr", "gridfs"),
param("const_bson_ptr", "query"),
param("bson_error_ptr", "error")]),
future_function("bool",
"mongoc_gridfs_file_remove",
[param("mongoc_gridfs_file_ptr", "file"),
param("bson_error_ptr", "error")]),
future_function("int",
"mongoc_gridfs_file_seek",
[param("mongoc_gridfs_file_ptr", "file"),
param("int64_t", "delta"),
param("int", "whence")]),
future_function("ssize_t",
"mongoc_gridfs_file_writev",
[param("mongoc_gridfs_file_ptr", "file"),
param("mongoc_iovec_ptr", "iov"),
param("size_t", "iovcnt"),
param("uint32_t", "timeout_msec")]),
future_function("mongoc_server_description_ptr",
"mongoc_topology_select",
[param("mongoc_topology_ptr", "topology"),
param("mongoc_ss_optype_t", "optype"),
param("const_mongoc_read_prefs_ptr", "read_prefs"),
param("bson_error_ptr", "error")]),
future_function("mongoc_gridfs_ptr",
"mongoc_client_get_gridfs",
[param("mongoc_client_ptr", "client"),
param("const_char_ptr", "db"),
param("const_char_ptr", "prefix"),
param("bson_error_ptr", "error")]),
]
for fn in future_functions:
if fn.ret_type not in type_list_with_void:
raise Exception('bad type "%s"\n\nin %s' % (fn.ret_type, fn))
for p in fn.params:
if p.type_name not in type_list:
raise Exception('bad type "%s"\n\nin %s' % (p.type_name, fn))
header_comment = """/**************************************************
*
* Generated by build/%s.
*
* DO NOT EDIT THIS FILE.
*
*************************************************/""" % basename(__file__)
def future_function_name(fn):
if fn.name.startswith('mongoc'):
# E.g. future_cursor_next().
return 'future' + fn.name[len('mongoc'):]
else:
# E.g. future__mongoc_client_kill_cursor().
return 'future_' + fn.name
env = Environment(loader=FileSystemLoader(template_dir))
env.filters['future_function_name'] = future_function_name
files = ["future.h",
"future.c",
"future-value.h",
"future-value.c",
"future-functions.h",
"future-functions.c"]
for file_name in files:
print(file_name)
with open(joinpath(mock_server_dir, file_name), 'w+') as f:
t = env.get_template(file_name + ".template")
f.write(t.render(globals()))
|