1 /* 2 * Copyright (C) 2015 The Dagger Authors. 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 17 package dagger.producers.monitoring; 18 19 import static org.mockito.Mockito.any; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.verifyNoMoreInteractions; 22 import static org.mockito.Mockito.when; 23 24 import com.google.common.testing.FakeTicker; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 import org.mockito.Mock; 30 import org.mockito.MockitoAnnotations; 31 32 @RunWith(JUnit4.class) 33 public final class TimingProductionComponentMonitorTest { 34 private static final class ProducerClassA {} 35 36 private static final class ProducerClassB {} 37 38 @Mock private ProductionComponentTimingRecorder.Factory productionComponentTimingRecorderFactory; 39 @Mock private ProductionComponentTimingRecorder productionComponentTimingRecorder; 40 @Mock private ProducerTimingRecorder producerTimingRecorderA; 41 @Mock private ProducerTimingRecorder producerTimingRecorderB; 42 43 private FakeTicker ticker; 44 private ProductionComponentMonitor.Factory monitorFactory; 45 46 @Before setUp()47 public void setUp() { 48 MockitoAnnotations.initMocks(this); 49 when(productionComponentTimingRecorderFactory.create(any(Object.class))) 50 .thenReturn(productionComponentTimingRecorder); 51 when( 52 productionComponentTimingRecorder.producerTimingRecorderFor( 53 ProducerToken.create(ProducerClassA.class))) 54 .thenReturn(producerTimingRecorderA); 55 when( 56 productionComponentTimingRecorder.producerTimingRecorderFor( 57 ProducerToken.create(ProducerClassB.class))) 58 .thenReturn(producerTimingRecorderB); 59 ticker = new FakeTicker(); 60 monitorFactory = 61 new TimingProductionComponentMonitor.Factory( 62 productionComponentTimingRecorderFactory, ticker); 63 } 64 65 @Test normalExecution_success()66 public void normalExecution_success() { 67 ProductionComponentMonitor monitor = monitorFactory.create(new Object()); 68 ProducerMonitor producerMonitorA = 69 monitor.producerMonitorFor(ProducerToken.create(ProducerClassA.class)); 70 ticker.advance(5000222); 71 producerMonitorA.methodStarting(); 72 ticker.advance(1333); 73 producerMonitorA.methodFinished(); 74 ticker.advance(40000555); 75 ProducerMonitor producerMonitorB = 76 monitor.producerMonitorFor(ProducerToken.create(ProducerClassB.class)); 77 producerMonitorB.methodStarting(); 78 ticker.advance(2000777); 79 producerMonitorA.succeeded(new Object()); 80 ticker.advance(3000999); 81 producerMonitorB.methodFinished(); 82 ticker.advance(100000222); 83 producerMonitorB.succeeded(new Object()); 84 85 verify(producerTimingRecorderA).recordMethod(5000222, 1333); 86 verify(producerTimingRecorderA).recordSuccess(1333 + 40000555 + 2000777); 87 verify(producerTimingRecorderB).recordMethod(5000222 + 1333 + 40000555, 2000777 + 3000999); 88 verify(producerTimingRecorderB).recordSuccess(2000777 + 3000999 + 100000222); 89 verifyNoMoreInteractions(producerTimingRecorderA, producerTimingRecorderB); 90 } 91 92 @Test normalExecution_failure()93 public void normalExecution_failure() { 94 Throwable failureA = new RuntimeException("monkey"); 95 Throwable failureB = new RuntimeException("gorilla"); 96 ProductionComponentMonitor monitor = monitorFactory.create(new Object()); 97 ProducerMonitor producerMonitorA = 98 monitor.producerMonitorFor(ProducerToken.create(ProducerClassA.class)); 99 ticker.advance(5000222); 100 producerMonitorA.methodStarting(); 101 ticker.advance(1333); 102 producerMonitorA.methodFinished(); 103 ticker.advance(40000555); 104 ProducerMonitor producerMonitorB = 105 monitor.producerMonitorFor(ProducerToken.create(ProducerClassB.class)); 106 producerMonitorB.methodStarting(); 107 ticker.advance(2000777); 108 producerMonitorA.failed(failureA); 109 ticker.advance(3000999); 110 producerMonitorB.methodFinished(); 111 ticker.advance(100000222); 112 producerMonitorB.failed(failureB); 113 114 verify(producerTimingRecorderA).recordMethod(5000222, 1333); 115 verify(producerTimingRecorderA).recordFailure(failureA, 1333 + 40000555 + 2000777); 116 verify(producerTimingRecorderB).recordMethod(5000222 + 1333 + 40000555, 2000777 + 3000999); 117 verify(producerTimingRecorderB).recordFailure(failureB, 2000777 + 3000999 + 100000222); 118 verifyNoMoreInteractions(producerTimingRecorderA, producerTimingRecorderB); 119 } 120 } 121