concurrent_connectivity_test.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include <grpc/grpc.h>
  3. #include <grpc/support/alloc.h>
  4. #include <grpc/support/thd.h>
  5. #define NUM_THREADS 100
  6. static grpc_channel* channels[NUM_THREADS];
  7. static grpc_completion_queue* queues[NUM_THREADS];
  8. void create_loop_destroy(void* actually_an_int) {
  9. int thread_index = (int)(actually_an_int);
  10. for (int i = 0; i < 10; ++i) {
  11. grpc_completion_queue* cq = grpc_completion_queue_create(NULL);
  12. grpc_channel* chan = grpc_insecure_channel_create("localhost", NULL, NULL);
  13. channels[thread_index] = chan;
  14. queues[thread_index] = cq;
  15. gpr_timespec inf_future = gpr_inf_future(GPR_CLOCK_REALTIME);
  16. gpr_timespec delta = gpr_time_from_millis(10, GPR_TIMESPAN);
  17. for (int j = 0; j < 10; ++j) {
  18. gpr_timespec later_time =
  19. gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), delta);
  20. grpc_connectivity_state state =
  21. grpc_channel_check_connectivity_state(chan, 1);
  22. grpc_channel_watch_connectivity_state(chan, state, later_time, cq, NULL);
  23. grpc_completion_queue_next(cq, inf_future, NULL);
  24. }
  25. grpc_channel_destroy(channels[thread_index]);
  26. grpc_completion_queue_destroy(queues[thread_index]);
  27. }
  28. }
  29. int main() {
  30. grpc_init();
  31. gpr_thd_id threads[NUM_THREADS];
  32. for (intptr_t i = 0; i < NUM_THREADS; ++i) {
  33. gpr_thd_options options = gpr_thd_options_default();
  34. gpr_thd_options_set_joinable(&options);
  35. gpr_thd_new(&threads[i], create_loop_destroy, (void*)i, &options);
  36. }
  37. for (int i = 0; i < NUM_THREADS; ++i) {
  38. gpr_thd_join(threads[i]);
  39. }
  40. grpc_shutdown();
  41. return 0;
  42. }