<commit_before>#include "TestSupport.h"
#include "Logging.h"
#include "LoggingAgent/LoggingServer.h"

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <oxt/thread.hpp>

using namespace Passenger;
using namespace std;
using namespace oxt;

namespace tut {
	struct LoggingTest {
		static const unsigned long long YESTERDAY = 1263299017000000ull;  // January 12, 2009, 13:23:37
		static const unsigned long long TODAY     = 1263385422000000ull;  // January 13, 2009, 13:23:42
		static const unsigned long long TOMORROW  = 1263471822000000ull;  // January 14, 2009, 13:23:42
		#define FOOBAR_MD5 "3858f62230ac3c915f300c664312c63f"
		
		ServerInstanceDirPtr serverInstanceDir;
		ServerInstanceDir::GenerationPtr generation;
		string socketFilename;
		string loggingDir;
		AccountsDatabasePtr accountsDatabase;
		MessageServerPtr server;
		shared_ptr<oxt::thread> serverThread;
		AnalyticsLoggerPtr logger;
		
		LoggingTest() {
			createServerInstanceDirAndGeneration(serverInstanceDir, generation);
			socketFilename = generation->getPath() + "/logging.socket";
			loggingDir = generation->getPath() + "/logs";
			accountsDatabase = ptr(new AccountsDatabase());
			accountsDatabase->add("test", "1234", false);
			
			server = ptr(new MessageServer(socketFilename, accountsDatabase));
			server->addHandler(ptr(new LoggingServer(loggingDir)));
			serverThread = ptr(new oxt::thread(
				boost::bind(&MessageServer::mainLoop, server.get())
			));
			
			logger = ptr(new AnalyticsLogger(socketFilename, "test", "1234"));
		}
		
		~LoggingTest() {
			serverThread->interrupt_and_join();
			SystemTime::releaseAll();
		}
	};
	
	DEFINE_TEST_GROUP(LoggingTest);
	
	TEST_METHOD(1) {
		// Test logging of new transaction.
		SystemTime::forceUsec(YESTERDAY);
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		log->message("hello");
		log->message("world");
		
		string data = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/13/log.txt");
		ensure(data.find("hello\n") != string::npos);
		ensure(data.find("world\n") != string::npos);
		
		ensure(!logger->isNull());
		ensure(!log->isNull());
	}
	
	TEST_METHOD(2) {
		// Test logging of existing transaction.
		SystemTime::forceUsec(YESTERDAY);
		
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		ensure_equals(log->getGroupName(), "foobar");
		log->message("message 1");
		
		AnalyticsLogPtr log2 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		log->message("message 2");
		
		string data = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/13/log.txt");
		ensure(data.find("message 1\n") != string::npos);
		ensure(data.find("message 2\n") != string::npos);
	}
	
	TEST_METHOD(3) {
		// Test logging with different points in time.
		SystemTime::forceUsec(YESTERDAY);
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		log->message("message 1");
		SystemTime::forceUsec(TODAY);
		log->message("message 2");
		
		SystemTime::forceUsec(TOMORROW);
		AnalyticsLogPtr log2 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		log2->message("message 3");
		
		AnalyticsLogPtr log3 = logger->newTransaction("foobar");
		log3->message("message 4");
		
		string yesterdayData = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/13/log.txt");
		string tomorrowData = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/14/13/log.txt");
		
		ensure("(1)", yesterdayData.find(toString(YESTERDAY) + " message 1\n") != string::npos);
		ensure("(2)", yesterdayData.find(toString(TODAY) + " message 2\n") != string::npos);
		ensure("(3)", yesterdayData.find(toString(TOMORROW) + " message 3\n") != string::npos);
		ensure("(4)", tomorrowData.find(toString(TOMORROW) + " message 4\n") != string::npos);
	}
	
