how to run expect_transaction example

This commit is contained in:
Christian Grothoff 2016-04-04 15:07:01 +02:00
parent a72661fd29
commit db44dc3e95

View File

@ -85,6 +85,16 @@ enum OpCode {
*/ */
OPCODE_RUN_AGGREGATOR, OPCODE_RUN_AGGREGATOR,
/**
* Expect that we have exhaustively gone over all transactions.
*/
OPCODE_EXPECT_TRANSACTIONS_EMPTY,
/**
* Expect that we have received the specified transaction.
*/
OPCODE_EXPECT_TRANSACTION,
/** /**
* Finish testcase with success. * Finish testcase with success.
*/ */
@ -97,8 +107,27 @@ enum OpCode {
struct Command struct Command
{ {
/**
* What instruction should we run?
*/
enum OpCode opcode; enum OpCode opcode;
/**
* Human-readable label for the command.
*/
const char *label;
union {
/**
* If @e opcode is #OPCODE_EXPECT_TRANSACTION, this
* specifies which transaction we expected. Note that
* the WTID will be set, not checked!
*/
struct Transaction expect_transaction;
} details;
}; };
@ -279,6 +308,20 @@ maint_child_death (void *cls,
} }
/**
* Fail the testcase at the current command.
*/
static void
fail (struct Command *cmd)
{
fprintf (stderr,
"Testcase failed at command `%s'\n",
cmd->label);
result = 2;
GNUNET_SCHEDULER_shutdown ();
}
/** /**
* Interprets the commands from the test program. * Interprets the commands from the test program.
* *
@ -290,33 +333,80 @@ interpreter (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc) const struct GNUNET_SCHEDULER_TaskContext *tc)
{ {
struct State *state = cls; struct State *state = cls;
struct Command *cmd = &state->commands[state->ioff];
switch (cmd->opcode) while (1)
{ {
case OPCODE_TERMINATE_SKIP: struct Command *cmd = &state->commands[state->ioff];
/* return skip: test not finished, but did not fail either */
result = 77; GNUNET_log (GNUNET_ERROR_TYPE_INFO,
GNUNET_SCHEDULER_shutdown (); "Running command %u (%s)\n",
return; state->ioff,
case OPCODE_RUN_AGGREGATOR: cmd->label);
GNUNET_assert (NULL == aggregator_state); switch (cmd->opcode)
aggregator_state = state; {
aggregator_proc case OPCODE_TERMINATE_SKIP:
= GNUNET_OS_start_process (GNUNET_NO, /* return skip: test not finished, but did not fail either */
GNUNET_OS_INHERIT_STD_ALL, result = 77;
NULL, NULL, NULL, GNUNET_SCHEDULER_shutdown ();
"taler-exchange-aggregator", return;
"taler-exchange-aggregator", case OPCODE_RUN_AGGREGATOR:
/* "-c", config_filename, */ GNUNET_assert (NULL == aggregator_state);
"-d", "test-exchange-home", aggregator_state = state;
"-t", /* enable temporary tables */ aggregator_proc
NULL); = GNUNET_OS_start_process (GNUNET_NO,
return; GNUNET_OS_INHERIT_STD_ALL,
case OPCODE_TERMINATE_SUCCESS: NULL, NULL, NULL,
result = 0; "taler-exchange-aggregator",
GNUNET_SCHEDULER_shutdown (); "taler-exchange-aggregator",
/* "-c", config_filename, */
"-d", "test-exchange-home",
"-t", /* enable temporary tables */
NULL);
return; return;
case OPCODE_EXPECT_TRANSACTIONS_EMPTY:
if (NULL != transactions_head)
{
fail (cmd);
return;
}
state->ioff++;
break;
case OPCODE_EXPECT_TRANSACTION:
{
const struct Transaction *want = &cmd->details.expect_transaction;
struct Transaction *t;
int found;
found = GNUNET_NO;
for (t = transactions_head; NULL != t; t = t->next)
{
if ( (want->debit_account == t->debit_account) &&
(want->credit_account == t->credit_account) &&
(0 == TALER_amount_cmp (&want->amount,
&t->amount)) )
{
GNUNET_CONTAINER_DLL_remove (transactions_head,
transactions_tail,
t);
cmd->details.expect_transaction.wtid = t->wtid;
GNUNET_free (t);
found = GNUNET_YES;
break;
}
}
if (GNUNET_NO == found)
{
fail (cmd);
return;
}
state->ioff++;
break;
}
case OPCODE_TERMINATE_SUCCESS:
result = 0;
GNUNET_SCHEDULER_shutdown ();
return;
}
} }
} }
@ -331,16 +421,32 @@ run_test ()
static struct Command commands[] = { static struct Command commands[] = {
/* FIXME: prime DB */ /* FIXME: prime DB */
{ {
.opcode = OPCODE_RUN_AGGREGATOR .opcode = OPCODE_RUN_AGGREGATOR,
.label = "run-aggregator-on-empty-db"
}, },
{ {
.opcode = OPCODE_TERMINATE_SKIP .opcode = OPCODE_EXPECT_TRANSACTIONS_EMPTY,
.label = "expect-empty-transactions-on-start"
},
{
.opcode = OPCODE_TERMINATE_SKIP,
.label = "testcase-incomplete-terminating-with-skip"
},
/* note: rest not reached, just sample code */
{
.opcode = OPCODE_EXPECT_TRANSACTION,
.label = "testing test logic",
.details.expect_transaction.debit_account = 1,
.details.expect_transaction.credit_account = 1,
.details.expect_transaction.amount = { 1, 0, "EUR" }
} }
}; };
static struct State state = { static struct State state = {
.commands = commands .commands = commands
}; };
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Launching interpreter\n");
GNUNET_SCHEDULER_add_now (&interpreter, GNUNET_SCHEDULER_add_now (&interpreter,
&state); &state);
} }
@ -636,6 +742,9 @@ main (int argc,
"test-taler-exchange-aggregator-%s", plugin_name); "test-taler-exchange-aggregator-%s", plugin_name);
(void) GNUNET_asprintf (&config_filename, (void) GNUNET_asprintf (&config_filename,
"%s.conf", testname); "%s.conf", testname);
GNUNET_log_setup ("test_taler_exchange_aggregator",
"WARNING",
NULL);
cfg = GNUNET_CONFIGURATION_create (); cfg = GNUNET_CONFIGURATION_create ();
if (GNUNET_OK != if (GNUNET_OK !=
GNUNET_CONFIGURATION_parse (cfg, GNUNET_CONFIGURATION_parse (cfg,