From 84146c4efff0d1e6a2349d2b3d7666de6ff5880f Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 28 Mar 2019 19:42:10 +0100
Subject: [PATCH] set seed in aql timeout test in integration

---
 alib2aux/src/debug/Random.cpp                 | 16 +++++++++++
 alib2aux/src/debug/Random.h                   | 28 +++++++++++++++++++
 alib2cli/src/command/SetCommand.h             |  3 ++
 .../test-src/testing/TimeoutAqlTest.cpp       | 17 ++++++++---
 .../test-src/tests/dummyTest.cpp              |  2 +-
 5 files changed, 61 insertions(+), 5 deletions(-)
 create mode 100644 alib2aux/src/debug/Random.cpp
 create mode 100644 alib2aux/src/debug/Random.h

diff --git a/alib2aux/src/debug/Random.cpp b/alib2aux/src/debug/Random.cpp
new file mode 100644
index 0000000000..cf0869ea21
--- /dev/null
+++ b/alib2aux/src/debug/Random.cpp
@@ -0,0 +1,16 @@
+/*
+ * Random.h
+ *
+ *  Created on: 18. 3. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#include "Random.h"
+#include <registration/AlgoRegistration.hpp>
+
+namespace debug {
+
+auto RandomInt = registration::AbstractRegister < Random < int >, int > ( Random < int >::random );
+auto RandomUnsigned = registration::AbstractRegister < Random < unsigned >, unsigned > ( Random < unsigned >::random );
+
+} /* namespace debug */
diff --git a/alib2aux/src/debug/Random.h b/alib2aux/src/debug/Random.h
new file mode 100644
index 0000000000..a2c28edea7
--- /dev/null
+++ b/alib2aux/src/debug/Random.h
@@ -0,0 +1,28 @@
+/*
+ * Random.h
+ *
+ *  Created on: 18. 3. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#ifndef RANDOM_H_
+#define RANDOM_H_
+
+#include <alib/random>
+
+namespace debug {
+
+template < class T >
+class Random {
+public:
+	static T random ( );
+};
+
+template < class T >
+T Random < T >::random ( ) {
+	return ext::random_devices::semirandom ( );
+}
+
+} /* namespace debug */
+
+#endif /* RANDOM_H_ */
diff --git a/alib2cli/src/command/SetCommand.h b/alib2cli/src/command/SetCommand.h
index 5627ad91f4..c6e21a99e0 100644
--- a/alib2cli/src/command/SetCommand.h
+++ b/alib2cli/src/command/SetCommand.h
@@ -4,6 +4,7 @@
 #include <command/Command.h>
 #include <environment/Environment.h>
 #include <global/GlobalData.h>
+#include <alib/random>
 
 namespace cli {
 
@@ -22,6 +23,8 @@ public:
 			common::GlobalData::measure = ext::from_string < bool > ( m_value );
 		} else if ( m_param == "optimizeXml" ) {
 			common::GlobalData::optimizeXml = ext::from_string < bool > ( m_value );
+		} else if ( m_param == "seed" ) {
+			ext::random_devices::semirandom.seed ( ext::from_string < unsigned > ( m_value ) );
 		} else {
 			common::Streams::out << "The set parameter " << m_param << " does not exist." << std::endl;
 		}
diff --git a/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp b/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp
index 726b8103e3..6d6ddaf4a5 100644
--- a/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp
+++ b/alib2integrationtest/test-src/testing/TimeoutAqlTest.cpp
@@ -10,6 +10,7 @@
 
 #include <global/GlobalData.h>
 #include <alib/exception>
+#include <alib/random>
 
 #define PIPE_RD 0
 #define PIPE_WR 1
@@ -40,7 +41,7 @@ void newSigChild ( int ) {
 		throw std::runtime_error ( "TimeoutAqlTest: write() failure (wakeup signalling)" );
 }
 
