34 #include "../SOURCE_TOOLS/arb_main.h"
36 #define env_assert(cond) arb_assert(cond)
51 char *upstr = strdup(str);
56 #define WARN_NOT_IMPLEMENTED(fun,mode) TEST_WARNING2("%s(%s) does nothing", #fun, upcase(mode_command[mode]));
67 static const char *
mutexDir(
const char *name) {
80 bool mutexdir_exists()
const {
return GB_is_directory(mutexdir.c_str()); }
82 static const char *now() {
85 gettimeofday(&t,
NULp);
86 sprintf(buf,
"%li.%li", t.tv_sec, t.tv_usec);
90 static set<string> known_mutexes;
98 bool gotMutex =
false;
104 test_data().entered_mutex_loop =
true;
106 if (mutexdir_exists()) {
107 if (wait>maxwait && mutexdir_exists()) {
108 GBK_terminatef(
"[%s] Failed to get mutex for more than %i seconds", now(), maxwait);
110 printf(
"[%s] mutex '%s' exists.. sleeping\n", now(), name.c_str());
115 int res = mkdir(mutexdir.c_str(), S_IRWXU);
117 #if defined(DUMP_MUTEX_ACCESS)
118 printf(
"[%s] allocated mutex '%s'\n", now(), name.c_str());
124 printf(
"[%s] lost race for mutex '%s'\n", now(), name.c_str());
128 known_mutexes.insert(name);
131 if (!mutexdir_exists()) {
132 printf(
"[%s] Strange - mutex '%s' has vanished\n", now(), name.c_str());
135 if (rmdir(mutexdir.c_str()) != 0) {
137 GBK_terminatef(
"[%s] Failed to release mutex dir (%s)", now(), error);
140 #if defined(DUMP_MUTEX_ACCESS)
141 printf(
"[%s] released mutex '%s'\n", now(), mutexdir.c_str());
145 known_mutexes.erase(name);
148 static bool owned(
const char *mutex_name) {
149 return known_mutexes.find(mutex_name) != known_mutexes.end();
159 set<string> Mutex::known_mutexes;
173 const char *flagFileName()
const {
177 bool flagFileExists()
const {
182 void createFlagFile()
const {
184 const char *flagfile = flagFileName();
185 FILE *fp = fopen(flagfile,
"w");
188 HERE.errorf(
true,
"%s\n", error);
195 void removeFlagFile()
const {
196 const char *flagfile = flagFileName();
198 if (!flagFileExists()) {
201 int res = unlink(flagfile);
204 HERE.errorf(
true,
"%s\n", error);
208 void updateFlagFile()
const {
209 if (value) createFlagFile();
210 else removeFlagFile();
216 value(flagFileExists()),
221 value(flagFileExists()),
222 is_volatile(is_volatile_)
225 if (flagFileExists() !=
bool(*
this)) {
226 StaticCode::printf(
"Mismatch between internal value(=%i) and flagfile='%s'\n",
int(value), name.c_str());
230 operator bool()
const {
231 if (is_volatile) value = flagFileExists();
236 if (b !=
bool(*
this)) {
251 bool will_be_volatile;
256 void instanciate_on_demand()
const {
264 LazyPersistantFlag(
const char *name_,
bool is_volatile_) : name(name_), will_be_volatile(is_volatile_) {}
266 operator bool()
const {
267 instanciate_on_demand();
272 instanciate_on_demand();
329 #if defined(INDEX_HEXDUMP)
330 TEST_DUMP_FILE(
"TEST_pt.arb.pt",
"TEST_pt.arb.pt.dump");
333 #if defined(TEST_AUTO_UPDATE)
334 TEST_COPY_FILE(
"TEST_pt.arb.pt",
"TEST_pt.arb.pt.expected");
335 # if defined(INDEX_HEXDUMP)
336 TEST_COPY_FILE(
"TEST_pt.arb.pt.dump",
"TEST_pt.arb.pt.dump.expected");
338 #else // !defined(TEST_AUTO_UPDATE)
339 # if defined(INDEX_HEXDUMP)
350 #if defined(INDEX_HEXDUMP)
378 #if defined(TEST_AUTO_UPDATE)
379 TEST_COPY_FILE(
"TEST_gpt.arb",
"TEST_gpt.arb.expected");
380 #else // !defined(TEST_AUTO_UPDATE)
385 #if defined(INDEX_HEXDUMP)
386 TEST_DUMP_FILE(
"TEST_gpt.arb.pt",
"TEST_gpt.arb.pt.dump");
389 #if defined(TEST_AUTO_UPDATE)
390 TEST_COPY_FILE(
"TEST_gpt.arb.pt",
"TEST_gpt.arb.pt.expected");
391 # if defined(INDEX_HEXDUMP)
392 TEST_COPY_FILE(
"TEST_gpt.arb.pt.dump",
"TEST_gpt.arb.pt.dump.expected");
394 #else // !defined(TEST_AUTO_UPDATE)
395 # if defined(INDEX_HEXDUMP)
407 #if defined(INDEX_HEXDUMP)
420 #undef TEST_AUTO_UPDATE
439 void all_get_lazy_again() {
453 if (chdir(
runDir()) != 0) {
460 switch (guard_says) {
471 :
"has been interrupted (might be a deadlock)";
479 error = strdup(
"has thrown an exception");
483 error = strdup(
"is invalid");
498 is_setup = (mode ==
SETUP);
499 #if defined(SIMULATE_ENVSETUP_TIMEOUT)
505 #if defined(SIMULATE_ENVSETUP_SLOW_OVERTIME)
512 all_get_lazy_again();
532 bool want_setup = (mode ==
SETUP);
534 bool perform_change =
false;
535 bool wait_for_change =
false;
540 if (is_setup == want_setup) {
542 get_name(), mode_command[mode], mode_command[
other_mode[mode]]);
545 wait_for_change =
true;
549 if (is_setup == want_setup) {
550 StaticCode::printf(
"[environment '%s' already was %s]\n", get_name(), mode_command[mode]);
553 changing = perform_change =
true;
557 all_get_lazy_again();
560 env_assert(!(perform_change && wait_for_change));
562 if (perform_change) {
563 error = set_to(mode);
568 all_get_lazy_again();
571 if (clean_error.isSet()) {
572 StaticCode::printf(
"[environment '%s' failed to reach '%s' after failure (Reason: %s)]\n",
573 get_name(), mode_command[
CLEAN], &*clean_error);
578 if (wait_for_change) {
579 bool reached =
false;
580 while (!reached && !error.isSet()) {
581 StaticCode::printf(
"[waiting until environment '%s' reaches '%s']\n", get_name(), mode_command[mode]);
585 if (!changing && is_setup == want_setup) reached =
true;
587 error =
GBS_global_string_copy(
"[environment '%s' failed to reach '%s' (in another process)]", get_name(), mode_command[mode]);
589 all_get_lazy_again();
592 if (reached)
StaticCode::printf(
"[environment '%s' has reached '%s']\n", get_name(), mode_command[mode]);
598 const char *
get_name()
const {
return name.c_str(); }
602 #define FUNINFO(fun) FunInfo(fun,#fun)
612 for (
size_t e = 0; e<
MODULES; ++e) {
613 if (modules[e].has_name(moduleName)) {
622 for (
size_t e = 0; error.isNull() && e<
MODULES; ++e) {
629 const char *mods =
NULp;
630 for (
size_t i = 0; i<
MODULES; ++i) {
638 const char *modes =
NULp;
639 for (
size_t i = 0; i<
ARRAY_ELEMS(mode_command); ++i) {
640 if (mode_command[i]) {
650 int main(
int argc,
char* argv[]) {
654 bool showUsage =
true;
656 if (argc<2 || argc>3) error = strdup(
"Wrong number of arguments");
658 const char *modearg = argv[1];
661 for (
size_t i = 0; i<
ARRAY_ELEMS(mode_command); ++i) {
662 if (mode_command[i] && strcmp(modearg, mode_command[i]) == 0) mode = (
Mode)i;
674 const char *modulearg = argv[2];
689 const char *exename = argv[0];
GB_ERROR GBK_system(const char *system_command)
static Environment_cb wrapped_cb
static void own_or_terminate(const char *mutex_name)
int main(int argc, char *argv[])
static const char * FLAG_MUTEX
static const char * known_modes(char separator)
static Error ptserver_gene(Mode mode)
GB_ERROR GB_IO_error(const char *action, const char *filename)
GB_ERROR arb_look_and_start_server(long magic_number, const char *arb_tcp_env)
GB_ERROR arb_look_and_kill_server(int magic_number, const char *arb_tcp_env)
const char * GBS_global_string(const char *templat,...)
void GBK_terminatef(const char *templat,...)
static const char * known_modules(char separator)
#define TEST_EXPECT_LESS_EQUAL(val, ref)
FunInfo(Environment_cb cb_, const char *name_)
bool isNull() const
test if SmartPtr is NULp
static const char * mode_command[]
#define ARRAY_ELEMS(array)
void setNull()
set SmartPtr to NULp
GB_CSTR GB_getenvARBHOME(void)
static const char * runDir()
static HelixNrInfo * start
static const char * mutexDir(const char *name)
Error(* Environment_cb)(Mode mode)
static Error set_all_modules_to(Mode mode)
static const char * flagDir()
unsigned long GB_time_of_file(const char *path)
UnitTestResult execute_guarded(UnitTest_function fun, long *duration_usec, long max_allowed_duration_ms, bool detect_environment_calls)
static SmartCharPtr wrapped_error
static void error(const char *msg)
static void test_ptserver_activate(bool start, int serverid)
LazyPersistantFlag(const char *name_)
Error switch_to(Mode mode)
#define RETURN_ONETIME_ALLOC(allocated)
#define RETURN_LOCAL_ALLOC(mallocation)
#define TEST_EXPECT_ZERO_OR_SHOW_ERRNO(iocond)
char * ARB_strupper(char *s)
const long MAX_EXEC_MS_SLOW
PersistantFlag(const string &name_, bool is_volatile_)
static const char * unitTesterDir()
LazyPersistantFlag(const char *name_, bool is_volatile_)
GB_CSTR GB_concat_full_path(const char *anypath_left, const char *anypath_right)
#define AISC_MAGIC_NUMBER
static const char * upcase(const char *str)
const long MAX_EXEC_MS_ENV
static Error ptserver(Mode mode)
PersistantFlag(const string &name_)
bool GB_is_directory(const char *path)
#define TEST_EXPECT_FILES_EQUAL(f1, f2)
static void printf(const char *format,...) __attribute__((format(printf
void GBK_dump_backtrace(FILE *out, const char *message)
#define TEST_EXPECT_NO_ERROR(call)
static void start_of_main()
bool GB_is_regularfile(const char *path)
bool has_name(const char *oname) const
Mutex(const char *mutex_name)
static FunInfo * find_module(const char *moduleName)
const char * GBS_ptserver_tag(int id)
#define TEST_EXPECT_TEXTFILES_EQUAL(fgot, fwant)
#define TEST_EXPECT_EQUAL(expr, want)
static bool owned(const char *mutex_name)
char * GBS_global_string_copy(const char *templat,...)
const char * get_name() const