Skip to content

Commit 59115d8

Browse files
committed
Merge branch 'main' into PR #2537 to update
2 parents af48bb1 + 298c8db commit 59115d8

File tree

1 file changed

+31
-9
lines changed
  • google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1

1 file changed

+31
-9
lines changed

google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/WaiterTest.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,61 @@ public void run() {
5555
}
5656

5757
@Test
58-
public void testTryWait_Completes() {
58+
public void testTryWait_Completes() throws Exception {
5959
final Waiter waiter = new Waiter();
6060
waiter.incrementPendingCount(1);
6161
final FakeClock clock = new FakeClock();
6262

63+
final Thread mainThread = Thread.currentThread();
6364
Thread t =
6465
new Thread(
65-
() -> {
66-
try {
67-
Thread.sleep(100);
68-
} catch (InterruptedException e) {
66+
new Runnable() {
67+
@Override
68+
public void run() {
69+
while (mainThread.getState() == Thread.State.NEW) {
70+
Thread.yield();
71+
}
72+
waiter.incrementPendingCount(-1);
6973
}
70-
waiter.incrementPendingCount(-1);
7174
});
7275
t.start();
7376

7477
assertTrue(waiter.tryWait(500, clock));
78+
t.join();
79+
80+
assertEquals(0, waiter.pendingCount());
7581
}
7682

7783
@Test
78-
public void testTryWait_TimesOut() {
84+
public void testTryWait_TimesOut() throws Exception {
7985
final Waiter waiter = new Waiter();
8086
waiter.incrementPendingCount(1);
8187
final FakeClock clock = new FakeClock();
8288

89+
final Thread mainThread = Thread.currentThread();
8390
Thread t =
8491
new Thread(
85-
() -> {
86-
clock.advance(100, TimeUnit.MILLISECONDS);
92+
new Runnable() {
93+
@Override
94+
public void run() {
95+
while (mainThread.getState() == Thread.State.NEW) {
96+
Thread.yield();
97+
}
98+
try {
99+
// Waits some additional time to ensure that the waiter is actually waiting.
100+
Thread.sleep(100);
101+
clock.advance(200, TimeUnit.MILLISECONDS);
102+
} catch (InterruptedException e) {
103+
// Ignored.
104+
}
105+
}
87106
});
88107
t.start();
89108

90109
assertFalse(waiter.tryWait(100, clock));
110+
t.join();
111+
112+
assertEquals(1, waiter.pendingCount());
91113
}
92114

93115
@Test

0 commit comments

Comments
 (0)