aboutsummaryrefslogtreecommitdiff
path: root/android/guava-tests/test/com/google/common/util/concurrent/MonitorTestCase.java
blob: 6d620ffc24cd604d1c8f8e806d085f77c4ce2f30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (C) 2010 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.util.concurrent;

import com.google.common.testing.NullPointerTester;
import com.google.common.testing.TearDownStack;
import java.util.Random;
import junit.framework.TestCase;

/**
 * Tests for {@link Monitor}, either interruptible or uninterruptible.
 *
 * @author Justin T. Sampson
 */

public abstract class MonitorTestCase extends TestCase {

  public class TestGuard extends Monitor.Guard {
    private volatile boolean satisfied;

    public TestGuard(boolean satisfied) {
      super(MonitorTestCase.this.monitor);
      this.satisfied = satisfied;
    }

    @Override
    public boolean isSatisfied() {
      return this.satisfied;
    }

    public void setSatisfied(boolean satisfied) {
      this.satisfied = satisfied;
    }
  }

  private final boolean interruptible;
  private Monitor monitor;
  private final TearDownStack tearDownStack = new TearDownStack(true);
  private TestThread<Monitor> thread1;
  private TestThread<Monitor> thread2;

  protected MonitorTestCase(boolean interruptible) {
    this.interruptible = interruptible;
  }

  @Override
  protected final void setUp() throws Exception {
    boolean fair = new Random().nextBoolean();
    monitor = new Monitor(fair);
    tearDownStack.addTearDown(thread1 = new TestThread<>(monitor, "TestThread #1"));
    tearDownStack.addTearDown(thread2 = new TestThread<>(monitor, "TestThread #2"));
  }

  @Override
  protected final void tearDown() {
    tearDownStack.runTearDown();
  }

  private String enter() {
    return interruptible ? "enterInterruptibly" : "enter";
  }

  private String tryEnter() {
    return "tryEnter";
  }

  private String enterIf() {
    return interruptible ? "enterIfInterruptibly" : "enterIf";
  }

  private String tryEnterIf() {
    return "tryEnterIf";
  }

  private String enterWhen() {
    return interruptible ? "enterWhen" : "enterWhenUninterruptibly";
  }

  private String waitFor() {
    return interruptible ? "waitFor" : "waitForUninterruptibly";
  }

  private String leave() {
    return "leave";
  }

  public final void testMutualExclusion() throws Exception {
    thread1.callAndAssertReturns(enter());
    thread2.callAndAssertBlocks(enter());
    thread1.callAndAssertReturns(leave());
    thread2.assertPriorCallReturns(enter());
  }

  public final void testTryEnter() throws Exception {
    thread1.callAndAssertReturns(true, tryEnter());
    thread2.callAndAssertReturns(false, tryEnter());
    thread1.callAndAssertReturns(true, tryEnter());
    thread2.callAndAssertReturns(false, tryEnter());
    thread1.callAndAssertReturns(leave());
    thread2.callAndAssertReturns(false, tryEnter());
    thread1.callAndAssertReturns(leave());
    thread2.callAndAssertReturns(true, tryEnter());
  }

  public final void testSystemStateMethods() throws Exception {
    checkSystemStateMethods(0);
    thread1.callAndAssertReturns(enter());
    checkSystemStateMethods(1);
    thread1.callAndAssertReturns(enter());
    checkSystemStateMethods(2);
    thread1.callAndAssertReturns(leave());
    checkSystemStateMethods(1);
    thread1.callAndAssertReturns(leave());
    checkSystemStateMethods(0);
  }

  private void checkSystemStateMethods(int enterCount) throws Exception {
    thread1.callAndAssertReturns(enterCount != 0, "isOccupied");
    thread1.callAndAssertReturns(enterCount != 0, "isOccupiedByCurrentThread");
    thread1.callAndAssertReturns(enterCount, "getOccupiedDepth");

    thread2.callAndAssertReturns(enterCount != 0, "isOccupied");
    thread2.callAndAssertReturns(false, "isOccupiedByCurrentThread");
    thread2.callAndAssertReturns(0, "getOccupiedDepth");
  }