	TEST_METHOD(4) {
		// newTransaction() and continueTransaction() write a ATTACH message
		// to the log file, while AnalyticsLogPtr writes an DETACH message upon
		// destruction.
		SystemTime::forceUsec(YESTERDAY);
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		SystemTime::forceUsec(TODAY);
		AnalyticsLogPtr log2 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		log2.reset();
		SystemTime::forceUsec(TOMORROW);
		log.reset();
		
		string data = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/13/log.txt");
		ensure("(1)", data.find(toString(YESTERDAY) + " ATTACH\n") != string::npos);
		ensure("(2)", data.find(toString(TODAY) + " ATTACH\n") != string::npos);
		ensure("(3)", data.find(toString(TODAY) + " DETACH\n") != string::npos);
		ensure("(4)", data.find(toString(TOMORROW) + " DETACH\n") != string::npos);
	}
	
	TEST_METHOD(5) {
		// newTransaction() generates a new ID, while continueTransaction()
		// reuses the ID.
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		AnalyticsLogPtr log2 = logger->newTransaction("foobar");
		AnalyticsLogPtr log3 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		AnalyticsLogPtr log4 = logger->continueTransaction(log2->getGroupName(), log2->getTxnId());
		
		ensure_equals(log->getTxnId(), log3->getTxnId());
		ensure_equals(log2->getTxnId(), log4->getTxnId());
		ensure(log->getTxnId() != log2->getTxnId());
	}
	
	TEST_METHOD(6) {
		// An empty AnalyticsLog doesn't do anything.
		AnalyticsLog log;
		ensure(log.isNull());
		log.message("hello world");
		ensure_equals(getFileType(loggingDir), FT_NONEXISTANT);
	}
	
	TEST_METHOD(7) {
		// An empty AnalyticsLogger doesn't do anything.
		AnalyticsLogger logger;
		ensure(logger.isNull());
		
		AnalyticsLogPtr log = logger.newTransaction("foo");
		ensure(log->isNull());
		log->message("hello world");
		ensure_equals(getFileType(loggingDir), FT_NONEXISTANT);
	}
}
<commit_msg>Fix LoggingTest.cpp, directory time names are in UTC not in local timezone.<commit_after>#include "TestSupport.h"
#include "Logging.h"
#include "LoggingAgent/LoggingServer.h"

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <oxt/thread.hpp>

using namespace Passenger;
using namespace std;
using namespace oxt;

namespace tut {
	struct LoggingTest {
		static const unsigned long long YESTERDAY = 1263299017000000ull;  // January 12, 2009, 12:23:37 UTC
		static const unsigned long long TODAY     = 1263385422000000ull;  // January 13, 2009, 12:23:42 UTC
		static const unsigned long long TOMORROW  = 1263471822000000ull;  // January 14, 2009, 12:23:42 UTC
		#define FOOBAR_MD5 "3858f62230ac3c915f300c664312c63f"
		
		ServerInstanceDirPtr serverInstanceDir;
		ServerInstanceDir::GenerationPtr generation;
		string socketFilename;
		string loggingDir;
		AccountsDatabasePtr accountsDatabase;
		MessageServerPtr server;
		shared_ptr<oxt::thread> serverThread;
		AnalyticsLoggerPtr logger;
		
		LoggingTest() {
			createServerInstanceDirAndGeneration(serverInstanceDir, generation);
			socketFilename = generation->getPath() + "/logging.socket";
			loggingDir = generation->getPath() + "/logs";
			accountsDatabase = ptr(new AccountsDatabase());
			accountsDatabase->add("test", "1234", false);
			
			server = ptr(new MessageServer(socketFilename, accountsDatabase));
			server->addHandler(ptr(new LoggingServer(loggingDir)));
			serverThread = ptr(new oxt::thread(
				boost::bind(&MessageServer::mainLoop, server.get())
			));
			
			logger = ptr(new AnalyticsLogger(socketFilename, "test", "1234"));
		}
		
		~LoggingTest() {
			serverThread->interrupt_and_join();
			SystemTime::releaseAll();
		}
	};
	
	DEFINE_TEST_GROUP(LoggingTest);
	
	TEST_METHOD(1) {
		// Test logging of new transaction.
		SystemTime::forceUsec(YESTERDAY);
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		log->message("hello");
		log->message("world");
		
		string data = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/12/log.txt");
		ensure(data.find("hello\n") != string::npos);
		ensure(data.find("world\n") != string::npos);
		
		ensure(!logger->isNull());
		ensure(!log->isNull());
	}
	
