aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/transport/byte_stream_test.cc
blob: 2aab6e926282a3a5a64ad6f46a549e48507d977a (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 *
 * Copyright 2017 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/transport/byte_stream.h"

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

#include "src/core/lib/slice/slice_internal.h"

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

//
// grpc_slice_buffer_stream tests
//

static void not_called_closure(void* arg, grpc_error* error) {
  GPR_ASSERT(false);
}

static void test_slice_buffer_stream_basic(void) {
  gpr_log(GPR_DEBUG, "test_slice_buffer_stream_basic");
  grpc_core::ExecCtx exec_ctx;
  // Create and populate slice buffer.
  grpc_slice_buffer buffer;
  grpc_slice_buffer_init(&buffer);
  grpc_slice input[] = {
      grpc_slice_from_static_string("foo"),
      grpc_slice_from_static_string("bar"),
  };
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    grpc_slice_buffer_add(&buffer, input[i]);
  }
  // Create byte stream.
  grpc_slice_buffer_stream stream;
  grpc_slice_buffer_stream_init(&stream, &buffer, 0);
  GPR_ASSERT(stream.base.length == 6);
  grpc_closure closure;
  GRPC_CLOSURE_INIT(&closure, not_called_closure, nullptr,
                    grpc_schedule_on_exec_ctx);
  // Read each slice.  Note that next() always returns synchronously.
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    GPR_ASSERT(grpc_byte_stream_next(&stream.base, ~(size_t)0, &closure));
    grpc_slice output;
    grpc_error* error = grpc_byte_stream_pull(&stream.base, &output);
    GPR_ASSERT(error == GRPC_ERROR_NONE);
    GPR_ASSERT(grpc_slice_eq(input[i], output));
    grpc_slice_unref_internal(output);
  }
  // Clean up.
  grpc_byte_stream_destroy(&stream.base);
  grpc_slice_buffer_destroy_internal(&buffer);
}

static void test_slice_buffer_stream_shutdown(void) {
  gpr_log(GPR_DEBUG, "test_slice_buffer_stream_shutdown");
  grpc_core::ExecCtx exec_ctx;
  // Create and populate slice buffer.
  grpc_slice_buffer buffer;
  grpc_slice_buffer_init(&buffer);
  grpc_slice input[] = {
      grpc_slice_from_static_string("foo"),
      grpc_slice_from_static_string("bar"),
  };
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    grpc_slice_buffer_add(&buffer, input[i]);
  }
  // Create byte stream.
  grpc_slice_buffer_stream stream;
  grpc_slice_buffer_stream_init(&stream, &buffer, 0);
  GPR_ASSERT(stream.base.length == 6);
  grpc_closure closure;
  GRPC_CLOSURE_INIT(&closure, not_called_closure, nullptr,
                    grpc_schedule_on_exec_ctx);
  // Read the first slice.
  GPR_ASSERT(grpc_byte_stream_next(&stream.base, ~(size_t)0, &closure));
  grpc_slice output;
  grpc_error* error = grpc_byte_stream_pull(&stream.base, &output);
  GPR_ASSERT(error == GRPC_ERROR_NONE);
  GPR_ASSERT(grpc_slice_eq(input[0], output));
  grpc_slice_unref_internal(output);
  // Now shutdown.
  grpc_error* shutdown_error =
      GRPC_ERROR_CREATE_FROM_STATIC_STRING("shutdown error");
  grpc_byte_stream_shutdown(&stream.base, GRPC_ERROR_REF(shutdown_error));
  // After shutdown, the next pull() should return the error.
  GPR_ASSERT(grpc_byte_stream_next(&stream.base, ~(size_t)0, &closure));
  error = grpc_byte_stream_pull(&stream.base, &output);
  GPR_ASSERT(error == shutdown_error);
  GRPC_ERROR_UNREF(error);
  GRPC_ERROR_UNREF(shutdown_error);
  // Clean up.
  grpc_byte_stream_destroy(&stream.base);
  grpc_slice_buffer_destroy_internal(&buffer);
}

//
// grpc_caching_byte_stream tests
//

static void test_caching_byte_stream_basic(void) {
  gpr_log(GPR_DEBUG, "test_caching_byte_stream_basic");
  grpc_core::ExecCtx exec_ctx;
  // Create and populate slice buffer byte stream.
  grpc_slice_buffer buffer;
  grpc_slice_buffer_init(&buffer);
  grpc_slice input[] = {
      grpc_slice_from_static_string("foo"),
      grpc_slice_from_static_string("bar"),
  };
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    grpc_slice_buffer_add(&buffer, input[i]);
  }
  grpc_slice_buffer_stream underlying_stream;
  grpc_slice_buffer_stream_init(&underlying_stream, &buffer, 0);
  // Create cache and caching stream.
  grpc_byte_stream_cache cache;
  grpc_byte_stream_cache_init(&cache, &underlying_stream.base);
  grpc_caching_byte_stream stream;
  grpc_caching_byte_stream_init(&stream, &cache);
  grpc_closure closure;
  GRPC_CLOSURE_INIT(&closure, not_called_closure, nullptr,
                    grpc_schedule_on_exec_ctx);
  // Read each slice.  Note that next() always returns synchronously,
  // because the underlying byte stream always does.
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    GPR_ASSERT(grpc_byte_stream_next(&stream.base, ~(size_t)0, &closure));
    grpc_slice output;
    grpc_error* error = grpc_byte_stream_pull(&stream.base, &output);
    GPR_ASSERT(error == GRPC_ERROR_NONE);
    GPR_ASSERT(grpc_slice_eq(input[i], output));
    grpc_slice_unref_internal(output);
  }
  // Clean up.
  grpc_byte_stream_destroy(&stream.base);
  grpc_byte_stream_cache_destroy(&cache);
  grpc_slice_buffer_destroy_internal(&buffer);
}

