LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_batch.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 53 60 88.3 %
Date: 2021-08-30 06:43:37 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU General Public License as
       7             :   published by the Free Software Foundation; either version 3, or
       8             :   (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU General Public
      16             :   License along with TALER; see the file COPYING.  If not, see
      17             :   <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file testing/testing_api_cmd_batch.c
      21             :  * @brief Implement batch-execution of CMDs.
      22             :  * @author Marcello Stanisci
      23             :  */
      24             : #include "platform.h"
      25             : #include "taler_json_lib.h"
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_testing_lib.h"
      28             : 
      29             : 
      30             : /**
      31             :  * State for a "batch" CMD.
      32             :  */
      33             : struct BatchState
      34             : {
      35             :   /**
      36             :    * CMDs batch.
      37             :    */
      38             :   struct TALER_TESTING_Command *batch;
      39             : 
      40             :   /**
      41             :    * Internal command pointer.
      42             :    */
      43             :   unsigned int batch_ip;
      44             : };
      45             : 
      46             : 
      47             : /**
      48             :  * Run the command.
      49             :  *
      50             :  * @param cls closure.
      51             :  * @param cmd the command being executed.
      52             :  * @param is the interpreter state.
      53             :  */
      54             : static void
      55         282 : batch_run (void *cls,
      56             :            const struct TALER_TESTING_Command *cmd,
      57             :            struct TALER_TESTING_Interpreter *is)
      58             : {
      59         282 :   struct BatchState *bs = cls;
      60             : 
      61         282 :   if (NULL != bs->batch[bs->batch_ip].label)
      62         261 :     TALER_LOG_INFO ("Running batched command: %s\n",
      63             :                     bs->batch[bs->batch_ip].label);
      64             : 
      65             :   /* hit end command, leap to next top-level command.  */
      66         282 :   if (NULL == bs->batch[bs->batch_ip].label)
      67             :   {
      68          21 :     TALER_LOG_INFO ("Exiting from batch: %s\n",
      69             :                     cmd->label);
      70          21 :     TALER_TESTING_interpreter_next (is);
      71          21 :     return;
      72             :   }
      73         261 :   bs->batch[bs->batch_ip].start_time
      74         522 :     = bs->batch[bs->batch_ip].last_req_time
      75         261 :       = GNUNET_TIME_absolute_get ();
      76         261 :   bs->batch[bs->batch_ip].num_tries = 1;
      77         261 :   bs->batch[bs->batch_ip].run (bs->batch[bs->batch_ip].cls,
      78         261 :                                &bs->batch[bs->batch_ip],
      79             :                                is);
      80             : }
      81             : 
      82             : 
      83             : /**
      84             :  * Cleanup the state from a "reserve status" CMD, and possibly
      85             :  * cancel a pending operation thereof.
      86             :  *
      87             :  * @param cls closure.
      88             :  * @param cmd the command which is being cleaned up.
      89             :  */
      90             : static void
      91          21 : batch_cleanup (void *cls,
      92             :                const struct TALER_TESTING_Command *cmd)
      93             : {
      94          21 :   struct BatchState *bs = cls;
      95             : 
      96             :   (void) cmd;
      97          21 :   for (unsigned int i = 0;
      98         281 :        NULL != bs->batch[i].label;
      99         260 :        i++)
     100         260 :     bs->batch[i].cleanup (bs->batch[i].cls,
     101         260 :                           &bs->batch[i]);
     102          21 :   GNUNET_free (bs->batch);
     103          21 :   GNUNET_free (bs);
     104          21 : }
     105             : 
     106             : 
     107             : /**
     108             :  * Offer internal data from a "batch" CMD, to other commands.
     109             :  *
     110             :  * @param cls closure.
     111             :  * @param[out] ret result.
     112             :  * @param trait name of the trait.
     113             :  * @param index index number of the object to offer.
     114             :  * @return #GNUNET_OK on success.
     115             :  */
     116             : static int
     117        2142 : batch_traits (void *cls,
     118             :               const void **ret,
     119             :               const char *trait,
     120             :               unsigned int index)
     121             : {
     122             : #define CURRENT_CMD_INDEX 0
     123             : #define BATCH_INDEX 1
     124             : 
     125        2142 :   struct BatchState *bs = cls;
     126             : 
     127             :   struct TALER_TESTING_Trait traits[] = {
     128        2142 :     TALER_TESTING_make_trait_cmd
     129        2142 :       (CURRENT_CMD_INDEX, &bs->batch[bs->batch_ip]),
     130        2142 :     TALER_TESTING_make_trait_cmd
     131        2142 :       (BATCH_INDEX, bs->batch),
     132        2142 :     TALER_TESTING_trait_end ()
     133             :   };
     134             : 
     135             :   /* Always return current command.  */
     136        2142 :   return TALER_TESTING_get_trait (traits,
     137             :                                   ret,
     138             :                                   trait,
     139             :                                   index);
     140             : }
     141             : 
     142             : 
     143             : /**
     144             :  * Create a "batch" command.  Such command takes a
     145             :  * end_CMD-terminated array of CMDs and executed them.
     146             :  * Once it hits the end CMD, it passes the control
     147             :  * to the next top-level CMD, regardless of it being
     148             :  * another batch or ordinary CMD.
     149             :  *
     150             :  * @param label the command label.
     151             :  * @param batch array of CMDs to execute.
     152             :  *
     153             :  * @return the command.
     154             :  */
     155             : struct TALER_TESTING_Command
     156          21 : TALER_TESTING_cmd_batch (const char *label,
     157             :                          struct TALER_TESTING_Command *batch)
     158             : {
     159             :   struct BatchState *bs;
     160             :   unsigned int i;
     161             : 
     162          21 :   bs = GNUNET_new (struct BatchState);
     163             : 
     164             :   /* Get number of commands.  */
     165         281 :   for (i = 0; NULL != batch[i].label; i++)
     166             :     /* noop */
     167             :     ;
     168             : 
     169          21 :   bs->batch = GNUNET_new_array (i + 1,
     170             :                                 struct TALER_TESTING_Command);
     171          21 :   memcpy (bs->batch,
     172             :           batch,
     173             :           sizeof (struct TALER_TESTING_Command) * i);
     174             :   {
     175          21 :     struct TALER_TESTING_Command cmd = {
     176             :       .cls = bs,
     177             :       .label = label,
     178             :       .run = &batch_run,
     179             :       .cleanup = &batch_cleanup,
     180             :       .traits = &batch_traits
     181             :     };
     182             : 
     183          21 :     return cmd;
     184             :   }
     185             : }
     186             : 
     187             : 
     188             : /**
     189             :  * Advance internal pointer to next command.
     190             :  *
     191             :  * @param is interpreter state.
     192             :  */
     193             : void
     194         281 : TALER_TESTING_cmd_batch_next (struct TALER_TESTING_Interpreter *is)
     195             : {
     196         281 :   struct BatchState *bs = is->commands[is->ip].cls;
     197             : 
     198         281 :   if (NULL == bs->batch[bs->batch_ip].label)
     199             :   {
     200          21 :     is->commands[is->ip].finish_time = GNUNET_TIME_absolute_get ();
     201          21 :     is->ip++;
     202          21 :     return;
     203             :   }
     204         260 :   bs->batch[bs->batch_ip].finish_time = GNUNET_TIME_absolute_get ();
     205         260 :   bs->batch_ip++;
     206             : }
     207             : 
     208             : 
     209             : /**
     210             :  * Test if this command is a batch command.
     211             :  *
     212             :  * @return false if not, true if it is a batch command
     213             :  */
     214             : int
     215        4441 : TALER_TESTING_cmd_is_batch (const struct TALER_TESTING_Command *cmd)
     216             : {
     217        4441 :   return cmd->run == &batch_run;
     218             : }
     219             : 
     220             : 
     221             : /**
     222             :  * Obtain what command the batch is at.
     223             :  *
     224             :  * @return cmd current batch command
     225             :  */
     226             : struct TALER_TESTING_Command *
     227        2086 : TALER_TESTING_cmd_batch_get_current (const struct TALER_TESTING_Command *cmd)
     228             : {
     229        2086 :   struct BatchState *bs = cmd->cls;
     230             : 
     231        2086 :   GNUNET_assert (cmd->run == &batch_run);
     232        2086 :   return &bs->batch[bs->batch_ip];
     233             : }
     234             : 
     235             : 
     236             : /**
     237             :  * Set what command the batch should be at.
     238             :  *
     239             :  * @param cmd current batch command
     240             :  * @param new_ip where to move the IP
     241             :  */
     242             : void
     243           0 : TALER_TESTING_cmd_batch_set_current (const struct TALER_TESTING_Command *cmd,
     244             :                                      unsigned int new_ip)
     245             : {
     246           0 :   struct BatchState *bs = cmd->cls;
     247             : 
     248             :   /* sanity checks */
     249           0 :   GNUNET_assert (cmd->run == &batch_run);
     250           0 :   for (unsigned int i = 0; i < new_ip; i++)
     251           0 :     GNUNET_assert (NULL != bs->batch[i].label);
     252             :   /* actual logic */
     253           0 :   bs->batch_ip = new_ip;
     254           0 : }

Generated by: LCOV version 1.14