	TEST_METHOD(2) {
		// Test logging of existing transaction.
		SystemTime::forceUsec(YESTERDAY);
		
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		ensure_equals(log->getGroupName(), "foobar");
		log->message("message 1");
		
		AnalyticsLogPtr log2 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		log->message("message 2");
		
		string data = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/12/log.txt");
		ensure(data.find("message 1\n") != string::npos);
		ensure(data.find("message 2\n") != string::npos);
	}
	
	TEST_METHOD(3) {
		// Test logging with different points in time.
		SystemTime::forceUsec(YESTERDAY);
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		log->message("message 1");
		SystemTime::forceUsec(TODAY);
		log->message("message 2");
		
		SystemTime::forceUsec(TOMORROW);
		AnalyticsLogPtr log2 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		log2->message("message 3");
		
		AnalyticsLogPtr log3 = logger->newTransaction("foobar");
		log3->message("message 4");
		
		string yesterdayData = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/12/log.txt");
		string tomorrowData = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/14/12/log.txt");
		
		ensure("(1)", yesterdayData.find(toString(YESTERDAY) + " message 1\n") != string::npos);
		ensure("(2)", yesterdayData.find(toString(TODAY) + " message 2\n") != string::npos);
		ensure("(3)", yesterdayData.find(toString(TOMORROW) + " message 3\n") != string::npos);
		ensure("(4)", tomorrowData.find(toString(TOMORROW) + " message 4\n") != string::npos);
	}
	
	TEST_METHOD(4) {
		// newTransaction() and continueTransaction() write a ATTACH message
		// to the log file, while AnalyticsLogPtr writes an DETACH message upon
		// destruction.
		SystemTime::forceUsec(YESTERDAY);
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		SystemTime::forceUsec(TODAY);
		AnalyticsLogPtr log2 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		log2.reset();
		SystemTime::forceUsec(TOMORROW);
		log.reset();
		
		string data = readAll(loggingDir + "/1/" FOOBAR_MD5 "/web/2010/01/12/12/log.txt");
		ensure("(1)", data.find(toString(YESTERDAY) + " ATTACH\n") != string::npos);
		ensure("(2)", data.find(toString(TODAY) + " ATTACH\n") != string::npos);
		ensure("(3)", data.find(toString(TODAY) + " DETACH\n") != string::npos);
		ensure("(4)", data.find(toString(TOMORROW) + " DETACH\n") != string::npos);
	}
	
	TEST_METHOD(5) {
		// newTransaction() generates a new ID, while continueTransaction()
		// reuses the ID.
		AnalyticsLogPtr log = logger->newTransaction("foobar");
		AnalyticsLogPtr log2 = logger->newTransaction("foobar");
		AnalyticsLogPtr log3 = logger->continueTransaction(log->getGroupName(), log->getTxnId());
		AnalyticsLogPtr log4 = logger->continueTransaction(log2->getGroupName(), log2->getTxnId());
		
		ensure_equals(log->getTxnId(), log3->getTxnId());
		ensure_equals(log2->getTxnId(), log4->getTxnId());
		ensure(log->getTxnId() != log2->getTxnId());
	}
	
	TEST_METHOD(6) {
		// An empty AnalyticsLog doesn't do anything.
		AnalyticsLog log;
		ensure(log.isNull());
		log.message("hello world");
		ensure_equals(getFileType(loggingDir), FT_NONEXISTANT);
	}
	
	TEST_METHOD(7) {
		// An empty AnalyticsLogger doesn't do anything.
		AnalyticsLogger logger;
		ensure(logger.isNull());
		
		AnalyticsLogPtr log = logger.newTransaction("foo");
		ensure(log->isNull());
		log->message("hello world");
		ensure_equals(getFileType(loggingDir), FT_NONEXISTANT);
	}
}

{'fraction_non_alphanumeric': 0.1609547123623011, 'fraction_numerical': 0.03846826368246197, 'mean_word_length': 5.107314468766685, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '889615', 'n_tokens_mistral': 4399, 'n_tokens_neox': 3728, 'n_words': 706}