• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //       http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "GRPCVehicleHardware.h"
16 #include "VehicleServer.grpc.pb.h"
17 #include "VehicleServer.pb.h"
18 
19 #include <gmock/gmock.h>
20 #include <grpc++/grpc++.h>
21 #include <gtest/gtest.h>
22 
23 #include <chrono>
24 #include <memory>
25 #include <string>
26 
27 namespace android::hardware::automotive::vehicle::virtualization {
28 
29 const std::string kFakeServerAddr = "0.0.0.0:54321";
30 
31 class FakeVehicleServer : public proto::VehicleServer::Service {
32   public:
StartPropertyValuesStream(::grpc::ServerContext * context,const::google::protobuf::Empty * request,::grpc::ServerWriter<proto::VehiclePropValues> * stream)33     ::grpc::Status StartPropertyValuesStream(
34             ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
35             ::grpc::ServerWriter<proto::VehiclePropValues>* stream) override {
36         stream->Write(proto::VehiclePropValues());
37         // A fake disconnection.
38         return ::grpc::Status(::grpc::StatusCode::ABORTED, "Connection lost.");
39     }
40 
41     // Functions that we do not care.
GetAllPropertyConfig(::grpc::ServerContext * context,const::google::protobuf::Empty * request,::grpc::ServerWriter<proto::VehiclePropConfig> * stream)42     ::grpc::Status GetAllPropertyConfig(
43             ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
44             ::grpc::ServerWriter<proto::VehiclePropConfig>* stream) override {
45         return ::grpc::Status::OK;
46     }
47 
SetValues(::grpc::ServerContext * context,const proto::VehiclePropValueRequests * requests,proto::SetValueResults * results)48     ::grpc::Status SetValues(::grpc::ServerContext* context,
49                              const proto::VehiclePropValueRequests* requests,
50                              proto::SetValueResults* results) override {
51         return ::grpc::Status::OK;
52     }
53 
GetValues(::grpc::ServerContext * context,const proto::VehiclePropValueRequests * requests,proto::GetValueResults * results)54     ::grpc::Status GetValues(::grpc::ServerContext* context,
55                              const proto::VehiclePropValueRequests* requests,
56                              proto::GetValueResults* results) override {
57         return ::grpc::Status::OK;
58     }
59 };
60 
TEST(GRPCVehicleHardwareUnitTest,Reconnect)61 TEST(GRPCVehicleHardwareUnitTest, Reconnect) {
62     auto receivedUpdate = std::make_shared<std::atomic<int>>(0);
63     auto vehicleHardware = std::make_unique<GRPCVehicleHardware>(kFakeServerAddr);
64     vehicleHardware->registerOnPropertyChangeEvent(
65             std::make_unique<const IVehicleHardware::PropertyChangeCallback>(
66                     [receivedUpdate](const auto&) { receivedUpdate->fetch_add(1); }));
67 
68     constexpr size_t kServerRestartTimes = 5;
69     for (size_t serverStart = 0; serverStart < kServerRestartTimes; ++serverStart) {
70         EXPECT_EQ(receivedUpdate->load(), 0);
71         auto fakeServer = std::make_unique<FakeVehicleServer>();
72         ::grpc::ServerBuilder builder;
73         builder.RegisterService(fakeServer.get());
74         builder.AddListeningPort(kFakeServerAddr, ::grpc::InsecureServerCredentials());
75         auto grpcServer = builder.BuildAndStart();
76 
77         // Wait until the vehicle hardware received the second update (after one fake
78         // disconnection).
79         constexpr auto kMaxWaitTime = std::chrono::seconds(5);
80         auto startTime = std::chrono::steady_clock::now();
81         while (receivedUpdate->load() <= 1 &&
82                std::chrono::steady_clock::now() - startTime < kMaxWaitTime)
83             ;
84 
85         grpcServer->Shutdown();
86         grpcServer->Wait();
87         EXPECT_GT(receivedUpdate->load(), 1);
88 
89         // Reset for the next round.
90         receivedUpdate->store(0);
91     }
92 }
93 
94 }  // namespace android::hardware::automotive::vehicle::virtualization
95