static bool
with_transaction_test_run_operation (json_test_ctx_t *ctx,
                                     const bson_t *test,
                                     const bson_t *operation)
{
   mongoc_transaction_opt_t *opts = NULL;
   mongoc_client_session_t *session = NULL;
   bson_error_t error;
   bson_t args;
   bson_t reply;
   bool res;
   cb_ctx_t cb_ctx;

   bson_lookup_doc (operation, "arguments", &args);

   /* If there is a 'callback' field, run the nested operations through
      mongoc_client_session_with_transaction(). */
   if (bson_has_field (&args, "callback")) {
      bson_init (&reply);

      ASSERT (bson_has_field (operation, "object"));
      session = session_from_name (ctx, bson_lookup_utf8 (operation, "object"));
      ASSERT (session);

      bson_lookup_doc (&args, "callback", &cb_ctx.callback);
      cb_ctx.ctx = ctx;

      if (bson_has_field (&args, "options")) {
         opts = bson_lookup_txn_opts (&args, "options");
      }

      res = mongoc_client_session_with_transaction (
         session, with_transaction_callback_runner, opts, &cb_ctx, &error);

   } else {
      /* If there is no 'callback' field, then run simply. */
      if (bson_has_field (&args, "session")) {
         session = session_from_name (ctx, bson_lookup_utf8 (&args, "session"));
      }

      res = json_test_operation (
         ctx, test, operation, ctx->collection, session, &reply);
   }

   bson_destroy (&args);
   bson_destroy (&reply);
   mongoc_transaction_opts_destroy (opts);

   return res;
}
Beispiel #2
0
static void
transactions_test_run_operation (json_test_ctx_t *ctx,
                                 const bson_t *test,
                                 const bson_t *operation,
                                 mongoc_collection_t *collection)
{
   const char *description;
   const char *session_name;
   mongoc_client_session_t *session;

   if (bson_has_field (operation, "arguments.session")) {
      session_name = bson_lookup_utf8 (operation, "arguments.session");
      if (!strcmp (session_name, "session0")) {
         session = ctx->sessions[0];
      } else if (!strcmp (session_name, "session1")) {
         session = ctx->sessions[1];
      } else {
         MONGOC_ERROR ("Unrecognized session name: %s", session_name);
         abort ();
      }
   } else {
      session = NULL;
   }

   description = bson_lookup_utf8 (test, "description");

   /* we log warnings from abortTransaction. suppress warnings that we expect,
    * but don't suppress all: we want to know if any other tests log warnings */
   if (!strcmp (description, "write conflict abort") ||
       !strcmp (description, "abort ignores TransactionAborted") ||
       !strcmp (description, "abort does not apply writeConcern")) {
      capture_logs (true);
   }

   /* json_test_operations() chose session0 or session1 from the
    * arguments.session field in the JSON test */
   json_test_operation (test, operation, collection, session);

   capture_logs (false);
}