Skip to content

Commit

Permalink
Added C API functions for compression args handling (w/ tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgquintas committed Aug 21, 2015
1 parent 083b4d3 commit a4c4f02
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 5 deletions.
34 changes: 33 additions & 1 deletion Makefile

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,20 @@
"gpr"
]
},
{
"name": "grpc_channel_args_test",
"build": "test",
"language": "c",
"src": [
"test/core/channel/channel_args_test.c"
],
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
{
"name": "grpc_channel_stack_test",
"build": "test",
Expand Down
26 changes: 23 additions & 3 deletions include/grpc/compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@

#include <stdlib.h>

#include <grpc/support/port_platform.h>

#ifdef __cplusplus
extern "C" {
#endif

/** To be used in channel arguments */
#define GRPC_COMPRESSION_ALGORITHM_ARG "grpc.compression_algorithm"
#define GRPC_COMPRESSION_ALGORITHM_STATE_ARG "grpc.compression_algorithm_state"

/* The various compression algorithms supported by GRPC */
typedef enum {
Expand All @@ -60,16 +63,19 @@ typedef enum {
GRPC_COMPRESS_LEVEL_COUNT
} grpc_compression_level;

typedef struct grpc_compression_options {
gpr_uint32 enabled_algorithms_bitset; /**< All algs are enabled by default */
grpc_compression_algorithm default_compression_algorithm; /**< for channel */
} grpc_compression_options;

/** Parses the first \a name_length bytes of \a name as a
* grpc_compression_algorithm instance, updating \a algorithm. Returns 1 upon
* success, 0 otherwise. */
int grpc_compression_algorithm_parse(const char *name, size_t name_length,
grpc_compression_algorithm *algorithm);

/** Updates \a name with the encoding name corresponding to a valid \a
* algorithm. Note that the string returned through \a name upon success is
* statically allocated and shouldn't be freed. Returns 1 upon success, 0
* otherwise. */
* algorithm. Returns 1 upon success, 0 otherwise. */
int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
char **name);

Expand All @@ -85,6 +91,20 @@ grpc_compression_level grpc_compression_level_for_algorithm(
grpc_compression_algorithm grpc_compression_algorithm_for_level(
grpc_compression_level level);

void grpc_compression_options_init(grpc_compression_options *opts);

/** Mark \a algorithm as enabled in \a opts. */
void grpc_compression_options_enable_algorithm(
grpc_compression_options *opts, grpc_compression_algorithm algorithm);

/** Mark \a algorithm as disabled in \a opts. */
void grpc_compression_options_disable_algorithm(
grpc_compression_options *opts, grpc_compression_algorithm algorithm);

/** Returns true if \a algorithm is marked as enabled in \a opts. */
int grpc_compression_options_is_algorithm_enabled(
const grpc_compression_options *opts, grpc_compression_algorithm algorithm);

#ifdef __cplusplus
}
#endif
Expand Down
62 changes: 62 additions & 0 deletions src/core/channel/channel_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <grpc/support/alloc.h>
#include <grpc/support/string_util.h>
#include <grpc/support/useful.h>

#include <string.h>

Expand Down Expand Up @@ -146,3 +147,64 @@ grpc_channel_args *grpc_channel_args_set_compression_algorithm(
tmp.value.integer = algorithm;
return grpc_channel_args_copy_and_add(a, &tmp, 1);
}

/** Returns 1 if the argument for compression algorithm's enabled states bitset
* was found in \a a, returning the arg's value in \a states. Otherwise, returns
* 0. */
static int find_compression_algorithm_states_bitset(
const grpc_channel_args *a, int **states_arg) {
if (a != NULL) {
size_t i;
for (i = 0; i < a->num_args; ++i) {
if (a->args[i].type == GRPC_ARG_INTEGER &&
!strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) {
*states_arg = &a->args[i].value.integer;
return 1; /* GPR_TRUE */
}
}
}
return 0; /* GPR_FALSE */
}

grpc_channel_args *grpc_channel_args_compression_algorithm_set_state(
grpc_channel_args *a,
grpc_compression_algorithm algorithm,
int state) {
int *states_arg;
grpc_channel_args *result = a;
const int states_arg_found =
find_compression_algorithm_states_bitset(a, &states_arg);

if (states_arg_found) {
if (state != 0) {
GPR_BITSET(states_arg, algorithm);
} else {
GPR_BITCLEAR(states_arg, algorithm);
}
} else {
/* create a new arg */
grpc_arg tmp;
tmp.type = GRPC_ARG_INTEGER;
tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG;
/* all enabled by default */
tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
if (state != 0) {
GPR_BITSET(&tmp.value.integer, algorithm);
} else {
GPR_BITCLEAR(&tmp.value.integer, algorithm);
}
result = grpc_channel_args_copy_and_add(a, &tmp, 1);
grpc_channel_args_destroy(a);
}
return result;
}

int grpc_channel_args_compression_algorithm_get_states(
const grpc_channel_args *a) {
int *states_arg;
if (find_compression_algorithm_states_bitset(a, &states_arg)) {
return *states_arg;
} else {
return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */
}
}
17 changes: 17 additions & 0 deletions src/core/channel/channel_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,21 @@ grpc_compression_algorithm grpc_channel_args_get_compression_algorithm(
grpc_channel_args *grpc_channel_args_set_compression_algorithm(
grpc_channel_args *a, grpc_compression_algorithm algorithm);

/** Sets the support for the given compression algorithm. By default, all
* compression algorithms are enabled. It's an error to disable an algorithm set
* by grpc_channel_args_set_compression_algorithm.
* */
grpc_channel_args *grpc_channel_args_compression_algorithm_set_state(
grpc_channel_args *a,
grpc_compression_algorithm algorithm,
int enabled);

/** Returns the bitset representing the support state (true for enabled, false
* for disabled) for compression algorithms.
*
* The i-th bit of the returned bitset corresponds to the i-th entry in the
* grpc_compression_algorithm enum. */
int grpc_channel_args_compression_algorithm_get_states(
const grpc_channel_args *a);

#endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H */
136 changes: 136 additions & 0 deletions test/core/channel/channel_args_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#include <string.h>

#include <grpc/support/log.h>
#include <grpc/support/useful.h>

#include "src/core/channel/channel_args.h"

#include "test/core/util/test_config.h"

static void test_create(void) {
grpc_arg arg_int;
grpc_arg arg_string;
grpc_arg to_add[2];
grpc_channel_args *ch_args;

arg_int.key = "int_arg";
arg_int.type = GRPC_ARG_INTEGER;
arg_int.value.integer = 123;

arg_string.key = "str key";
arg_string.type = GRPC_ARG_STRING;
arg_string.value.string = "str value";

to_add[0] = arg_int;
to_add[1] = arg_string;
ch_args = grpc_channel_args_copy_and_add(NULL, to_add, 2);

GPR_ASSERT(ch_args->num_args == 2);
GPR_ASSERT(strcmp(ch_args->args[0].key, arg_int.key) == 0);
GPR_ASSERT(ch_args->args[0].type == arg_int.type);
GPR_ASSERT(ch_args->args[0].value.integer == arg_int.value.integer);

GPR_ASSERT(strcmp(ch_args->args[1].key, arg_string.key) == 0);
GPR_ASSERT(ch_args->args[1].type == arg_string.type);
GPR_ASSERT(strcmp(ch_args->args[1].value.string, arg_string.value.string) ==
0);

grpc_channel_args_destroy(ch_args);
}

static void test_set_compression_algorithm(void) {
grpc_channel_args *ch_args;

ch_args =
grpc_channel_args_set_compression_algorithm(NULL, GRPC_COMPRESS_GZIP);
GPR_ASSERT(ch_args->num_args == 1);
GPR_ASSERT(strcmp(ch_args->args[0].key, GRPC_COMPRESSION_ALGORITHM_ARG) == 0);
GPR_ASSERT(ch_args->args[0].type == GRPC_ARG_INTEGER);

grpc_channel_args_destroy(ch_args);
}

static void test_compression_algorithm_states(void) {
grpc_channel_args *ch_args;
int states_bitset;
size_t i;

ch_args = grpc_channel_args_copy_and_add(NULL, NULL, 0);
/* by default, all enabled */
states_bitset = grpc_channel_args_compression_algorithm_get_states(ch_args);

for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
GPR_ASSERT(GPR_BITGET(states_bitset, i));
}

/* disable gzip and deflate */
ch_args = grpc_channel_args_compression_algorithm_set_state(
ch_args, GRPC_COMPRESS_GZIP, 0);
ch_args = grpc_channel_args_compression_algorithm_set_state(
ch_args, GRPC_COMPRESS_DEFLATE, 0);

states_bitset = grpc_channel_args_compression_algorithm_get_states(ch_args);
for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
if (i == GRPC_COMPRESS_GZIP || i == GRPC_COMPRESS_DEFLATE) {
GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0);
} else {
GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
}
}

/* re-enabled gzip only */
ch_args = grpc_channel_args_compression_algorithm_set_state(
ch_args, GRPC_COMPRESS_GZIP, 1);

states_bitset = grpc_channel_args_compression_algorithm_get_states(ch_args);
for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
if (i == GRPC_COMPRESS_DEFLATE) {
GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0);
} else {
GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
}
}

