blob: e1556d7d7b038b9eefbdb69f56313ee3748ac86a [file] [log] [blame]
Alistair Veitch4aaba752016-06-02 17:11:46 -07001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/ext/census/resource.h"
35#include <grpc/census.h>
36#include <grpc/support/log.h>
37#include <grpc/support/port_platform.h>
38#include <grpc/support/useful.h>
39#include <stdbool.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include "src/core/ext/census/base_resources.h"
43#include "test/core/util/test_config.h"
44
45// Test all the functionality for dealing with Resources.
46
47// Just startup and shutdown resources subsystem.
48static void test_enable_disable() {
49 initialize_resources();
50 shutdown_resources();
51}
52
53// A blank/empty initialization should not work.
54static void test_empty_definition() {
55 initialize_resources();
56 int32_t rid = census_define_resource(NULL, 0);
57 GPR_ASSERT(rid == -1);
58 uint8_t buffer[50] = {0};
59 rid = census_define_resource(buffer, 50);
60 GPR_ASSERT(rid == -1);
61 shutdown_resources();
62}
63
64// Given a file name, read raw proto and define the resource included within.
65// Returns resource id from census_define_resource().
66static int32_t define_resource_from_file(const char *file) {
Alistair Veitch7a97fe22016-06-03 12:10:45 -070067#define BUF_SIZE 512
68 uint8_t buffer[BUF_SIZE];
Alistair Veitch8c7618e2016-07-26 10:30:36 -070069 FILE *input = fopen(file, "rb");
Alistair Veitch4aaba752016-06-02 17:11:46 -070070 GPR_ASSERT(input != NULL);
Alistair Veitch7a97fe22016-06-03 12:10:45 -070071 size_t nbytes = fread(buffer, 1, BUF_SIZE, input);
Alistair Veitch8c7618e2016-07-26 10:30:36 -070072 GPR_ASSERT(nbytes != 0 && nbytes < BUF_SIZE && feof(input) && !ferror(input));
Alistair Veitch4aaba752016-06-02 17:11:46 -070073 int32_t rid = census_define_resource(buffer, nbytes);
74 GPR_ASSERT(fclose(input) == 0);
75 return rid;
76}
77
78// Test definition of a single resource, using a proto read from a file. The
79// `succeed` parameter indicates whether we expect the definition to succeed or
80// fail. `name` is used to check that the returned resource can be looked up by
81// name.
82static void test_define_single_resource(const char *file, const char *name,
83 bool succeed) {
84 gpr_log(GPR_INFO, "Test defining resource \"%s\"\n", name);
85 initialize_resources();
86 int32_t rid = define_resource_from_file(file);
87 if (succeed) {
88 GPR_ASSERT(rid >= 0);
89 int32_t rid2 = census_resource_id(name);
90 GPR_ASSERT(rid == rid2);
91 } else {
92 GPR_ASSERT(rid < 0);
93 }
94 shutdown_resources();
95}
96
97// Try deleting various resources (both those that exist and those that don't).
98static void test_delete_resource() {
99 initialize_resources();
100 // Try deleting resource before any are defined.
101 census_delete_resource(0);
102 // Create and check a couple of resources.
103 int32_t rid1 = define_resource_from_file(
104 "test/core/census/data/resource_minimal_good.pb");
105 int32_t rid2 =
106 define_resource_from_file("test/core/census/data/resource_full.pb");
107 GPR_ASSERT(rid1 >= 0 && rid2 >= 0 && rid1 != rid2);
108 int32_t rid3 = census_resource_id("minimal_good");
109 int32_t rid4 = census_resource_id("full_resource");
110 GPR_ASSERT(rid1 == rid3 && rid2 == rid4);
111 // Try deleting non-existant resources.
112 census_delete_resource(-1);
113 census_delete_resource(rid1 + rid2 + 1);
114 census_delete_resource(10000000);
115 // Delete one of the previously defined resources and check for deletion.
116 census_delete_resource(rid1);
117 rid3 = census_resource_id("minimal_good");
118 GPR_ASSERT(rid3 < 0);
119 // Check that re-adding works.
120 rid1 = define_resource_from_file(
121 "test/core/census/data/resource_minimal_good.pb");
122 GPR_ASSERT(rid1 >= 0);
123 rid3 = census_resource_id("minimal_good");
124 GPR_ASSERT(rid1 == rid3);
125 shutdown_resources();
126}
127
128// Test define base resources.
129static void test_base_resources() {
130 initialize_resources();
131 define_base_resources();
132 int32_t rid1 = census_resource_id("client_rpc_latency");
133 int32_t rid2 = census_resource_id("server_rpc_latency");
134 GPR_ASSERT(rid1 >= 0 && rid2 >= 0 && rid1 != rid2);
135 shutdown_resources();
136}
137
138int main(int argc, char **argv) {
139 grpc_test_init(argc, argv);
140 test_enable_disable();
141 test_empty_definition();
142 test_define_single_resource("test/core/census/data/resource_minimal_good.pb",
143 "minimal_good", true);
144 test_define_single_resource("test/core/census/data/resource_full.pb",
145 "full_resource", true);
146 test_define_single_resource("test/core/census/data/resource_no_name.pb",
147 "resource_no_name", false);
148 test_define_single_resource("test/core/census/data/resource_no_numerator.pb",
149 "resource_no_numerator", false);
150 test_define_single_resource("test/core/census/data/resource_no_unit.pb",
151 "resource_no_unit", false);
152 test_define_single_resource("test/core/census/data/resource_empty_name.pb",
153 "resource_empty_name", false);
154 test_delete_resource();
155 test_base_resources();
156 return 0;
157}