1 //
2 //
3 // Copyright 2020 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
19 #include <grpc/status.h>
20
21 #include <memory>
22
23 #include "gtest/gtest.h"
24 #include "src/core/util/time.h"
25 #include "test/core/end2end/end2end_tests.h"
26
27 namespace grpc_core {
28 namespace {
29
30 // Client streaming test where the client sends a bunch of messages and the
31 // server reads them. After reading some messages, the server sends the status.
32 // Client writes fail after that due to the end of stream and the client
33 // subsequently requests and receives the status.
ClientStreaming(CoreEnd2endTest & test,int messages)34 void ClientStreaming(CoreEnd2endTest& test, int messages) {
35 auto c = test.NewClientCall("/foo").Timeout(Duration::Seconds(30)).Create();
36
37 IncomingMetadata server_initial_metadata;
38 c.NewBatch(1).SendInitialMetadata({}).RecvInitialMetadata(
39 server_initial_metadata);
40 auto s = test.RequestCall(100);
41 test.Expect(100, true);
42 test.Step();
43 s.NewBatch(101).SendInitialMetadata({});
44 test.Expect(101, true);
45 test.Expect(1, true);
46 test.Step();
47 // Client writes bunch of messages and server reads them
48 for (int i = 0; i < messages; i++) {
49 c.NewBatch(2).SendMessage("hello world");
50 IncomingMessage client_message;
51 s.NewBatch(102).RecvMessage(client_message);
52 test.Expect(2, true);
53 test.Expect(102, true);
54 test.Step();
55 EXPECT_EQ(client_message.payload(), "hello world");
56 }
57
58 // Server sends status denoting end of stream
59 s.NewBatch(103).SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {});
60 test.Expect(103, true);
61 test.Step();
62 // Do an empty verify to make sure that the client receives the status
63 test.Step();
64
65 // Client tries sending another message which should fail
66 c.NewBatch(3).SendMessage("hello world");
67 test.Expect(3, false);
68 test.Step();
69
70 // Client sends close and requests status
71 IncomingStatusOnClient server_status;
72 c.NewBatch(4).SendCloseFromClient().RecvStatusOnClient(server_status);
73 test.Expect(4, true);
74 test.Step();
75 EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
76 EXPECT_EQ(server_status.message(), "xyz");
77 }
78
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming0)79 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming0) {
80 ClientStreaming(*this, 0);
81 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming1)82 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming1) {
83 ClientStreaming(*this, 1);
84 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming3)85 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming3) {
86 ClientStreaming(*this, 3);
87 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming10)88 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming10) {
89 ClientStreaming(*this, 10);
90 }
CORE_END2END_TEST(CoreEnd2endTest,ClientStreaming30)91 CORE_END2END_TEST(CoreEnd2endTest, ClientStreaming30) {
92 ClientStreaming(*this, 30);
93 }
94
95 } // namespace
96 } // namespace grpc_core
97