  public final void testEnterWhen_initiallyTrue() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread1.callAndAssertReturns(enterWhen(), guard);
  }

  public final void testEnterWhen_initiallyFalse() throws Exception {
    TestGuard guard = new TestGuard(false);
    thread1.callAndAssertWaits(enterWhen(), guard);
    monitor.enter();
    guard.setSatisfied(true);
    monitor.leave();
    thread1.assertPriorCallReturns(enterWhen());
  }

  public final void testEnterWhen_alreadyOccupied() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread2.callAndAssertReturns(enter());
    thread1.callAndAssertBlocks(enterWhen(), guard);
    thread2.callAndAssertReturns(leave());
    thread1.assertPriorCallReturns(enterWhen());
  }

  public final void testEnterIf_initiallyTrue() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread1.callAndAssertReturns(true, enterIf(), guard);
    thread2.callAndAssertBlocks(enter());
  }

  public final void testEnterIf_initiallyFalse() throws Exception {
    TestGuard guard = new TestGuard(false);
    thread1.callAndAssertReturns(false, enterIf(), guard);
    thread2.callAndAssertReturns(enter());
  }

  public final void testEnterIf_alreadyOccupied() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread2.callAndAssertReturns(enter());
    thread1.callAndAssertBlocks(enterIf(), guard);
    thread2.callAndAssertReturns(leave());
    thread1.assertPriorCallReturns(true, enterIf());
  }

  public final void testTryEnterIf_initiallyTrue() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread1.callAndAssertReturns(true, tryEnterIf(), guard);
    thread2.callAndAssertBlocks(enter());
  }

  public final void testTryEnterIf_initiallyFalse() throws Exception {
    TestGuard guard = new TestGuard(false);
    thread1.callAndAssertReturns(false, tryEnterIf(), guard);
    thread2.callAndAssertReturns(enter());
  }

  public final void testTryEnterIf_alreadyOccupied() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread2.callAndAssertReturns(enter());
    thread1.callAndAssertReturns(false, tryEnterIf(), guard);
  }

  public final void testWaitFor_initiallyTrue() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread1.callAndAssertReturns(enter());
    thread1.callAndAssertReturns(waitFor(), guard);
  }

  public final void testWaitFor_initiallyFalse() throws Exception {
    TestGuard guard = new TestGuard(false);
    thread1.callAndAssertReturns(enter());
    thread1.callAndAssertWaits(waitFor(), guard);
    monitor.enter();
    guard.setSatisfied(true);
    monitor.leave();
    thread1.assertPriorCallReturns(waitFor());
  }

  public final void testWaitFor_withoutEnter() throws Exception {
    TestGuard guard = new TestGuard(true);
    thread1.callAndAssertThrows(IllegalMonitorStateException.class, waitFor(), guard);
  }

  public void testNulls() {
    monitor.enter(); // Inhibit IllegalMonitorStateException
    new NullPointerTester()
        .setDefault(Monitor.Guard.class, new TestGuard(true))
        .testAllPublicInstanceMethods(monitor);
  }

  // TODO: Test enter(long, TimeUnit).
  // TODO: Test enterWhen(Guard, long, TimeUnit).
  // TODO: Test enterIf(Guard, long, TimeUnit).
  // TODO: Test waitFor(Guard, long, TimeUnit).
  // TODO: Test getQueueLength().
  // TODO: Test hasQueuedThreads().
  // TODO: Test getWaitQueueLength(Guard).
  // TODO: Test automatic signaling before leave, waitFor, and reentrant enterWhen.
  // TODO: Test blocking to re-enter monitor after being signaled.
  // TODO: Test interrupts with both interruptible and uninterruptible monitor.
  // TODO: Test multiple waiters: If guard is still satisfied, signal next waiter.
  // TODO: Test multiple waiters: If guard is no longer satisfied, do not signal next waiter.

}