1/*
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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.
8 *
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).
14 *
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.
18 *
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
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "runtime/os.hpp"
27#include "utilities/ostream.hpp"
28#include "utilities/spinYield.hpp"
29#include "unittest.hpp"
30
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.
34
35static void check_report(const SpinYield* spinner, const char* expected) {
36 char buffer[100];
37 stringStream s(buffer, sizeof(buffer));
38 spinner->report(&s);
39 ASSERT_STREQ(expected, buffer);
40}
41
42TEST(SpinYield, no_waiting) {
43 SpinYield spinner;
44 check_report(&spinner, "no waiting");
45}
46
47TEST(SpinYield, one_wait) {
48 SpinYield spinner(100);
49 spinner.wait();
50 check_report(&spinner, os::is_MP() ? "spins = 1" : "yields = 1");
51}
52
53TEST(SpinYield, ten_waits) {
54 SpinYield spinner(100, 100);
55 for (unsigned i = 0; i < 10; ++i) {
56 spinner.wait();
57 }
58 check_report(&spinner, os::is_MP() ? "spins = 10" : "yields = 10");
59}
60
61TEST(SpinYield, two_yields) {
62 SpinYield spinner(0, 10);
63 spinner.wait();
64 spinner.wait();
65 check_report(&spinner, "yields = 2");
66}
67
68TEST_VM(SpinYield, one_sleep) {
69 SpinYield spinner(0, 0);
70 spinner.wait();
71
72 char buffer[100];
73 stringStream s(buffer, sizeof(buffer));
74 spinner.report(&s);
75
76 const char* expected = "sleep = ";
77 ASSERT_TRUE(strncmp(expected, buffer, strlen(expected)) == 0);
78}
79
80TEST_VM(SpinYield, one_spin_one_sleep) {
81 SpinYield spinner(1, 0);
82 spinner.wait();
83 spinner.wait();
84
85 char buffer[100];
86 stringStream s(buffer, sizeof(buffer));
87 spinner.report(&s);
88
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);
93}
94