grpc_channel_args_destroy(ch_args);
}

int main(int argc, char **argv) {
grpc_test_init(argc, argv);
test_create();
test_set_compression_algorithm();
test_compression_algorithm_states();
return 0;
}
14 changes: 14 additions & 0 deletions tools/run_tests/sources_and_headers.json
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,20 @@
"test/core/surface/byte_buffer_reader_test.c"
]
},
{
"deps": [
"gpr",
"gpr_test_util",
"grpc",
"grpc_test_util"
],
"headers": [],
"language": "c",
"name": "grpc_channel_args_test",
"src": [
"test/core/channel/channel_args_test.c"
]
},
{
"deps": [
"gpr",
Expand Down
19 changes: 19 additions & 0 deletions tools/run_tests/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,24 @@
"windows"
]
},
{
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"exclude_configs": [],
"flaky": false,
"language": "c",
"name": "grpc_channel_args_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
]
},
{
"ci_platforms": [
"linux",
Expand Down Expand Up @@ -1538,6 +1556,7 @@
"posix",
"windows"
],
"exclude_configs": [],
"flaky": false,
"language": "c++",
"name": "status_test",
Expand Down
10 changes: 9 additions & 1 deletion vsprojects/Grpc.mak

Large diffs are not rendered by default.

0 comments on commit a4c4f02

Please sign in to comment.