blob: 1dd83689b96ef4bde0152eec0278b73b9c532ec5 [file]
// Copyright 2023 Google Inc. All Rights Reserved.
//
// Licensed 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.
#include "persistent_service.h"
#include "ipc_utils.h"
#include "persistent_service_test_lib.cc"
#include "test.h"
#include "util.h"
#ifndef _WIN32
#include <unistd.h> // For sleep()
#endif
static std::string kTestServiceName = persistent_service_test::kServiceName;
class PersistentServiceServerTest : public ::testing::Test {
public:
PersistentServiceServerTest() : server(kTestServiceName) {}
PersistentService::Server server;
};
class PersistentServiceClientTest : public ::testing::Test {
public:
PersistentServiceClientTest() : client(kTestServiceName) {}
void TearDown() override {
// Ensure any server process started by the test is stopped.
std::string err;
if (!client.StopServer(&err)) {
// This fails when there is no server, or when the server
// is shutting down but still alive (in which case the connection
// will be refused).
return;
}
ASSERT_TRUE(client.WaitForServerShutdown()) << "Could not shutdown server!";
}
#ifndef _WIN32
SigPipeBlocker ignore_sigpipe_;
#endif // !_WIN32
PersistentService::Client client;
};
TEST_F(PersistentServiceServerTest, BindService) {
std::string error;
ASSERT_TRUE(server.BindService(&error)) << error;
PersistentService::Client client(kTestServiceName);
EXPECT_TRUE(client.HasServer());
}
TEST_F(PersistentServiceServerTest, BindServiceWithLogs) {
std::string error;
std::string logs;
server.SetLogger(Logger::CreateForString(&logs));
EXPECT_TRUE(server.BindService(&error));
PersistentService::Client client(kTestServiceName);
EXPECT_TRUE(client.HasServer());
EXPECT_EQ(logs,
"Trying to start local exclusive service: "
"persistent-server-test\nGot it!\n");
}
TEST_F(PersistentServiceClientTest, DefaultMode) {
std::string error;
auto config = persistent_service_test::GetServerConfig();
ScopedHandle conn = client.Connect(config, &error);
ASSERT_TRUE(conn) << error;
EXPECT_TRUE(client.HasServer());
// Run the test.
ASSERT_TRUE(persistent_service_test::RunTest(conn, &error)) << error;
EXPECT_TRUE(client.HasServer());
// The server closes its peer handle after running the test,
// so running again with the same connection should fail, even
// though the server is still running.
ASSERT_FALSE(persistent_service_test::RunTest(conn, &error)) << error;
EXPECT_TRUE(client.HasServer());
// Create new client connection.
conn = client.Connect(config, &error);
ASSERT_TRUE(conn) << error;
// Run the test again with the new connection.
ASSERT_TRUE(persistent_service_test::RunTest(conn, &error)) << error;
ASSERT_TRUE(client.HasServer());
// Tell the server to stop. This is handled by the service handler.
conn = client.Connect(config, &error);
ASSERT_TRUE(persistent_service_test::StopServer(conn, &error)) << error;
conn.Close();
ASSERT_TRUE(client.WaitForServerShutdown());
ASSERT_FALSE(client.HasServer());
}
TEST_F(PersistentServiceClientTest, SuccessiveConnections) {
std::string error;
auto config = persistent_service_test::GetServerConfig();
ScopedHandle conn;
for (size_t n = 0; n < 10; ++n) {
std::string info = std::string("#") + std::to_string(n) + ": ";
conn = client.Connect(config, &error);
ASSERT_TRUE(conn) << info << error;
EXPECT_TRUE(client.HasServer()) << info;
ASSERT_TRUE(persistent_service_test::RunTest(conn, &error))
<< info << error;
}
conn = client.Connect(config, &error);
ASSERT_TRUE(conn) << error;
ASSERT_TRUE(persistent_service_test::StopServer(conn, &error)) << error;
conn.Close();
ASSERT_TRUE(client.WaitForServerShutdown());
ASSERT_FALSE(client.HasServer());
}
TEST_F(PersistentServiceClientTest, WithConnectionTimeout) {
std::string error;
auto config = persistent_service_test::GetServerConfig();
// Verify that the server shuts down gracefully after a 100ms timeout.
persistent_service_test::SetServiceConfigTimeoutMs(config, 100);
ScopedHandle conn = client.Connect(config, &error);
ASSERT_TRUE(conn) << error;
ASSERT_TRUE(persistent_service_test::RunTest(conn, &error)) << error;
conn.Close();
#ifdef _WIN32
::Sleep(1000);
#else
::sleep(1);
#endif
EXPECT_FALSE(client.HasServer());
}
TEST_F(PersistentServiceClientTest, WithDifferentVersion) {
// First start an instance with the default version.
std::string error;
auto config = persistent_service_test::GetServerConfig();
ScopedHandle conn = client.Connect(config, &error);
ASSERT_TRUE(conn) << error;
ASSERT_TRUE(persistent_service_test::RunTest(conn, &error));
EXPECT_TRUE(client.HasServer());
conn.Close();
persistent_service_test::SetServiceConfigVersion(config, "another");
conn = client.Connect(config, &error);
ASSERT_TRUE(conn) << error;
EXPECT_TRUE(client.HasServer());
}