2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
#include "precompiled.hpp"
26
#include "runtime/os.hpp"
27
#include "utilities/ostream.hpp"
28
#include "utilities/spinYield.hpp"
29
#include "unittest.hpp"
31
// Some basic tests of SpinYield, using comparison of report output with
32
// expected results to verify state. This is all very hard-wired to the
33
// current implementation of SpinYield, esp. the report function.
35
static void check_report(const SpinYield* spinner, const char* expected) {
37
stringStream s(buffer, sizeof(buffer));
39
ASSERT_STREQ(expected, buffer);
42
TEST(SpinYield, no_waiting) {
44
check_report(&spinner, "no waiting");
47
TEST(SpinYield, one_wait) {
48
SpinYield spinner(100);
50
check_report(&spinner, os::is_MP() ? "spins = 1" : "yields = 1");
53
TEST(SpinYield, ten_waits) {
54
SpinYield spinner(100, 100);
55
for (unsigned i = 0; i < 10; ++i) {
58
check_report(&spinner, os::is_MP() ? "spins = 10" : "yields = 10");
61
TEST(SpinYield, two_yields) {
62
SpinYield spinner(0, 10);
65
check_report(&spinner, "yields = 2");
68
TEST_VM(SpinYield, one_sleep) {
69
SpinYield spinner(0, 0);
73
stringStream s(buffer, sizeof(buffer));
76
const char* expected = "sleep = ";
77
ASSERT_TRUE(strncmp(expected, buffer, strlen(expected)) == 0);
80
TEST_VM(SpinYield, one_spin_one_sleep) {
81
SpinYield spinner(1, 0);
86
stringStream s(buffer, sizeof(buffer));
89
const char* expected_MP = "spins = 1, sleep = ";
90
const char* expected_UP = "sleep = ";
91
const char* expected = os::is_MP() ? expected_MP : expected_UP;
92
ASSERT_TRUE(strncmp(expected, buffer, strlen(expected)) == 0);