/* * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
// Default value for -new-thread option: true on AIX because we run into // problems when attempting to initialize the JVM on the primordial thread. #ifdef _AIX conststaticbool DEFAULT_SPAWN_IN_NEW_THREAD = true; #else conststaticbool DEFAULT_SPAWN_IN_NEW_THREAD = false; #endif
int ret = JNI_CreateJavaVM(jvm_ptr, (void**)&env, &args); if (ret == JNI_OK) { // CreateJavaVM leaves WXExec context, while gtests // calls internal functions assuming running in WXWwrite. // Switch to WXWrite once for all test cases.
MACOS_AARCH64_ONLY(Thread::current()->enable_wx(WXWrite));
} return ret;
}
virtualvoid OnTestStart(const ::testing::TestInfo& test_info) { constchar* name = test_info.name(); if (_jvm == nullptr && is_same_vm_test(name)) { // we want to have hs_err and core files when we execute regular tests int ret_val = init_jvm(_argc, _argv, false, &_jvm); if (ret_val != 0) {
ADD_FAILURE() << "Could not initialize the JVM: " << ret_val;
os::exit(1);
}
}
}
void destroy_jvm() { if (_jvm != NULL) { int ret = _jvm->DestroyJavaVM(); if (ret != 0) {
fprintf(stderr, "Warning: DestroyJavaVM error %d\n", ret);
}
}
}
};
staticchar* get_java_home_arg(int argc, char** argv) { for (int i = 0; i < argc; i++) { if (strncmp(argv[i], "-jdk", strlen(argv[i])) == 0) { return argv[i+1];
} if (is_prefix("--jdk=", argv[i])) { return argv[i] + strlen("--jdk=");
} if (is_prefix("-jdk:", argv[i])) { return argv[i] + strlen("-jdk:");
}
} return NULL;
}
staticbool get_spawn_new_main_thread_arg(int argc, char** argv) { // -new-thread[=(true|false)] for (int i = 0; i < argc; i++) { if (is_prefix("-new-thread", argv[i])) { constchar* v = argv[i] + strlen("-new-thread"); if (strlen(v) == 0) { returntrue;
} else { if (strcmp(v, "=true") == 0) { returntrue;
} elseif (strcmp(v, "=false") == 0) { returnfalse;
} else {
fprintf(stderr, "Invalid value for -new-thread (%s)", v);
}
}
}
} return DEFAULT_SPAWN_IN_NEW_THREAD;
}
staticint num_args_to_skip(char* arg) { if (strcmp(arg, "-jdk") == 0) { return 2; // skip the argument after -jdk as well
} if (is_prefix("--jdk=", arg)) { return 1;
} if (is_prefix("-jdk:", arg)) { return 1;
} if (is_prefix("-new-thread", arg)) { return 1;
} return 0;
}
int i = 0; while (i < argc) { int args_to_skip = num_args_to_skip(argv[i]); if (args_to_skip == 0) {
new_argv[new_argc] = argv[i];
i++;
new_argc++;
} else {
i += num_args_to_skip(argv[i]);
}
}
*argcp = new_argc; return new_argv;
}
// This is generally run once for a set of tests. But if that set includes a vm_assert or // other_vm test, then a new process is forked, and runUnitTestsInner is called, passing // just that test as the one to be executed. // // When we execute a vm_assert or other_vm test we create and initialize the JVM below. // // A vm_assert test crashes the VM so no cleanup is needed, but for other_vm we call // DestroyJavaVM via the TEST_OTHER_VM macro prior to the call to exit(). // // For same_vm tests we use an event listener to create the JVM when the first same_vm // test is executed. Once all tests are completed we can then call DestroyJavaVM on that // JVM directly. staticvoid runUnitTestsInner(int argc, char** argv) {
::testing::InitGoogleMock(&argc, argv);
::testing::GTEST_FLAG(death_test_style) = "threadsafe";
bool is_vmassert_test = false; bool is_othervm_test = false; // death tests facility is used for both regular death tests, other vm and vmassert tests if (::testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) { // when we execute death test, filter value equals to test name constchar* test_name = ::testing::GTEST_FLAG(filter).c_str(); constchar* const othervm_suffix = "_other_vm"; // TEST_OTHER_VM constchar* const vmassert_suffix = "_vm_assert"; // TEST_VM_ASSERT(_MSG) if (is_suffix(othervm_suffix, test_name)) {
is_othervm_test = true;
} elseif (is_suffix(vmassert_suffix, test_name)) {
is_vmassert_test = true;
}
}
char* java_home = get_java_home_arg(argc, argv); if (java_home == NULL) {
fprintf(stderr, "ERROR: You must specify a JDK to use for running the unit tests.\n");
os::exit(1);
} #ifndef _WIN32 int overwrite = 1; // overwrite an eventual existing value for JAVA_HOME
setenv("JAVA_HOME", java_home, overwrite);
if (is_vmassert_test || is_othervm_test) {
JavaVM* jvm = NULL; // both vmassert and other vm tests require inited jvm // but only vmassert tests disable hs_err and core file generation int ret; if ((ret = init_jvm(argc, argv, is_vmassert_test, &jvm)) != 0) {
fprintf(stderr, "ERROR: JNI_CreateJavaVM failed: %d\n", ret);
abort();
}
} else {
::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
jvm_listener = new JVMInitializerListener(argc, argv);
listeners.Append(jvm_listener);
}
int result = RUN_ALL_TESTS();
ALLOW_C_FUNCTION(::free, ::free(argv);)
// vm_assert and other_vm tests never reach this point as they either abort, or call // exit() - see TEST_OTHER_VM macro. We will reach here when all same_vm tests have // completed for this run, so we can terminate the VM used for that case.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.