blob: 588522389483ed5e31b082db5f3b60dccd1b58ff [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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/transport/stream_op.h"
35
36#include <string.h>
37
38#include <grpc/support/log.h>
39#include "test/core/util/test_config.h"
40
41static void flow_ctl_cb_fails(void *ignored, grpc_op_error error) {
42 GPR_ASSERT(error == GRPC_OP_ERROR);
43}
44
45static void assert_slices_equal(gpr_slice a, gpr_slice b) {
46 GPR_ASSERT(a.refcount == b.refcount);
47 if (a.refcount) {
48 GPR_ASSERT(a.data.refcounted.bytes == b.data.refcounted.bytes);
49 GPR_ASSERT(a.data.refcounted.length == b.data.refcounted.length);
50 } else {
51 GPR_ASSERT(a.data.inlined.length == b.data.inlined.length);
52 GPR_ASSERT(0 == memcmp(a.data.inlined.bytes, b.data.inlined.bytes,
53 a.data.inlined.length));
54 }
55}
56
57int main(int argc, char **argv) {
58 /* some basic test data */
59 gpr_slice test_slice_1 = gpr_slice_malloc(1);
60 gpr_slice test_slice_2 = gpr_slice_malloc(2);
61 gpr_slice test_slice_3 = gpr_slice_malloc(3);
62 gpr_slice test_slice_4 = gpr_slice_malloc(4);
63 char x;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +010064 unsigned i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065
66 grpc_stream_op_buffer buf;
67 grpc_stream_op_buffer buf2;
68
69 grpc_test_init(argc, argv);
70 /* initialize one of our buffers */
71 grpc_sopb_init(&buf);
72 /* it should start out empty */
73 GPR_ASSERT(buf.nops == 0);
74
75 /* add some data to the buffer */
76 grpc_sopb_add_begin_message(&buf, 1, 2);
77 grpc_sopb_add_slice(&buf, test_slice_1);
78 grpc_sopb_add_slice(&buf, test_slice_2);
79 grpc_sopb_add_slice(&buf, test_slice_3);
80 grpc_sopb_add_slice(&buf, test_slice_4);
81 grpc_sopb_add_flow_ctl_cb(&buf, flow_ctl_cb_fails, &x);
82 grpc_sopb_add_no_op(&buf);
83
84 /* verify that the data went in ok */
85 GPR_ASSERT(buf.nops == 7);
86 GPR_ASSERT(buf.ops[0].type == GRPC_OP_BEGIN_MESSAGE);
87 GPR_ASSERT(buf.ops[0].data.begin_message.length == 1);
88 GPR_ASSERT(buf.ops[0].data.begin_message.flags == 2);
89 GPR_ASSERT(buf.ops[1].type == GRPC_OP_SLICE);
90 assert_slices_equal(buf.ops[1].data.slice, test_slice_1);
91 GPR_ASSERT(buf.ops[2].type == GRPC_OP_SLICE);
92 assert_slices_equal(buf.ops[2].data.slice, test_slice_2);
93 GPR_ASSERT(buf.ops[3].type == GRPC_OP_SLICE);
94 assert_slices_equal(buf.ops[3].data.slice, test_slice_3);
95 GPR_ASSERT(buf.ops[4].type == GRPC_OP_SLICE);
96 assert_slices_equal(buf.ops[4].data.slice, test_slice_4);
97 GPR_ASSERT(buf.ops[5].type == GRPC_OP_FLOW_CTL_CB);
98 GPR_ASSERT(buf.ops[5].data.flow_ctl_cb.cb == flow_ctl_cb_fails);
99 GPR_ASSERT(buf.ops[5].data.flow_ctl_cb.arg == &x);
100 GPR_ASSERT(buf.ops[6].type == GRPC_NO_OP);
101
102 /* initialize the second buffer */
103 grpc_sopb_init(&buf2);
104 /* add a no-op, and then the original buffer */
105 grpc_sopb_add_no_op(&buf2);
106 grpc_sopb_append(&buf2, buf.ops, buf.nops);
107 /* should be one element bigger than the original */
108 GPR_ASSERT(buf2.nops == buf.nops + 1);
109 GPR_ASSERT(buf2.ops[0].type == GRPC_NO_OP);
110 /* and the tail should be the same */
111 for (i = 0; i < buf.nops; i++) {
112 GPR_ASSERT(buf2.ops[i + 1].type == buf.ops[i].type);
113 }
114
115 /* destroy the buffers */
116 grpc_sopb_destroy(&buf);
117 grpc_sopb_destroy(&buf2);
118
119 gpr_slice_unref(test_slice_1);
120 gpr_slice_unref(test_slice_2);
121 gpr_slice_unref(test_slice_3);
122 gpr_slice_unref(test_slice_4);
123
124 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800125}