wasphin commented on code in PR #3485:
URL: https://github.com/apache/brpc/pull/3485#discussion_r3844953327


##########
src/brpc/controller.cpp:
##########
@@ -1711,6 +1711,12 @@ void Controller::HandleStreamConnection(Socket 
*host_socket) {
             if (!FailedInline()) {
                 SetFailed(EREQUEST, "The server didn't accept the stream");
             }
+        } else if (_remote_stream_settings->extra_stream_ids_size() !=
+                   (int)stream_num - 1) {
+            SetFailed(ERESPONSE, "Server returned %d extra_stream_ids, "
+                      "expected %d",
+                      _remote_stream_settings->extra_stream_ids_size(),
+                      (int)stream_num - 1);

Review Comment:
   Addressed in 186ec05b. `stream_num == 0` is already excluded by the early 
`_request_streams.empty()` return. The expected count is now retained as 
`size_t`, the protobuf count is converted to the common type for comparison, 
and the diagnostic uses `%zu`.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1130,6 +1131,74 @@ class MyServiceWithExtraStream : public 
test::EchoService {
     int _n;
 };
 
+class MyServiceWithMismatchedExtraStreamIds : public test::EchoService {
+public:
+    MyServiceWithMismatchedExtraStreamIds(size_t stream_count, int adjustment)
+        : _stream_count(stream_count), _adjustment(adjustment) {}
+
+    void Echo(::google::protobuf::RpcController* controller,
+              const ::test::EchoRequest* request,
+              ::test::EchoResponse* response,
+              ::google::protobuf::Closure* done) override {
+        brpc::ClosureGuard done_guard(done);
+        brpc::Controller* cntl = static_cast<brpc::Controller*>(controller);
+        response->set_message(request->message());
+
+        brpc::ControllerPrivateAccessor accessor(cntl);
+        brpc::StreamSettings* settings = accessor.remote_stream_settings();
+        if (_adjustment < 0) {
+            settings->mutable_extra_stream_ids()->RemoveLast();
+        } else {
+            settings->add_extra_stream_ids(settings->extra_stream_ids(0));
+        }
+
+        brpc::StreamIds response_streams;
+        ASSERT_EQ(0, brpc::StreamAccept(response_streams, *cntl, nullptr));
+        ASSERT_EQ((int)_stream_count + _adjustment,
+                  (int)response_streams.size());
+    }
+
+private:
+    size_t _stream_count;
+    int _adjustment;
+};
+
+TEST_F(StreamingRpcTest, reject_mismatched_returned_stream_identifiers) {
+    const size_t STREAM_COUNT = 3;
+
+    for (int adjustment : {-1, 1}) {
+        brpc::Server server;
+        MyServiceWithMismatchedExtraStreamIds service(STREAM_COUNT, 
adjustment);
+        ASSERT_EQ(0, server.AddService(
+            &service, brpc::SERVER_DOESNT_OWN_SERVICE));
+        ASSERT_EQ(0, server.Start(0, nullptr));
+
+        brpc::Channel channel;
+        ASSERT_EQ(0, channel.Init(server.listen_address(), nullptr));
+
+        brpc::Controller cntl;
+        brpc::StreamIds request_streams;
+        ASSERT_EQ(0, brpc::StreamCreate(request_streams, STREAM_COUNT, cntl,
+                                        nullptr));
+        ASSERT_EQ(STREAM_COUNT, request_streams.size());
+
+        test::EchoService_Stub stub(&channel);
+        stub.Echo(&cntl, &request, &response, nullptr);
+        ASSERT_TRUE(cntl.Failed());
+        ASSERT_EQ(brpc::ERESPONSE, cntl.ErrorCode());
+        ASSERT_NE(std::string::npos,
+                  cntl.ErrorText().find("extra_stream_ids, expected 2"));

Review Comment:
   Addressed in 186ec05b. The expected diagnostic substring is now derived from 
`STREAM_COUNT - 1` instead of hard-coding `2`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to