aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/transport/chttp2/context_list_test.cc
blob: 3814184e164c36021c36e4d753983c68ccae3394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "src/core/lib/iomgr/port.h"

#include <new>
#include <vector>

#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
#include "src/core/ext/transport/chttp2/transport/context_list.h"
#include "src/core/lib/transport/transport.h"
#include "test/core/util/mock_endpoint.h"
#include "test/core/util/test_config.h"

#include <grpc/grpc.h>

static void TestExecuteFlushesListVerifier(void* arg,
                                           grpc_core::Timestamps* ts) {
  GPR_ASSERT(arg != nullptr);
  gpr_atm* done = reinterpret_cast<gpr_atm*>(arg);
  gpr_atm_rel_store(done, static_cast<gpr_atm>(1));
}

static void discard_write(grpc_slice slice) {}

/** Tests that all ContextList elements in the list are flushed out on
 * execute.
 * Also tests that arg is passed correctly.
 */
static void TestExecuteFlushesList() {
  grpc_core::ContextList* list = nullptr;
  grpc_http2_set_write_timestamps_callback(TestExecuteFlushesListVerifier);
#define NUM_ELEM 5
  grpc_core::ExecCtx exec_ctx;
  grpc_stream_refcount ref;
  grpc_resource_quota* resource_quota =
      grpc_resource_quota_create("context_list_test");
  grpc_endpoint* mock_endpoint =
      grpc_mock_endpoint_create(discard_write, resource_quota);
  grpc_transport* t =
      grpc_create_chttp2_transport(nullptr, mock_endpoint, true);
  std::vector<grpc_chttp2_stream*> s;
  s.reserve(NUM_ELEM);
  gpr_atm verifier_called[NUM_ELEM];
  for (auto i = 0; i < NUM_ELEM; i++) {
    s.push_back(static_cast<grpc_chttp2_stream*>(
        gpr_malloc(grpc_transport_stream_size(t))));
    grpc_transport_init_stream(reinterpret_cast<grpc_transport*>(t),
                               reinterpret_cast<grpc_stream*>(s[i]), &ref,
                               nullptr, nullptr);
    s[i]->context = &verifier_called[i];
    gpr_atm_rel_store(&verifier_called[i], static_cast<gpr_atm>(0));
    grpc_core::ContextList::Append(&list, s[i]);
  }
  grpc_core::Timestamps ts;
  grpc_core::ContextList::Execute(list, &ts, GRPC_ERROR_NONE);
  for (auto i = 0; i < NUM_ELEM; i++) {
    GPR_ASSERT(gpr_atm_acq_load(&verifier_called[i]) ==
               static_cast<gpr_atm>(1));
    grpc_transport_destroy_stream(reinterpret_cast<grpc_transport*>(t),
                                  reinterpret_cast<grpc_stream*>(s[i]),
                                  nullptr);
    exec_ctx.Flush();
    gpr_free(s[i]);
  }
  grpc_transport_destroy(t);
  grpc_resource_quota_unref(resource_quota);
  exec_ctx.Flush();
}

static void TestContextList() { TestExecuteFlushesList(); }

int main(int argc, char** argv) {
  grpc_init();
  TestContextList();
  grpc_shutdown();
  return 0;
}