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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
/*
* Copyright (C) 2012 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 static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.TestLogHandler;
import com.google.common.util.concurrent.Service.State;
import com.google.common.util.concurrent.ServiceManager.Listener;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import junit.framework.TestCase;
/**
* Tests for {@link ServiceManager}.
*
* @author Luke Sandberg
* @author Chris Nokleberg
*/
public class ServiceManagerTest extends TestCase {
private static class NoOpService extends AbstractService {
@Override
protected void doStart() {
notifyStarted();
}
@Override
protected void doStop() {
notifyStopped();
}
}
/*
* A NoOp service that will delay the startup and shutdown notification for a configurable amount
* of time.
*/
private static class NoOpDelayedService extends NoOpService {
private long delay;
public NoOpDelayedService(long delay) {
this.delay = delay;
}
@Override
protected void doStart() {
new Thread() {
@Override
public void run() {
Uninterruptibles.sleepUninterruptibly(delay, TimeUnit.MILLISECONDS);
notifyStarted();
}
}.start();
}
@Override
protected void doStop() {
new Thread() {
@Override
public void run() {
Uninterruptibles.sleepUninterruptibly(delay, TimeUnit.MILLISECONDS);
notifyStopped();
}
}.start();
}
}
private static class FailStartService extends NoOpService {
@Override
protected void doStart() {
notifyFailed(new IllegalStateException("start failure"));
}
}
private static class FailRunService extends NoOpService {
@Override
protected void doStart() {
super.doStart();
notifyFailed(new IllegalStateException("run failure"));
}
}
private static class FailStopService extends NoOpService {
@Override
protected void doStop() {
notifyFailed(new IllegalStateException("stop failure"));
}
}
public void testServiceStartupTimes() {
Service a = new NoOpDelayedService(150);
Service b = new NoOpDelayedService(353);
ServiceManager serviceManager = new ServiceManager(asList(a, b));
serviceManager.startAsync().awaitHealthy();
ImmutableMap<Service, Long> startupTimes = serviceManager.startupTimes();
assertThat(startupTimes).hasSize(2);
assertThat(startupTimes.get(a)).isAtLeast(150);
assertThat(startupTimes.get(b)).isAtLeast(353);
}
public void testServiceStartupTimes_selfStartingServices() {
// This tests to ensure that:
// 1. service times are accurate when the service is started by the manager
// 2. service times are recorded when the service is not started by the manager (but they may
// not be accurate).
final Service b =
new NoOpDelayedService(353) {
@Override
protected void doStart() {
super.doStart();
// This will delay service listener execution at least 150 milliseconds
Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);
}
};
Service a =
new NoOpDelayedService(150) {
@Override
protected void doStart() {
b.startAsync();
super.doStart();
}
};
ServiceManager serviceManager = new ServiceManager(asList(a, b));
serviceManager.startAsync().awaitHealthy();
ImmutableMap<Service, Long> startupTimes = serviceManager.startupTimes();
assertThat(startupTimes).hasSize(2);
assertThat(startupTimes.get(a)).isAtLeast(150);
// Service b startup takes at least 353 millis, but starting the timer is delayed by at least
// 150 milliseconds. so in a perfect world the timing would be 353-150=203ms, but since either
// of our sleep calls can be arbitrarily delayed we should just assert that there is a time
// recorded.
assertThat(startupTimes.get(b)).isNotNull();
}
public void testServiceStartStop() {
Service a = new NoOpService();
Service b = new NoOpService();
ServiceManager manager = new ServiceManager(asList(a, b));
RecordingListener listener = new RecordingListener();
manager.addListener(listener, directExecutor());
assertState(manager, Service.State.NEW, a, b);
assertFalse(manager.isHealthy());
manager.startAsync().awaitHealthy();
assertState(manager, Service.State.RUNNING, a, b);
assertTrue(manager.isHealthy());
assertTrue(listener.healthyCalled);
assertFalse(listener.stoppedCalled);
assertTrue(listener.failedServices.isEmpty());
manager.stopAsync().awaitStopped();
assertState(manager, Service.State.TERMINATED, a, b);
assertFalse(manager.isHealthy());
assertTrue(listener.stoppedCalled);
assertTrue(listener.failedServices.isEmpty());
}
public void testFailStart() throws Exception {
Service a = new NoOpService();
Service b = new FailStartService();
Service c = new NoOpService();
Service d = new FailStartService();
Service e = new NoOpService();
ServiceManager manager = new ServiceManager(asList(a, b, c, d, e));
RecordingListener listener = new RecordingListener();
manager.addListener(listener, directExecutor());
assertState(manager, Service.State.NEW, a, b, c, d, e);
try {
manager.startAsync().awaitHealthy();
fail();
} catch (IllegalStateException expected) {
}
assertFalse(listener.healthyCalled);
assertState(manager, Service.State.RUNNING, a, c, e);
assertEquals(ImmutableSet.of(b, d), listener.failedServices);
assertState(manager, Service.State.FAILED, b, d);
assertFalse(manager.isHealthy());
manager.stopAsync().awaitStopped();
assertFalse(manager.isHealthy());
assertFalse(listener.healthyCalled);
assertTrue(listener.stoppedCalled);
}
public void testFailRun() throws Exception {
Service a = new NoOpService();
Service b = new FailRunService();
ServiceManager manager = new ServiceManager(asList(a, b));
RecordingListener listener = new RecordingListener();
manager.addListener(listener, directExecutor());
assertState(manager, Service.State.NEW, a, b);
try {
manager.startAsync().awaitHealthy();
fail();
} catch (IllegalStateException expected) {
}
assertTrue(listener.healthyCalled);
assertEquals(ImmutableSet.of(b), listener.failedServices);
manager.stopAsync().awaitStopped();
assertState(manager, Service.State.FAILED, b);
assertState(manager, Service.State.TERMINATED, a);
assertTrue(listener.stoppedCalled);
}
public void testFailStop() throws Exception {
Service a = new NoOpService();
Service b = new FailStopService();
Service c = new NoOpService();
ServiceManager manager = new ServiceManager(asList(a, b, c));
RecordingListener listener = new RecordingListener();
manager.addListener(listener, directExecutor());
manager.startAsync().awaitHealthy();
assertTrue(listener.healthyCalled);
assertFalse(listener.stoppedCalled);
manager.stopAsync().awaitStopped();
assertTrue(listener.stoppedCalled);
assertEquals(ImmutableSet.of(b), listener.failedServices);
assertState(manager, Service.State.FAILED, b);
assertState(manager, Service.State.TERMINATED, a, c);
}
public void testToString() throws Exception {
Service a = new NoOpService();
Service b = new FailStartService();
ServiceManager manager = new ServiceManager(asList(a, b));
String toString = manager.toString();
assertThat(toString).contains("NoOpService");
assertThat(toString).contains("FailStartService");
}
public void testTimeouts() throws Exception {
Service a = new NoOpDelayedService(50);
ServiceManager manager = new ServiceManager(asList(a));
manager.startAsync();
try {
manager.awaitHealthy(1, TimeUnit.MILLISECONDS);
fail();
} catch (TimeoutException expected) {
}
manager.awaitHealthy(5, SECONDS); // no exception thrown
manager.stopAsync();
try {
manager.awaitStopped(1, TimeUnit.MILLISECONDS);
fail();
} catch (TimeoutException expected) {
}
manager.awaitStopped(5, SECONDS); // no exception thrown
}
/**
* This covers a case where if the last service to stop failed then the stopped callback would
* never be called.
*/
public void testSingleFailedServiceCallsStopped() {
Service a = new FailStartService();
ServiceManager manager = new ServiceManager(asList(a));
RecordingListener listener = new RecordingListener();
manager.addListener(listener, directExecutor());
try {
manager.startAsync().awaitHealthy();
fail();
} catch (IllegalStateException expected) {
}
assertTrue(listener.stoppedCalled);
}
/**
* This covers a bug where listener.healthy would get called when a single service failed during
* startup (it occurred in more complicated cases also).
*/
public void testFailStart_singleServiceCallsHealthy() {
Service a = new FailStartService();
ServiceManager manager = new ServiceManager(asList(a));
RecordingListener listener = new RecordingListener();
manager.addListener(listener, directExecutor());
try {
manager.startAsync().awaitHealthy();
fail();
} catch (IllegalStateException expected) {
}
assertFalse(listener.healthyCalled);
}
/**
* This covers a bug where if a listener was installed that would stop the manager if any service
* fails and something failed during startup before service.start was called on all the services,
* then awaitStopped would deadlock due to an IllegalStateException that was thrown when trying to
* stop the timer(!).
*/
public void testFailStart_stopOthers() throws TimeoutException {
Service a = new FailStartService();
Service b = new NoOpService();
final ServiceManager manager = new ServiceManager(asList(a, b));
manager.addListener(
new Listener() {
@Override
public void failure(Service service) {
manager.stopAsync();
}
},
directExecutor());
manager.startAsync();
manager.awaitStopped(10, TimeUnit.MILLISECONDS);
}
public void testDoCancelStart() throws TimeoutException {
Service a =
new AbstractService() {
@Override
protected void doStart() {
// Never starts!
}
@Override
protected void doCancelStart() {
assertThat(state()).isEqualTo(Service.State.STOPPING);
notifyStopped();
}
@Override
protected void doStop() {
throw new AssertionError(); // Should not be called.
}
};
final ServiceManager manager = new ServiceManager(asList(a));
manager.startAsync();
manager.stopAsync();
manager.awaitStopped(10, TimeUnit.MILLISECONDS);
assertThat(manager.servicesByState().keySet()).containsExactly(Service.State.TERMINATED);
}
public void testNotifyStoppedAfterFailure() throws TimeoutException {
Service a =
new AbstractService() {
@Override
protected void doStart() {
notifyFailed(new IllegalStateException("start failure"));
notifyStopped(); // This will be a no-op.
}
@Override
protected void doStop() {
notifyStopped();
}
};
final ServiceManager manager = new ServiceManager(asList(a));
manager.startAsync();
manager.awaitStopped(10, TimeUnit.MILLISECONDS);
assertThat(manager.servicesByState().keySet()).containsExactly(Service.State.FAILED);
}
private static void assertState(
ServiceManager manager, Service.State state, Service... services) {
Collection<Service> managerServices = manager.servicesByState().get(state);
for (Service service : services) {
assertEquals(service.toString(), state, service.state());
assertEquals(service.toString(), service.isRunning(), state == Service.State.RUNNING);
assertTrue(managerServices + " should contain " + service, managerServices.contains(service));
}
}
/**
* This is for covering a case where the ServiceManager would behave strangely if constructed with
* no service under management. Listeners would never fire because the ServiceManager was healthy
* and stopped at the same time. This test ensures that listeners fire and isHealthy makes sense.
*/
public void testEmptyServiceManager() {
Logger logger = Logger.getLogger(ServiceManager.class.getName());
logger.setLevel(Level.FINEST);
TestLogHandler logHandler = new TestLogHandler();
logger.addHandler(logHandler);
ServiceManager manager = new ServiceManager(Arrays.<Service>asList());
RecordingListener listener = new RecordingListener();
manager.addListener(listener, directExecutor());
manager.startAsync().awaitHealthy();
assertTrue(manager.isHealthy());
assertTrue(listener.healthyCalled);
assertFalse(listener.stoppedCalled);
assertTrue(listener.failedServices.isEmpty());
manager.stopAsync().awaitStopped();
assertFalse(manager.isHealthy());
assertTrue(listener.stoppedCalled);
assertTrue(listener.failedServices.isEmpty());
// check that our NoOpService is not directly observable via any of the inspection methods or
// via logging.
assertEquals("ServiceManager{services=[]}", manager.toString());
assertTrue(manager.servicesByState().isEmpty());
assertTrue(manager.startupTimes().isEmpty());
Formatter logFormatter =
new Formatter() {
@Override
public String format(LogRecord record) {
return formatMessage(record);
}
};
for (LogRecord record : logHandler.getStoredLogRecords()) {
assertThat(logFormatter.format(record)).doesNotContain("NoOpService");
}
}
/**
* Tests that a ServiceManager can be fully shut down if one of its failure listeners is slow or
* even permanently blocked.
*/
public void testListenerDeadlock() throws InterruptedException {
final CountDownLatch failEnter = new CountDownLatch(1);
final CountDownLatch failLeave = new CountDownLatch(1);
final CountDownLatch afterStarted = new CountDownLatch(1);
Service failRunService =
new AbstractService() {
@Override
protected void doStart() {
new Thread() {
@Override
public void run() {
notifyStarted();
// We need to wait for the main thread to leave the ServiceManager.startAsync call
// to
// ensure that the thread running the failure callbacks is not the main thread.
Uninterruptibles.awaitUninterruptibly(afterStarted);
notifyFailed(new Exception("boom"));
}
}.start();
}
@Override
protected void doStop() {
notifyStopped();
}
};
final ServiceManager manager =
new ServiceManager(Arrays.asList(failRunService, new NoOpService()));
manager.addListener(
new ServiceManager.Listener() {
@Override
public void failure(Service service) {
failEnter.countDown();
// block until after the service manager is shutdown
Uninterruptibles.awaitUninterruptibly(failLeave);
}
},
directExecutor());
manager.startAsync();
afterStarted.countDown();
// We do not call awaitHealthy because, due to races, that method may throw an exception. But
// we really just want to wait for the thread to be in the failure callback so we wait for that
// explicitly instead.
failEnter.await();
assertFalse("State should be updated before calling listeners", manager.isHealthy());
// now we want to stop the services.
Thread stoppingThread =
new Thread() {
@Override
public void run() {
manager.stopAsync().awaitStopped();
}
};
stoppingThread.start();
// this should be super fast since the only non stopped service is a NoOpService
stoppingThread.join(1000);
assertFalse("stopAsync has deadlocked!.", stoppingThread.isAlive());
failLeave.countDown(); // release the background thread
}
/**
* Catches a bug where when constructing a service manager failed, later interactions with the
* service could cause IllegalStateExceptions inside the partially constructed ServiceManager.
* This ISE wouldn't actually bubble up but would get logged by ExecutionQueue. This obfuscated
* the original error (which was not constructing ServiceManager correctly).
*/
public void testPartiallyConstructedManager() {
Logger logger = Logger.getLogger("global");
logger.setLevel(Level.FINEST);
TestLogHandler logHandler = new TestLogHandler();
logger.addHandler(logHandler);
NoOpService service = new NoOpService();
service.startAsync();
try {
new ServiceManager(Arrays.asList(service));
fail();
} catch (IllegalArgumentException expected) {
}
service.stopAsync();
// Nothing was logged!
assertEquals(0, logHandler.getStoredLogRecords().size());
}
public void testPartiallyConstructedManager_transitionAfterAddListenerBeforeStateIsReady() {
// The implementation of this test is pretty sensitive to the implementation :( but we want to
// ensure that if weird things happen during construction then we get exceptions.
final NoOpService service1 = new NoOpService();
// This service will start service1 when addListener is called. This simulates service1 being
// started asynchronously.
Service service2 =
new Service() {
final NoOpService delegate = new NoOpService();
@Override
public final void addListener(Listener listener, Executor executor) {
service1.startAsync();
delegate.addListener(listener, executor);
}
// Delegates from here on down
@Override
public final Service startAsync() {
return delegate.startAsync();
}
@Override
public final Service stopAsync() {
return delegate.stopAsync();
}
@Override
public final void awaitRunning() {
delegate.awaitRunning();
}
@Override
public final void awaitRunning(long timeout, TimeUnit unit) throws TimeoutException {
delegate.awaitRunning(timeout, unit);
}
@Override
public final void awaitTerminated() {
delegate.awaitTerminated();
}
@Override
public final void awaitTerminated(long timeout, TimeUnit unit) throws TimeoutException {
delegate.awaitTerminated(timeout, unit);
}
@Override
public final boolean isRunning() {
return delegate.isRunning();
}
@Override
public final State state() {
return delegate.state();
}
@Override
public final Throwable failureCause() {
return delegate.failureCause();
}
};
try {
new ServiceManager(Arrays.asList(service1, service2));
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected.getMessage()).contains("started transitioning asynchronously");
}
}
/**
* This test is for a case where two Service.Listener callbacks for the same service would call
* transitionService in the wrong order due to a race. Due to the fact that it is a race this test
* isn't guaranteed to expose the issue, but it is at least likely to become flaky if the race
* sneaks back in, and in this case flaky means something is definitely wrong.
*
* <p>Before the bug was fixed this test would fail at least 30% of the time.
*/
public void testTransitionRace() throws TimeoutException {
for (int k = 0; k < 1000; k++) {
List<Service> services = Lists.newArrayList();
for (int i = 0; i < 5; i++) {
services.add(new SnappyShutdownService(i));
}
ServiceManager manager = new ServiceManager(services);
manager.startAsync().awaitHealthy();
manager.stopAsync().awaitStopped(10, TimeUnit.SECONDS);
}
}
/**
* This service will shutdown very quickly after stopAsync is called and uses a background thread
* so that we know that the stopping() listeners will execute on a different thread than the
* terminated() listeners.
*/
private static class SnappyShutdownService extends AbstractExecutionThreadService {
final int index;
final CountDownLatch latch = new CountDownLatch(1);
SnappyShutdownService(int index) {
this.index = index;
}
@Override
protected void run() throws Exception {
latch.await();
}
@Override
protected void triggerShutdown() {
latch.countDown();
}
@Override
protected String serviceName() {
return this.getClass().getSimpleName() + "[" + index + "]";
}
}
public void testNulls() {
ServiceManager manager = new ServiceManager(Arrays.<Service>asList());
new NullPointerTester()
.setDefault(ServiceManager.Listener.class, new RecordingListener())
.testAllPublicInstanceMethods(manager);
}
private static final class RecordingListener extends ServiceManager.Listener {
volatile boolean healthyCalled;
volatile boolean stoppedCalled;
final Set<Service> failedServices = Sets.newConcurrentHashSet();
@Override
public void healthy() {
healthyCalled = true;
}
@Override
public void stopped() {
stoppedCalled = true;
}
@Override
public void failure(Service service) {
failedServices.add(service);
}
}
}
|