Skip to content

Commit

Permalink
Move LaunchServer and WaitForServerStartup to lib/server/ServerContro…
Browse files Browse the repository at this point in the history
…l.cpp
  • Loading branch information
qris committed Jan 14, 2019
1 parent 05b9202 commit a9bc580
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 150 deletions.
16 changes: 8 additions & 8 deletions lib/backupstore/StoreTestUtils.cpp
Expand Up @@ -266,11 +266,11 @@ bool check_reference_counts()
return counts_ok;
}

bool StartServer()
bool StartServer(const std::string& daemon_args)
{
bbstored_pid = StartDaemon(bbstored_pid,
BBSTORED " " + bbstored_args + " testfiles/bbstored.conf",
"testfiles/bbstored.pid");
const std::string& daemon_args_final(daemon_args.size() ? daemon_args : bbstored_args);
bbstored_pid = StartDaemon(bbstored_pid, BBSTORED " " + daemon_args_final +
" testfiles/bbstored.conf", "testfiles/bbstored.pid", 22011);
return bbstored_pid != 0;
}

Expand All @@ -282,11 +282,11 @@ bool StopServer(bool wait_for_process)
return result;
}

bool StartClient(const std::string& bbackupd_conf_file)
bool StartClient(const std::string& bbackupd_conf_file, const std::string& daemon_args)
{
bbackupd_pid = StartDaemon(bbackupd_pid,
BBACKUPD " " + bbackupd_args + " " + bbackupd_conf_file,
"testfiles/bbackupd.pid");
const std::string& daemon_args_final(daemon_args.size() ? daemon_args : bbackupd_args);
bbackupd_pid = StartDaemon(bbackupd_pid, BBACKUPD " " + daemon_args_final + " -c " +
bbackupd_conf_file, "testfiles/bbackupd.pid", 0, "testfiles/bbackupd.sock");
return bbackupd_pid != 0;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/backupstore/StoreTestUtils.h
Expand Up @@ -66,13 +66,14 @@ bool run_housekeeping_and_check_account();
bool check_reference_counts();

//! Starts the bbstored test server running, which must not already be running.
bool StartServer();
bool StartServer(const std::string& daemon_args = "");

//! Stops the currently running bbstored test server.
bool StopServer(bool wait_for_process = false);

//! Starts the bbackupd client running, which must not already be running.
bool StartClient(const std::string& bbackupd_conf_file = "testfiles/bbackupd.conf");
bool StartClient(const std::string& bbackupd_conf_file = "testfiles/bbackupd.conf",
const std::string& daemon_args = "");

//! Stops the currently running bbackupd client.
bool StopClient(bool wait_for_process = false);
Expand Down
3 changes: 3 additions & 0 deletions lib/common/BoxException.h
Expand Up @@ -34,6 +34,9 @@ class BoxException : public std::exception
private:
};

#define EXCEPTION_IS_TYPE(exception_obj, type, subtype) \
(exception_obj.GetType() == type::ExceptionType && \
exception_obj.GetSubType() == type::subtype)

#endif // BOXEXCEPTION__H

127 changes: 0 additions & 127 deletions lib/common/Test.cpp
Expand Up @@ -382,133 +382,6 @@ int ReadPidFile(const char *pidFile)
return pid;
}

int LaunchServer(const std::string& rCommandLine, const char *pidFile)
{
BOX_INFO("Starting server: " << rCommandLine);

#ifdef WIN32

PROCESS_INFORMATION procInfo;

STARTUPINFO startInfo;
startInfo.cb = sizeof(startInfo);
startInfo.lpReserved = NULL;
startInfo.lpDesktop = NULL;
startInfo.lpTitle = NULL;
startInfo.dwFlags = 0;
startInfo.cbReserved2 = 0;
startInfo.lpReserved2 = NULL;

std::string cmd = ConvertPaths(rCommandLine);
CHAR* tempCmd = strdup(cmd.c_str());

DWORD result = CreateProcess
(
NULL, // lpApplicationName, naughty!
tempCmd, // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
false, // bInheritHandles
0, // dwCreationFlags
NULL, // lpEnvironment
NULL, // lpCurrentDirectory
&startInfo, // lpStartupInfo
&procInfo // lpProcessInformation
);

free(tempCmd);

TEST_THAT_OR(result != 0,
BOX_LOG_WIN_ERROR("Launch failed: " << rCommandLine);
return -1;
);

CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);

return WaitForServerStartup(pidFile, (int)procInfo.dwProcessId);

#else // !WIN32

TEST_THAT_OR(RunCommand(rCommandLine) == 0,
TEST_FAIL_WITH_MESSAGE("Failed to start server: " << rCommandLine);
return -1;
)

return WaitForServerStartup(pidFile, 0);

#endif // WIN32
}

int WaitForServerStartup(const char *pidFile, int pidIfKnown)
{
#ifdef WIN32
if (pidFile == NULL)
{
return pidIfKnown;
}
#else
// on other platforms there is no other way to get
// the PID, so a NULL pidFile doesn't make sense.
ASSERT(pidFile != NULL);
#endif

// time for it to start up
BOX_TRACE("Waiting for server to start");

for (int i = 0; i < 15; i++)
{
if (TestFileNotEmpty(pidFile))
{
break;
}

if (pidIfKnown && !ServerIsAlive(pidIfKnown))
{
break;
}

::sleep(1);
}

// on Win32 we can check whether the process is alive
// without even checking the PID file

if (pidIfKnown && !ServerIsAlive(pidIfKnown))
{
TEST_FAIL_WITH_MESSAGE("Server died!");
return -1;
}

if (!TestFileNotEmpty(pidFile))
{
TEST_FAIL_WITH_MESSAGE("Server didn't save PID file");
return -1;
}

BOX_TRACE("Server started");

// wait a second for the pid to be written to the file
::sleep(1);

// read pid file
int pid = ReadPidFile(pidFile);

// On Win32 we can check whether the PID in the pidFile matches
// the one returned by the system, which it always should.

if (pidIfKnown && pid != pidIfKnown)
{
BOX_ERROR("Server wrote wrong pid to file (" << pidFile <<
"): expected " << pidIfKnown << " but found " <<
pid);
TEST_FAIL_WITH_MESSAGE("Server wrote wrong pid to file");
return -1;
}

return pid;
}

void TestRemoteProcessMemLeaksFunc(const char *filename,
const char* file, int line)
{
Expand Down
3 changes: 1 addition & 2 deletions lib/common/Test.h
Expand Up @@ -219,15 +219,14 @@ int finish_test_suite();

bool TestFileExists(const char *Filename);
bool TestDirExists(const char *Filename);
bool TestFileNotEmpty(const char *Filename);

// -1 if doesn't exist
int TestGetFileSize(const std::string& Filename);
std::string ConvertPaths(const std::string& rOriginal);
int RunCommand(const std::string& rCommandLine);
bool ServerIsAlive(int pid);
int ReadPidFile(const char *pidFile);
int LaunchServer(const std::string& rCommandLine, const char *pidFile);
int WaitForServerStartup(const char *pidFile, int pidIfKnown);

#define TestRemoteProcessMemLeaks(filename) \
TestRemoteProcessMemLeaksFunc(filename, __FILE__, __LINE__)
Expand Down

0 comments on commit a9bc580

Please sign in to comment.