-int aqlTest ( int fd, const ext::vector < std::string > & queries ) {
+int aqlTest ( int fd, const ext::vector < std::string > & queries, unsigned seed ) {
 	try {
 		cli::Environment environment;
 
@@ -48,8 +49,11 @@ int aqlTest ( int fd, const ext::vector < std::string > & queries ) {
 		common::Streams::out = ss;
 		common::Streams::err = ss;
 
+		cli::Parser parser = cli::Parser ( cli::Lexer ( "set seed " + ext::to_string ( seed ) ) );
+		parser.parse ( ) -> run ( environment );
+
 		for ( const std::string & q : queries ) {
-			cli::Parser parser = cli::Parser ( cli::Lexer ( q ) );
+			parser = cli::Parser ( cli::Lexer ( q ) );
 			parser.parse ( ) -> run ( environment );
 
 			if ( write ( fd, ss.str ( ).c_str ( ), ss.str ( ).length ( ) ) != ( ssize_t ) ss.str ( ).length ( ) )
@@ -97,6 +101,8 @@ void _TimeoutAqlTest ( const std::chrono::microseconds & timeout, const ext::vec
 	if ( pipe ( g_Wakeup ) )
 		throw std::runtime_error ( "TimeoutAqlTest: Failed to initialize pipe (wakeup signalling)" );
 
+	unsigned seed = ext::random_devices::random ( );
+
 	/* do the forking */
 	pid_t x = fork ();
 	if ( x < 0 ) {
@@ -115,7 +121,7 @@ void _TimeoutAqlTest ( const std::chrono::microseconds & timeout, const ext::vec
 		close ( 1 );
 		close ( 2 );
 
-		exit ( aqlTest ( pipefd [ PIPE_WR ], queries ) );
+		exit ( aqlTest ( pipefd [ PIPE_WR ], queries, seed ) );
 	}
 
 	close ( pipefd [ PIPE_WR ] );
@@ -141,14 +147,17 @@ void _TimeoutAqlTest ( const std::chrono::microseconds & timeout, const ext::vec
 
 	if ( WIFEXITED ( status ) ) {
 		INFO ( "AqlTest failure. Trying to execute: " << queries );
+		INFO ( "Seed was: " << seed );
 		INFO ( "Child output was: >" << childOutput << "<" );
 		REQUIRE ( WEXITSTATUS ( status ) == 0 );
 	} else if ( WIFSIGNALED ( status ) ) {
+		INFO ( "AqlTest failure. Trying to execute: " << queries );
+		INFO ( "Seed was: " << seed );
+		INFO ( "Child output was: >" << childOutput << "<" );
 		if ( WTERMSIG ( status ) == SIGTERM || WTERMSIG ( status ) == SIGKILL ) { /* killed by timeout control */
 			WARN ( "Timeout (" << timeout.count ( ) << " us) reached in test (" << queries << ")" );
 		} else {
 			INFO ( "Child process signaled, signal " << WTERMSIG ( status ) << " (" << strsignal ( WTERMSIG ( status ) ) << ")" );
-			INFO ( "Child output was: >" << childOutput << "<" );
 			FAIL ( );
 		}
 	}
diff --git a/alib2integrationtest/test-src/tests/dummyTest.cpp b/alib2integrationtest/test-src/tests/dummyTest.cpp
index 9eef01b2bc..7ae412c131 100644
--- a/alib2integrationtest/test-src/tests/dummyTest.cpp
+++ b/alib2integrationtest/test-src/tests/dummyTest.cpp
@@ -15,7 +15,7 @@ TEST_CASE ( "Dummy Test", "[integration][dummy][!mayfail][!hide]" ) {
 
 TEST_CASE ( "Segfault Test", "[integration][dummy][!shouldfail][!hide]" ) {
 	ext::vector < std::string > qs = {
-		"execute \"generated some output\"",
+		"execute debug::Random @ int",
 		"execute debug::Segfault"
 	};
 
-- 
GitLab