blob: 5fda0d565827677d050f6ae1129f237d034ab40e [file] [log] [blame]
// 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 "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.
if (!client.HasServer())
return;
std::string err;
if (!client.StopServer(&err)) {
fprintf(stderr, "\nERROR: %s", err.c_str());
AddAssertionFailure();
} else {
if (!client.WaitForServerShutdown()) {
fprintf(stderr, "\nERROR: Could not shutdown server!\n");
AddAssertionFailure();
}
}
}
PersistentService::Client client;
};
TEST_F(PersistentServiceServerTest, BindService) {
std::string error;
EXPECT_TRUE(server.BindService(&error));
PersistentService::Client client(kTestServiceName);
EXPECT_TRUE(client.HasServer());
}
TEST_F(PersistentServiceClientTest, DefaultMode) {
std::string error;
auto config = persistent_service_test::GetServerConfig();
IpcHandle conn = client.Connect(config, &error);
if (!conn)
fprintf(stderr, "ERROR [%s]\n", error.c_str());
ASSERT_TRUE(conn);
EXPECT_TRUE(client.HasServer());
bool success = persistent_service_test::RunTest(conn, false, &error);
if (!success)
fprintf(stderr, "ERROR: %s\n", error.c_str());
ASSERT_TRUE(success);
EXPECT_TRUE(client.HasServer());
success = persistent_service_test::RunTest(conn, true, &error);
if (!success)
fprintf(stderr, "ERROR: %s\n", error.c_str());
ASSERT_TRUE(success);
ASSERT_TRUE(client.WaitForServerShutdown());
ASSERT_FALSE(client.HasServer());
}
TEST_F(PersistentServiceClientTest, WithConnectionTimeout) {
std::string error;
auto config = persistent_service_test::GetServerConfig();
persistent_service_test::SetServiceConfigTimeoutMs(config, 100);
IpcHandle conn = client.Connect(config, &error);
if (!conn)
fprintf(stderr, "ERROR [%s]\n", error.c_str());
ASSERT_TRUE(conn);
bool success = persistent_service_test::RunTest(conn, false, &error);
if (!success)
fprintf(stderr, "ERROR: %s\n", error.c_str());
ASSERT_TRUE(success);
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();
IpcHandle conn = client.Connect(config, &error);
ASSERT_TRUE(conn);
bool success = persistent_service_test::RunTest(conn, false, &error);
ASSERT_TRUE(success);
EXPECT_TRUE(client.HasServer());
conn.Close();
persistent_service_test::SetServiceConfigVersion(config, "another");
conn = client.Connect(config, &error);
if (!conn)
fprintf(stderr, "ERROR: %s\n", error.c_str());
EXPECT_TRUE(conn);
EXPECT_TRUE(client.HasServer());
}