alamb commented on code in PR #3378:
URL: https://github.com/apache/arrow-rs/pull/3378#discussion_r1053618097


##########
arrow-flight/tests/common/server.rs:
##########
@@ -0,0 +1,229 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+use std::{
+    pin::Pin,
+    sync::{Arc, Mutex},
+};
+
+use futures::Stream;
+use tonic::{metadata::MetadataMap, Request, Response, Status, Streaming};
+
+use arrow_flight::{
+    flight_service_server::{FlightService, FlightServiceServer},
+    Action, ActionType, Criteria, Empty, FlightData, FlightDescriptor, 
FlightInfo,
+    HandshakeRequest, HandshakeResponse, PutResult, SchemaResult, Ticket,
+};
+
+#[derive(Debug, Clone)]
+/// Flight server for testing, with configurable responses
+pub struct TestFlightServer {
+    /// Shared state to configure responses
+    state: Arc<Mutex<State>>,
+}
+
+impl TestFlightServer {
+    /// Create a `TestFlightServer`
+    pub fn new() -> Self {
+        Self {
+            state: Arc::new(Mutex::new(State::new())),
+        }
+    }
+
+    /// Return an [`FlightServiceServer`] that can be used with a
+    /// [`Server`](tonic::transport::Server)
+    pub fn service(&self) -> FlightServiceServer<TestFlightServer> {
+        // wrap up tonic goop
+        FlightServiceServer::new(self.clone())
+    }
+
+    /// Specify the response returned from the next call to handshake
+    pub fn set_handshake_response(&self, response: Result<HandshakeResponse, 
Status>) {
+        let mut state = self.state.lock().expect("mutex not poisoned");
+
+        state.handshake_response.replace(response);
+    }
+
+    /// Take and return last handshake request send to the server,
+    pub fn take_handshake_request(&self) -> Option<HandshakeRequest> {
+        self.state
+            .lock()
+            .expect("mutex not poisoned")
+            .handshake_request
+            .take()
+    }
+
+    /// Specify the response returned from the next call to handshake
+    pub fn set_get_flight_info_response(&self, response: Result<FlightInfo, 
Status>) {
+        let mut state = self.state.lock().expect("mutex not poisoned");
+
+        state.get_flight_info_response.replace(response);
+    }
+
+    /// Take and return last get_flight_info request send to the server,
+    pub fn take_get_flight_info_request(&self) -> Option<FlightDescriptor> {
+        self.state
+            .lock()
+            .expect("mutex not poisoned")
+            .get_flight_info_request
+            .take()
+    }
+
+    /// Returns the last metadata from a request received by the server
+    pub fn take_last_request_metadata(&self) -> Option<MetadataMap> {
+        self.state
+            .lock()
+            .expect("mutex not poisoned")
+            .last_request_metadata
+            .take()
+    }
+
+    /// Save the last request's metadatacom
+    fn save_metadata<T>(&self, request: &Request<T>) {
+        let metadata = request.metadata().clone();
+        let mut state = self.state.lock().expect("mutex not poisoned");
+        state.last_request_metadata = Some(metadata);
+    }
+}
+
+/// mutable state for the TestFlightSwrver
+#[derive(Debug, Default)]
+struct State {
+    /// The last handshake request that was received
+    pub handshake_request: Option<HandshakeRequest>,
+    /// The next response to return from `handshake()`
+    pub handshake_response: Option<Result<HandshakeResponse, Status>>,
+    /// The last `get_flight_info` request received
+    pub get_flight_info_request: Option<FlightDescriptor>,
+    /// the next response  to return from `get_flight_info`
+    pub get_flight_info_response: Option<Result<FlightInfo, Status>>,
+    /// The last request headers received
+    pub last_request_metadata: Option<MetadataMap>,
+}
+
+impl State {
+    fn new() -> Self {
+        Default::default()
+    }
+}
+
+/// Implement the FlightService trait
+#[tonic::async_trait]
+impl FlightService for TestFlightServer {
+    type HandshakeStream = Pin<
+        Box<dyn Stream<Item = Result<HandshakeResponse, Status>> + Send + Sync 
+ 'static>,
+    >;

Review Comment:
   in 901bd7556



-- 
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]

Reply via email to