cq_verifier.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "test/core/end2end/cq_verifier.h"
  19. #include <inttypes.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <grpc/byte_buffer.h>
  24. #include <grpc/byte_buffer_reader.h>
  25. #include <grpc/support/alloc.h>
  26. #include <grpc/support/log.h>
  27. #include <grpc/support/string_util.h>
  28. #include <grpc/support/time.h>
  29. #include "src/core/lib/compression/compression_internal.h"
  30. #include "src/core/lib/compression/message_compress.h"
  31. #include "src/core/lib/gpr/string.h"
  32. #include "src/core/lib/surface/event_string.h"
  33. #define ROOT_EXPECTATION 1000
  34. /* a set of metadata we expect to find on an event */
  35. typedef struct metadata {
  36. size_t count;
  37. size_t cap;
  38. char** keys;
  39. char** values;
  40. } metadata;
  41. /* details what we expect to find on a single event - and forms a linked
  42. list to detail other expectations */
  43. typedef struct expectation {
  44. struct expectation* next;
  45. const char* file;
  46. int line;
  47. grpc_completion_type type;
  48. void* tag;
  49. int success;
  50. } expectation;
  51. /* the verifier itself */
  52. struct cq_verifier {
  53. /* bound completion queue */
  54. grpc_completion_queue* cq;
  55. /* start of expectation list */
  56. expectation* first_expectation;
  57. };
  58. cq_verifier* cq_verifier_create(grpc_completion_queue* cq) {
  59. cq_verifier* v = static_cast<cq_verifier*>(gpr_malloc(sizeof(cq_verifier)));
  60. v->cq = cq;
  61. v->first_expectation = nullptr;
  62. return v;
  63. }
  64. void cq_verifier_destroy(cq_verifier* v) {
  65. cq_verify(v);
  66. gpr_free(v);
  67. }
  68. static int has_metadata(const grpc_metadata* md, size_t count, const char* key,
  69. const char* value) {
  70. size_t i;
  71. for (i = 0; i < count; i++) {
  72. if (0 == grpc_slice_str_cmp(md[i].key, key) &&
  73. 0 == grpc_slice_str_cmp(md[i].value, value)) {
  74. return 1;
  75. }
  76. }
  77. return 0;
  78. }
  79. int contains_metadata(grpc_metadata_array* array, const char* key,
  80. const char* value) {
  81. return has_metadata(array->metadata, array->count, key, value);
  82. }
  83. static int has_metadata_slices(const grpc_metadata* md, size_t count,
  84. grpc_slice key, grpc_slice value) {
  85. size_t i;
  86. for (i = 0; i < count; i++) {
  87. if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) {
  88. return 1;
  89. }
  90. }
  91. return 0;
  92. }
  93. int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key,
  94. grpc_slice value) {
  95. return has_metadata_slices(array->metadata, array->count, key, value);
  96. }
  97. static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) {
  98. size_t i;
  99. size_t len = 0;
  100. uint8_t* cursor;
  101. grpc_slice out;
  102. for (i = 0; i < nslices; i++) {
  103. len += GRPC_SLICE_LENGTH(slices[i]);
  104. }
  105. out = grpc_slice_malloc(len);
  106. cursor = GRPC_SLICE_START_PTR(out);
  107. for (i = 0; i < nslices; i++) {
  108. memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
  109. GRPC_SLICE_LENGTH(slices[i]));
  110. cursor += GRPC_SLICE_LENGTH(slices[i]);
  111. }
  112. return out;
  113. }
  114. int raw_byte_buffer_eq_slice(grpc_byte_buffer* rbb, grpc_slice b) {
  115. grpc_slice a;
  116. int ok;
  117. if (!rbb) return 0;
  118. a = merge_slices(rbb->data.raw.slice_buffer.slices,
  119. rbb->data.raw.slice_buffer.count);
  120. ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) &&
  121. 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
  122. GRPC_SLICE_LENGTH(a));
  123. grpc_slice_unref(a);
  124. grpc_slice_unref(b);
  125. return ok;
  126. }
  127. int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b) {
  128. if (bb->data.raw.compression > GRPC_COMPRESS_NONE) {
  129. grpc_slice_buffer decompressed_buffer;
  130. grpc_slice_buffer_init(&decompressed_buffer);
  131. GPR_ASSERT(grpc_msg_decompress(
  132. grpc_compression_algorithm_to_message_compression_algorithm(
  133. bb->data.raw.compression),
  134. &bb->data.raw.slice_buffer, &decompressed_buffer));
  135. grpc_byte_buffer* rbb = grpc_raw_byte_buffer_create(
  136. decompressed_buffer.slices, decompressed_buffer.count);
  137. int ret_val = raw_byte_buffer_eq_slice(rbb, b);
  138. grpc_byte_buffer_destroy(rbb);
  139. grpc_slice_buffer_destroy(&decompressed_buffer);
  140. return ret_val;
  141. }
  142. return raw_byte_buffer_eq_slice(bb, b);
  143. }
  144. int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str) {
  145. return byte_buffer_eq_slice(bb, grpc_slice_from_copied_string(str));
  146. }
  147. static bool is_probably_integer(void* p) { return ((uintptr_t)p) < 1000000; }
  148. static void expectation_to_strvec(gpr_strvec* buf, expectation* e) {
  149. char* tmp;
  150. if (is_probably_integer(e->tag)) {
  151. gpr_asprintf(&tmp, "tag(%" PRIdPTR ") ", (intptr_t)e->tag);
  152. } else {
  153. gpr_asprintf(&tmp, "%p ", e->tag);
  154. }
  155. gpr_strvec_add(buf, tmp);
  156. switch (e->type) {
  157. case GRPC_OP_COMPLETE:
  158. gpr_asprintf(&tmp, "GRPC_OP_COMPLETE success=%d %s:%d", e->success,
  159. e->file, e->line);
  160. gpr_strvec_add(buf, tmp);
  161. break;
  162. case GRPC_QUEUE_TIMEOUT:
  163. case GRPC_QUEUE_SHUTDOWN:
  164. gpr_log(GPR_ERROR, "not implemented");
  165. abort();
  166. break;
  167. }
  168. }
  169. static void expectations_to_strvec(gpr_strvec* buf, cq_verifier* v) {
  170. expectation* e;
  171. for (e = v->first_expectation; e != nullptr; e = e->next) {
  172. expectation_to_strvec(buf, e);
  173. gpr_strvec_add(buf, gpr_strdup("\n"));
  174. }
  175. }
  176. static void fail_no_event_received(cq_verifier* v) {
  177. gpr_strvec buf;
  178. char* msg;
  179. gpr_strvec_init(&buf);
  180. gpr_strvec_add(&buf, gpr_strdup("no event received, but expected:\n"));
  181. expectations_to_strvec(&buf, v);
  182. msg = gpr_strvec_flatten(&buf, nullptr);
  183. gpr_log(GPR_ERROR, "%s", msg);
  184. gpr_strvec_destroy(&buf);
  185. gpr_free(msg);
  186. abort();
  187. }
  188. static void verify_matches(expectation* e, grpc_event* ev) {
  189. GPR_ASSERT(e->type == ev->type);
  190. switch (e->type) {
  191. case GRPC_OP_COMPLETE:
  192. if (e->success != ev->success) {
  193. gpr_strvec expected;
  194. gpr_strvec_init(&expected);
  195. expectation_to_strvec(&expected, e);
  196. char* s = gpr_strvec_flatten(&expected, nullptr);
  197. gpr_strvec_destroy(&expected);
  198. gpr_log(GPR_ERROR, "actual success does not match expected: %s", s);
  199. gpr_free(s);
  200. abort();
  201. }
  202. break;
  203. case GRPC_QUEUE_SHUTDOWN:
  204. gpr_log(GPR_ERROR, "premature queue shutdown");
  205. abort();
  206. break;
  207. case GRPC_QUEUE_TIMEOUT:
  208. gpr_log(GPR_ERROR, "not implemented");
  209. abort();
  210. break;
  211. }
  212. }
  213. void cq_verify(cq_verifier* v) {
  214. const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
  215. while (v->first_expectation != nullptr) {
  216. grpc_event ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
  217. if (ev.type == GRPC_QUEUE_TIMEOUT) {
  218. fail_no_event_received(v);
  219. break;
  220. }
  221. expectation* e;
  222. expectation* prev = nullptr;
  223. for (e = v->first_expectation; e != nullptr; e = e->next) {
  224. if (e->tag == ev.tag) {
  225. verify_matches(e, &ev);
  226. if (e == v->first_expectation) v->first_expectation = e->next;
  227. if (prev != nullptr) prev->next = e->next;
  228. gpr_free(e);
  229. break;
  230. }
  231. prev = e;
  232. }
  233. if (e == nullptr) {
  234. char* s = grpc_event_string(&ev);
  235. gpr_log(GPR_ERROR, "cq returned unexpected event: %s", s);
  236. gpr_free(s);
  237. gpr_strvec expectations;
  238. gpr_strvec_init(&expectations);
  239. expectations_to_strvec(&expectations, v);
  240. s = gpr_strvec_flatten(&expectations, nullptr);
  241. gpr_strvec_destroy(&expectations);
  242. gpr_log(GPR_ERROR, "expected tags:\n%s", s);
  243. gpr_free(s);
  244. abort();
  245. }
  246. }
  247. }
  248. void cq_verify_empty_timeout(cq_verifier* v, int timeout_sec) {
  249. gpr_timespec deadline =
  250. gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  251. gpr_time_from_seconds(timeout_sec, GPR_TIMESPAN));
  252. grpc_event ev;
  253. GPR_ASSERT(v->first_expectation == nullptr &&
  254. "expectation queue must be empty");
  255. ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
  256. if (ev.type != GRPC_QUEUE_TIMEOUT) {
  257. char* s = grpc_event_string(&ev);
  258. gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s", s);
  259. gpr_free(s);
  260. abort();
  261. }
  262. }
  263. void cq_verify_empty(cq_verifier* v) { cq_verify_empty_timeout(v, 1); }
  264. static void add(cq_verifier* v, const char* file, int line,
  265. grpc_completion_type type, void* tag, bool success) {
  266. expectation* e = static_cast<expectation*>(gpr_malloc(sizeof(expectation)));
  267. e->type = type;
  268. e->file = file;
  269. e->line = line;
  270. e->tag = tag;
  271. e->success = success;
  272. e->next = v->first_expectation;
  273. v->first_expectation = e;
  274. }
  275. void cq_expect_completion(cq_verifier* v, const char* file, int line, void* tag,
  276. bool success) {
  277. add(v, file, line, GRPC_OP_COMPLETE, tag, success);
  278. }