Skip to content

Commit adc360c

Browse files
committed
fixed tests
1 parent b1b49bb commit adc360c

File tree

6 files changed

+61
-34
lines changed

6 files changed

+61
-34
lines changed

cdap-common/src/test/java/io/cdap/cdap/common/internal/remote/RemoteTaskExecutorTest.java

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package io.cdap.cdap.common.internal.remote;
1818

19-
import com.google.common.util.concurrent.ListenableFuture;
19+
import com.google.common.util.concurrent.AbstractService;
20+
import com.google.common.util.concurrent.Service;
2021
import io.cdap.cdap.api.metrics.MetricsCollectionService;
2122
import io.cdap.cdap.api.metrics.MetricsContext;
2223
import io.cdap.cdap.api.service.worker.RemoteExecutionException;
@@ -39,6 +40,8 @@
3940
import java.util.HashMap;
4041
import java.util.Map;
4142
import java.util.concurrent.Executor;
43+
import java.util.concurrent.TimeUnit;
44+
import java.util.concurrent.TimeoutException;
4245
import org.apache.twill.common.Cancellable;
4346
import org.apache.twill.discovery.InMemoryDiscoveryService;
4447
import org.junit.After;
@@ -110,39 +113,69 @@ public byte[] decrypt(byte[] cipherData, byte[] associatedData) throws CipherExc
110113