static void test_caching_byte_stream_reset(void) {
  gpr_log(GPR_DEBUG, "test_caching_byte_stream_reset");
  grpc_core::ExecCtx exec_ctx;
  // Create and populate slice buffer byte stream.
  grpc_slice_buffer buffer;
  grpc_slice_buffer_init(&buffer);
  grpc_slice input[] = {
      grpc_slice_from_static_string("foo"),
      grpc_slice_from_static_string("bar"),
  };
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    grpc_slice_buffer_add(&buffer, input[i]);
  }
  grpc_slice_buffer_stream underlying_stream;
  grpc_slice_buffer_stream_init(&underlying_stream, &buffer, 0);
  // Create cache and caching stream.
  grpc_byte_stream_cache cache;
  grpc_byte_stream_cache_init(&cache, &underlying_stream.base);
  grpc_caching_byte_stream stream;
  grpc_caching_byte_stream_init(&stream, &cache);
  grpc_closure closure;
  GRPC_CLOSURE_INIT(&closure, not_called_closure, nullptr,
                    grpc_schedule_on_exec_ctx);
  // Read one slice.
  GPR_ASSERT(grpc_byte_stream_next(&stream.base, ~(size_t)0, &closure));
  grpc_slice output;
  grpc_error* error = grpc_byte_stream_pull(&stream.base, &output);
  GPR_ASSERT(error == GRPC_ERROR_NONE);
  GPR_ASSERT(grpc_slice_eq(input[0], output));
  grpc_slice_unref_internal(output);
  // Reset the caching stream.  The reads should start over from the
  // first slice.
  grpc_caching_byte_stream_reset(&stream);
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    GPR_ASSERT(grpc_byte_stream_next(&stream.base, ~(size_t)0, &closure));
    error = grpc_byte_stream_pull(&stream.base, &output);
    GPR_ASSERT(error == GRPC_ERROR_NONE);
    GPR_ASSERT(grpc_slice_eq(input[i], output));
    grpc_slice_unref_internal(output);
  }
  // Clean up.
  grpc_byte_stream_destroy(&stream.base);
  grpc_byte_stream_cache_destroy(&cache);
  grpc_slice_buffer_destroy_internal(&buffer);
}

static void test_caching_byte_stream_shared_cache(void) {
  gpr_log(GPR_DEBUG, "test_caching_byte_stream_shared_cache");
  grpc_core::ExecCtx exec_ctx;
  // Create and populate slice buffer byte stream.
  grpc_slice_buffer buffer;
  grpc_slice_buffer_init(&buffer);
  grpc_slice input[] = {
      grpc_slice_from_static_string("foo"),
      grpc_slice_from_static_string("bar"),
  };
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    grpc_slice_buffer_add(&buffer, input[i]);
  }
  grpc_slice_buffer_stream underlying_stream;
  grpc_slice_buffer_stream_init(&underlying_stream, &buffer, 0);
  // Create cache and two caching streams.
  grpc_byte_stream_cache cache;
  grpc_byte_stream_cache_init(&cache, &underlying_stream.base);
  grpc_caching_byte_stream stream1;
  grpc_caching_byte_stream_init(&stream1, &cache);
  grpc_caching_byte_stream stream2;
  grpc_caching_byte_stream_init(&stream2, &cache);
  grpc_closure closure;
  GRPC_CLOSURE_INIT(&closure, not_called_closure, nullptr,
                    grpc_schedule_on_exec_ctx);
  // Read one slice from stream1.
  GPR_ASSERT(grpc_byte_stream_next(&stream1.base, ~(size_t)0, &closure));
  grpc_slice output;
  grpc_error* error = grpc_byte_stream_pull(&stream1.base, &output);
  GPR_ASSERT(error == GRPC_ERROR_NONE);
  GPR_ASSERT(grpc_slice_eq(input[0], output));
  grpc_slice_unref_internal(output);
  // Read all slices from stream2.
  for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
    GPR_ASSERT(grpc_byte_stream_next(&stream2.base, ~(size_t)0, &closure));
    error = grpc_byte_stream_pull(&stream2.base, &output);
    GPR_ASSERT(error == GRPC_ERROR_NONE);
    GPR_ASSERT(grpc_slice_eq(input[i], output));
    grpc_slice_unref_internal(output);
  }
  // Now read the second slice from stream1.
  GPR_ASSERT(grpc_byte_stream_next(&stream1.base, ~(size_t)0, &closure));
  error = grpc_byte_stream_pull(&stream1.base, &output);
  GPR_ASSERT(error == GRPC_ERROR_NONE);
  GPR_ASSERT(grpc_slice_eq(input[1], output));
  grpc_slice_unref_internal(output);
  // Clean up.
  grpc_byte_stream_destroy(&stream1.base);
  grpc_byte_stream_destroy(&stream2.base);
  grpc_byte_stream_cache_destroy(&cache);
  grpc_slice_buffer_destroy_internal(&buffer);
}

int main(int argc, char** argv) {
  grpc_init();
  grpc_test_init(argc, argv);
  test_slice_buffer_stream_basic();
  test_slice_buffer_stream_shutdown();
  test_caching_byte_stream_basic();
  test_caching_byte_stream_reset();
  test_caching_byte_stream_shared_cache();
  grpc_shutdown();
  return 0;
}