Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 1520e89

Browse files
committed
Move non-shared tests out of pure java
Change-Id: I34830f0fb54321f8fa72a752325edbbbcb607eb2
1 parent 24fe440 commit 1520e89

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// Copyright (c) 2019 Couchbase, Inc All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package com.couchbase.lite;
17+
18+
import java.util.concurrent.CountDownLatch;
19+
import java.util.concurrent.Executor;
20+
import java.util.concurrent.RejectedExecutionException;
21+
import java.util.concurrent.ThreadPoolExecutor;
22+
import java.util.concurrent.TimeUnit;
23+
24+
import org.junit.Ignore;
25+
import org.junit.Test;
26+
27+
import com.couchbase.lite.internal.AbstractExecutionService;
28+
import com.couchbase.lite.internal.AndroidExecutionService;
29+
30+
31+
public class AndroidConcurrencyTest {
32+
33+
@Ignore("This is not actually a test. Use it to verify logcat output")
34+
@Test(expected = RejectedExecutionException.class)
35+
public void testSerialExecutorFailure() {
36+
AndroidExecutionService execSvc = new AndroidExecutionService();
37+
ThreadPoolExecutor exec = execSvc.getBaseExecutor();
38+
Executor serialExcecutor = execSvc.getSerialExecutor();
39+
40+
// hang the queue
41+
final CountDownLatch latch = new CountDownLatch(1);
42+
serialExcecutor.execute(() -> {
43+
try { latch.await(2, TimeUnit.SECONDS); }
44+
catch (InterruptedException ignore) { }
45+
});
46+
47+
// put some stuff in the serial executor queue
48+
for (int i = 1; i < 10; i++) { serialExcecutor.execute(() -> {}); }
49+
50+
// fill the base executor.
51+
try {
52+
while (true) {
53+
exec.execute(
54+
new AbstractExecutionService.InstrumentedTask(
55+
() -> {
56+
try { Thread.sleep(10 * 1000); }
57+
catch (InterruptedException ignore) {}
58+
},
59+
() -> {}));
60+
}
61+
}
62+
catch (RejectedExecutionException ignore) { }
63+
64+
// this should free the running serial job,
65+
// which should fail trying to start the next job
66+
latch.countDown();
67+
}
68+
69+
@Ignore("This is not actually a test. Use it to verify logcat output")
70+
@Test(expected = RejectedExecutionException.class)
71+
public void testConcurrentExecutorFailure() {
72+
AndroidExecutionService execSvc = new AndroidExecutionService();
73+
ThreadPoolExecutor exec = execSvc.getBaseExecutor();
74+
75+
// fill the executor
76+
try {
77+
while (true) {
78+
exec.execute(
79+
new AndroidExecutionService.InstrumentedTask(
80+
() -> {
81+
try { Thread.sleep(10 * 1000); }
82+
catch (InterruptedException ignore) {}
83+
},
84+
() -> {}));
85+
}
86+
}
87+
catch (RejectedExecutionException ignore) { }
88+
89+
// this should fail because the executor is full
90+
execSvc.getConcurrentExecutor().execute(() -> {});
91+
}
92+
}
93+

0 commit comments

Comments
 (0)