111114
private MetricsCollectionService createMockMetricsCollectionService() {
112115
return new MetricsCollectionService() {
116+
private final AbstractService delegate = new AbstractService() {
117+
@Override
118+
protected void doStart() {
119+
notifyStarted();
120+
}
113121

114-
@Override
115-
public ListenableFuture<State> start() {
116-
return null;
117-
}
122+
@Override
123+
protected void doStop() {
124+
notifyStopped();
125+
}
126+
};
118127

119128
@Override
120-
public State startAndWait() {
121-
return null;
129+
public Service startAsync() {
130+
delegate.startAsync();
131+
return this;
122132
}
123133

124134
@Override
125135
public boolean isRunning() {
126-
return false;
136+
return delegate.isRunning();
127137
}
128138

129139
@Override
130140
public State state() {
131-
return null;
141+
return delegate.state();
132142
}
133143

134144
@Override
135-
public ListenableFuture<State> stop() {
136-
return null;
145+
public Service stopAsync() {
146+
delegate.stopAsync();
147+
return this;
137148
}
138149

139150
@Override
140-
public State stopAndWait() {
141-
return null;
151+
public void awaitRunning() {
152+
delegate.awaitRunning();
142153
}
143154

144155
@Override
145-
public void addListener(final Listener listener, final Executor executor) {}
156+
public void awaitRunning(long timeout, TimeUnit unit) throws TimeoutException {
157+
delegate.awaitRunning(timeout, unit);
158+
}
159+
160+
@Override
161+
public void awaitTerminated() {
162+
delegate.awaitTerminated();
163+
}
164+
165+
@Override
166+
public void awaitTerminated(long timeout, TimeUnit unit) throws TimeoutException {
167+
delegate.awaitTerminated(timeout, unit);
168+
}
169+
170+
@Override
171+
public Throwable failureCause() {
172+
return delegate.failureCause();
173+
}
174+
175+
@Override
176+
public void addListener(final Listener listener, final Executor executor) {
177+
delegate.addListener(listener, executor);
178+
}
146179

147180
@Override
148181
public MetricsContext getContext(Map<String, String> context) {

cdap-common/src/test/java/io/cdap/cdap/common/service/CommandPortServiceTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package io.cdap.cdap.common.service;
1818

19-
import com.google.common.util.concurrent.FutureCallback;
20-
import com.google.common.util.concurrent.Futures;
21-
import com.google.common.util.concurrent.MoreExecutors;
2219
import com.google.common.util.concurrent.Service;
2320
import java.io.BufferedReader;
2421
import java.io.BufferedWriter;
@@ -63,20 +60,18 @@ public void testCommandPortServer() throws Exception {
6360
.build();
6461

6562
final CountDownLatch stopLatch = new CountDownLatch(1);
66-
Futures.addCallback(server.start(), new FutureCallback<Service.State>() {
63+
server.addListener(new Service.Listener() {
6764
@Override
68-
public void onSuccess(Service.State result) {
65+
public void terminated(Service.State from) {
6966
stopLatch.countDown();
7067
}
7168

7269
@Override
73-
public void onFailure(Throwable t) {
70+
public void failed(Service.State from, Throwable failure) {
7471
stopLatch.countDown();
7572
}
76-
},
77-
MoreExecutors.directExecutor());
78-
// wait a bit for service to start
79-
TimeUnit.SECONDS.sleep(1);
73+
}, Runnable::run);
74+
server.startAsync().awaitRunning();
8075

8176
try {
8277
for (int i = 0; i < 10; i++) {

cdap-common/src/test/java/io/cdap/cdap/common/service/RetryOnStartFailureServiceTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.concurrent.TimeoutException;
2828
import java.util.function.Supplier;
2929
import org.apache.twill.common.Threads;
30-
import com.google.common.util.concurrent.Service;
3130
import org.junit.Assert;
3231
import org.junit.Test;
3332

@@ -61,7 +60,7 @@ public void failed(Service.State from, Throwable failure) {
6160
}
6261
}, Threads.SAME_THREAD_EXECUTOR);
6362

64-
service.start();
63+
service.startAsync();
6564
Assert.assertTrue(failureLatch.await(1, TimeUnit.SECONDS));
6665
Assert.assertFalse(startLatch.await(100, TimeUnit.MILLISECONDS));
6766
}

cdap-common/src/test/java/io/cdap/cdap/common/service/RetryableScheduledServiceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected long runTask() {
4545
}
4646
};
4747

48-
service.start();
48+
service.startAsync();
4949
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
5050
service.stopAsync().awaitTerminated();
5151
}
@@ -59,7 +59,7 @@ protected long runTask() throws Exception {
5959
}
6060
};
6161

62-
service.start();
62+
service.startAsync();
6363
// Wait for the service to fail
6464
Tasks.waitFor(Service.State.FAILED, service::state, 5, TimeUnit.SECONDS, 10, TimeUnit.MILLISECONDS);
6565

@@ -89,7 +89,7 @@ protected boolean shouldRetry(Exception ex) {
8989
}
9090
};
9191

92-
service.start();
92+
service.startAsync();
9393
// Wait for the service to fail
9494
Tasks.waitFor(Service.State.FAILED, service::state, 5, TimeUnit.SECONDS, 10, TimeUnit.MILLISECONDS);
9595

@@ -118,7 +118,7 @@ protected long runTask() throws Exception {
118118
return 1L;
119119
}
120120
};
121-
service.start();
121+
service.startAsync();
122122
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
123123
service.stopAsync().awaitTerminated();
124124
}

cdap-common/src/test/java/io/cdap/cdap/common/utils/TimeBoundIteratorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class TimeBoundIteratorTest {
3434
@Test
3535
public void testTimeBoundNotHit() {
3636
SettableTicker ticker = new SettableTicker(0);
37-
Stopwatch stopwatch = new Stopwatch(ticker);
37+
Stopwatch stopwatch = Stopwatch.createUnstarted(ticker);
3838

3939
List<Integer> list = new ArrayList<>();
4040
list.add(0);
@@ -54,7 +54,7 @@ public void testTimeBoundNotHit() {
5454
@Test
5555
public void testTimeBoundImmediatelyHit() {
5656
SettableTicker ticker = new SettableTicker(0);
57-
Stopwatch stopwatch = new Stopwatch(ticker);
57+
Stopwatch stopwatch = Stopwatch.createUnstarted(ticker);
5858

5959
List<Integer> list = new ArrayList<>();
6060
list.add(0);
@@ -70,7 +70,7 @@ public void testTimeBoundImmediatelyHit() {
7070
@Test
7171
public void testEarlyStop() {
7272
SettableTicker ticker = new SettableTicker(0);
73-
Stopwatch stopwatch = new Stopwatch(ticker);
73+
Stopwatch stopwatch = Stopwatch.createUnstarted(ticker);
7474

7575
List<Integer> list = new ArrayList<>();
7676
list.add(0);

cdap-common/src/test/java/io/cdap/cdap/common/zookeeper/election/LeaderElectionInfoServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void follower() {
105105
LOG.info("Follow: {}", participantId);
106106
}
107107
});
108-
leaderElection.start();
108+
leaderElection.startAsync();
109109
leaderElections.add(leaderElection);
110110
}
111111

0 commit comments

Comments
 (0)