Skip to content

Commit

Permalink
test: introduce new SWIM packet filter by component names
Browse files Browse the repository at this point in the history
In the next patch on payloads it is wanted to drop only packets
containing certain sections such as anti-entropy, dissemination.
New SWIM test transport filters allow to implement this with
ease.

Part of #3234
  • Loading branch information
Gerold103 committed Apr 12, 2019
1 parent 6ac68cc commit a001697
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/unit/swim_test_utils.c
Expand Up @@ -35,6 +35,7 @@
#include "uuid/tt_uuid.h"
#include "trivia/util.h"
#include "fiber.h"
#include "msgpuck.h"

/**
* SWIM cluster node and its UUID. UUID is stored separately
Expand Down Expand Up @@ -314,6 +315,59 @@ swim_cluster_set_drop_in(struct swim_cluster *cluster, int i, double value)
swim_cluster_set_drop_generic(cluster, i, value, true, false);
}

/**
* A list of components to drop used by component packet filter.
*/
struct swim_drop_components {
/** List of component body keys. */
const int *keys;
/** Length of @a keys. */
int key_count;
};

/**
* Check if a packet contains any of the components to filter out.
*/
static bool
swim_filter_drop_component(const char *data, int size, void *udata, int dir)
{
(void) size;
(void) dir;
struct swim_drop_components *dc = (struct swim_drop_components *) udata;
/* Skip meta. */
mp_next(&data);
int map_size = mp_decode_map(&data);
for (int i = 0; i < map_size; ++i) {
int key = mp_decode_uint(&data);
for (int j = 0; j < dc->key_count; ++j) {
if (dc->keys[j] == key)
return true;
}
/* Skip value. */
mp_next(&data);
}
return false;
}

void
swim_cluster_drop_components(struct swim_cluster *cluster, int i,
const int *keys, int key_count)
{
int fd = swim_fd(swim_cluster_node(cluster, i));
if (key_count == 0) {
swim_test_transport_remove_filter(fd,
swim_filter_drop_component);
return;
}
struct swim_drop_components *dc =
(struct swim_drop_components *) malloc(sizeof(*dc));
assert(dc != NULL);
dc->key_count = key_count;
dc->keys = keys;
swim_test_transport_add_filter(fd, swim_filter_drop_component, free,
dc);
}

/** Check if @a s1 knows every member of @a s2's table. */
static inline bool
swim1_contains_swim2(struct swim *s1, struct swim *s2)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/swim_test_utils.h
Expand Up @@ -109,6 +109,15 @@ swim_cluster_set_drop_out(struct swim_cluster *cluster, int i, double value);
void
swim_cluster_set_drop_in(struct swim_cluster *cluster, int i, double value);

/**
* Drop all packets from/to a SWIM instance with id @a i
* containing components specified in @a keys. Components are
* defined by the constants in the packet body.
*/
void
swim_cluster_drop_components(struct swim_cluster *cluster, int i,
const int *keys, int key_count);

/**
* Explicitly add a member of id @a from_id to a member of id
* @a to_id.
Expand Down

0 comments on commit a001697

Please sign in to comment.