forked from grpc/grpc-java
/
SharedResourceHolderTest.java
284 lines (242 loc) · 9.08 KB
/
SharedResourceHolderTest.java
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright 2014 The gRPC 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 io.grpc.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import io.grpc.internal.SharedResourceHolder.Resource;
import java.util.LinkedList;
import java.util.concurrent.Delayed;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/** Unit tests for {@link SharedResourceHolder}. */
@RunWith(JUnit4.class)
public class SharedResourceHolderTest {
private final LinkedList<MockScheduledFuture<?>> scheduledDestroyTasks =
new LinkedList<>();
private SharedResourceHolder holder;
private static class ResourceInstance {
volatile boolean closed;
}
private static class ResourceFactory implements Resource<ResourceInstance> {
@Override
public ResourceInstance create() {
return new ResourceInstance();
}
@Override
public void close(ResourceInstance instance) {
instance.closed = true;
}
}
// Defines two kinds of resources
private static final Resource<ResourceInstance> SHARED_FOO = new ResourceFactory();
private static final Resource<ResourceInstance> SHARED_BAR = new ResourceFactory();
@Before public void setUp() {
holder = new SharedResourceHolder(new MockExecutorFactory());
}
@Test public void destroyResourceWhenRefCountReachesZero() {
ResourceInstance foo1 = holder.getInternal(SHARED_FOO);
ResourceInstance sharedFoo = foo1;
ResourceInstance foo2 = holder.getInternal(SHARED_FOO);
assertSame(sharedFoo, foo2);
ResourceInstance bar1 = holder.getInternal(SHARED_BAR);
ResourceInstance sharedBar = bar1;
foo2 = holder.releaseInternal(SHARED_FOO, foo2);
// foo refcount not reached 0, thus shared foo is not closed
assertTrue(scheduledDestroyTasks.isEmpty());
assertFalse(sharedFoo.closed);
assertNull(foo2);
foo1 = holder.releaseInternal(SHARED_FOO, foo1);
assertNull(foo1);
// foo refcount has reached 0, a destroying task is scheduled
assertEquals(1, scheduledDestroyTasks.size());
MockScheduledFuture<?> scheduledDestroyTask = scheduledDestroyTasks.poll();
assertEquals(SharedResourceHolder.DESTROY_DELAY_SECONDS,
scheduledDestroyTask.getDelay(TimeUnit.SECONDS));
// Simluate that the destroyer executes the foo destroying task
scheduledDestroyTask.runTask();
assertTrue(sharedFoo.closed);
// After the destroying, obtaining a foo will get a different instance
ResourceInstance foo3 = holder.getInternal(SHARED_FOO);
assertNotSame(sharedFoo, foo3);
holder.releaseInternal(SHARED_BAR, bar1);
// bar refcount has reached 0, a destroying task is scheduled
assertEquals(1, scheduledDestroyTasks.size());
scheduledDestroyTask = scheduledDestroyTasks.poll();
assertEquals(SharedResourceHolder.DESTROY_DELAY_SECONDS,
scheduledDestroyTask.getDelay(TimeUnit.SECONDS));
// Simulate that the destroyer executes the bar destroying task
scheduledDestroyTask.runTask();
assertTrue(sharedBar.closed);
}
@Test public void cancelDestroyTask() {
ResourceInstance foo1 = holder.getInternal(SHARED_FOO);
ResourceInstance sharedFoo = foo1;
holder.releaseInternal(SHARED_FOO, foo1);
// A destroying task for foo is scheduled
MockScheduledFuture<?> scheduledDestroyTask = scheduledDestroyTasks.poll();
assertFalse(scheduledDestroyTask.cancelled);
// obtaining a foo before the destroying task is executed will cancel the destroy
ResourceInstance foo2 = holder.getInternal(SHARED_FOO);
assertTrue(scheduledDestroyTask.cancelled);
assertTrue(scheduledDestroyTasks.isEmpty());
assertFalse(sharedFoo.closed);
// And it will be the same foo instance
assertSame(sharedFoo, foo2);
// Release it and the destroying task is scheduled again
holder.releaseInternal(SHARED_FOO, foo2);
scheduledDestroyTask = scheduledDestroyTasks.poll();
assertFalse(scheduledDestroyTask.cancelled);
scheduledDestroyTask.runTask();
assertTrue(sharedFoo.closed);
}
@Test public void releaseWrongInstance() {
ResourceInstance uncached = new ResourceInstance();
try {
holder.releaseInternal(SHARED_FOO, uncached);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
ResourceInstance cached = holder.getInternal(SHARED_FOO);
try {
holder.releaseInternal(SHARED_FOO, uncached);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
holder.releaseInternal(SHARED_FOO, cached);
}
@Test public void overreleaseInstance() {
ResourceInstance foo1 = holder.getInternal(SHARED_FOO);
holder.releaseInternal(SHARED_FOO, foo1);
try {
holder.releaseInternal(SHARED_FOO, foo1);
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// expected
}
}
@Test public void handleInstanceCloseError() {
class ExceptionOnCloseResource implements Resource<ResourceInstance> {
@Override
public ResourceInstance create() {
return new ResourceInstance();
}
@Override
public void close(ResourceInstance instance) {
throw new RuntimeException();
}
}
Resource<ResourceInstance> resource = new ExceptionOnCloseResource();
ResourceInstance instance = holder.getInternal(resource);
holder.releaseInternal(resource, instance);
MockScheduledFuture<?> scheduledDestroyTask = scheduledDestroyTasks.poll();
try {
scheduledDestroyTask.runTask();
fail("Should throw RuntimeException");
} catch (RuntimeException e) {
// expected
}
// Future resource fetches should not get the partially-closed one.
assertNotSame(instance, holder.getInternal(resource));
}
private class MockExecutorFactory implements
SharedResourceHolder.ScheduledExecutorFactory {
@Override
public ScheduledExecutorService createScheduledExecutor() {
ScheduledExecutorService mockExecutor = mock(ScheduledExecutorService.class);
when(mockExecutor.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class))).thenAnswer(
new Answer<MockScheduledFuture<Void>>() {
@Override
public MockScheduledFuture<Void> answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Runnable command = (Runnable) args[0];
long delay = (Long) args[1];
TimeUnit unit = (TimeUnit) args[2];
MockScheduledFuture<Void> future = new MockScheduledFuture<>(
command, delay, unit);
scheduledDestroyTasks.add(future);
return future;
}
});
return mockExecutor;
}
}
private static class MockScheduledFuture<V> implements ScheduledFuture<V> {
private boolean cancelled;
private boolean finished;
final Runnable command;
final long delay;
final TimeUnit unit;
MockScheduledFuture(Runnable command, long delay, TimeUnit unit) {
this.command = command;
this.delay = delay;
this.unit = unit;
}
void runTask() {
command.run();
finished = true;
}
@Override
public boolean cancel(boolean interrupt) {
if (cancelled || finished) {
return false;
}
cancelled = true;
return true;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public long getDelay(TimeUnit targetUnit) {
return targetUnit.convert(this.delay, this.unit);
}
@Override
public int compareTo(Delayed o) {
throw new UnsupportedOperationException();
}
@Override
public boolean isDone() {
return cancelled || finished;
}
@Override
public V get() {
throw new UnsupportedOperationException();
}
@Override
public V get(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
}
}