use of org.easymock.Capture in project commons by twitter.
the class ThriftTest method testAsyncRetriesRecover.
@Test
public void testAsyncRetriesRecover() throws Exception {
// Capture the callback that Thift has wrapped around our callback.
Capture<AsyncMethodCallback<Integer>> callbackCapture = new Capture<AsyncMethodCallback<Integer>>();
// 1st call
expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture));
expectLastCall().andThrow(new TTransportException());
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
// 1st retry recovers
expectAsyncServiceRetry(false).calculateMass(eq("jake"), capture(callbackCapture));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.SUCCESS), anyLong());
// Verifies that our callback was called.
callback.onComplete(42);
Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
control.replay();
thrift.builder().withRetries(1).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create().calculateMass("jake", callback);
// Mimicks the async response from the server.
callbackCapture.getValue().onComplete(42);
assertRequestsTotal(thrift, 1);
assertErrorsTotal(thrift, 0);
assertReconnectsTotal(thrift, 0);
assertTimeoutsTotal(thrift, 0);
control.verify();
}
use of org.easymock.Capture in project commons by twitter.
the class SingletonServiceTest method testLeadMulti.
@Test
public void testLeadMulti() throws Exception {
List<Capture<Leader>> leaderCaptures = Lists.newArrayList();
List<Capture<LeaderControl>> leaderControlCaptures = Lists.newArrayList();
for (int i = 0; i < 5; i++) {
Capture<Leader> leaderCapture = new Capture<Leader>();
leaderCaptures.add(leaderCapture);
Capture<LeaderControl> controlCapture = createCapture();
leaderControlCaptures.add(controlCapture);
expect(candidate.offerLeadership(capture(leaderCapture))).andReturn(null);
listener.onLeading(capture(controlCapture));
InetSocketAddress primary = InetSocketAddress.createUnresolved("foo" + i, PORT_A);
Map<String, InetSocketAddress> aux = ImmutableMap.of("http-admin", InetSocketAddress.createUnresolved("foo" + i, PORT_B));
expect(serverSet.join(primary, aux)).andReturn(endpointStatus);
endpointStatus.leave();
abdicate.execute();
}
control.replay();
for (int i = 0; i < 5; i++) {
final String leaderName = "foo" + i;
newLeader(leaderName, leaderCaptures.get(i));
leaderControlCaptures.get(i).getValue().advertise();
leaderControlCaptures.get(i).getValue().leave();
}
}
use of org.easymock.Capture in project commons by twitter.
the class SingletonServiceTest method testLeadJoinFailure.
@Test
public void testLeadJoinFailure() throws Exception {
Capture<Leader> leaderCapture = new Capture<Leader>();
expect(candidate.offerLeadership(capture(leaderCapture))).andReturn(null);
Capture<LeaderControl> controlCapture = createCapture();
listener.onLeading(capture(controlCapture));
expectJoin().andThrow(new Group.JoinException("Injected join failure.", new Exception()));
abdicate.execute();
control.replay();
newLeader("foo", leaderCapture);
try {
controlCapture.getValue().advertise();
fail("Join should have failed.");
} catch (JoinException e) {
// Expected.
}
controlCapture.getValue().leave();
}
use of org.easymock.Capture in project android_frameworks_base by DirtyUnicorns.
the class NetworkStatsServiceTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
mServiceContext = new BroadcastInterceptingContext(getContext());
mStatsDir = getContext().getFilesDir();
if (mStatsDir.exists()) {
IoUtils.deleteContents(mStatsDir);
}
mNetManager = createMock(INetworkManagementService.class);
// TODO: Mock AlarmManager when migrating this test to Mockito.
AlarmManager alarmManager = (AlarmManager) mServiceContext.getSystemService(Context.ALARM_SERVICE);
mTime = createMock(TrustedTime.class);
mSettings = createMock(NetworkStatsSettings.class);
mConnManager = createMock(IConnectivityManager.class);
PowerManager powerManager = (PowerManager) mServiceContext.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mService = new NetworkStatsService(mServiceContext, mNetManager, alarmManager, wakeLock, mTime, TelephonyManager.getDefault(), mSettings, new NetworkStatsObservers(), mStatsDir, getBaseDir(mStatsDir));
mHandlerThread = new IdleableHandlerThread("HandlerThread");
mHandlerThread.start();
Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService);
mHandler = new Handler(mHandlerThread.getLooper(), callback);
mService.setHandler(mHandler, callback);
mService.bindConnectivityManager(mConnManager);
mElapsedRealtime = 0L;
expectCurrentTime();
expectDefaultSettings();
expectNetworkStatsUidDetail(buildEmptyStats());
expectSystemReady();
// catch INetworkManagementEventObserver during systemReady()
final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
mNetManager.registerObserver(capture(networkObserver));
expectLastCall().atLeastOnce();
replay();
mService.systemReady();
mSession = mService.openSession();
verifyAndReset();
mNetworkObserver = networkObserver.getValue();
}
use of org.easymock.Capture in project android_frameworks_base by DirtyUnicorns.
the class NetworkStatsServiceTest method testStatsRebootPersist.
public void testStatsRebootPersist() throws Exception {
assertStatsFilesExist(false);
// pretend that wifi network comes online; service should ask about full
// network state, and poll any existing interfaces before updating.
expectCurrentTime();
expectDefaultSettings();
expectNetworkState(buildWifiState());
expectNetworkStatsSummary(buildEmptyStats());
expectNetworkStatsUidDetail(buildEmptyStats());
expectNetworkStatsPoll();
expectBandwidthControlCheck();
replay();
mService.forceUpdateIfaces();
// verify service has empty history for wifi
assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
verifyAndReset();
// modify some number on wifi, and trigger poll event
incrementCurrentTime(HOUR_IN_MILLIS);
expectCurrentTime();
expectDefaultSettings();
expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
expectNetworkStatsPoll();
mService.setUidForeground(UID_RED, false);
mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
mService.setUidForeground(UID_RED, true);
mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
replay();
forcePollAndWaitForIdle();
// verify service recorded history
assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L, 6);
assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
verifyAndReset();
// graceful shutdown system, which should trigger persist of stats, and
// clear any values in memory.
expectCurrentTime();
expectDefaultSettings();
replay();
mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
verifyAndReset();
assertStatsFilesExist(true);
// boot through serviceReady() again
expectCurrentTime();
expectDefaultSettings();
expectNetworkStatsUidDetail(buildEmptyStats());
expectSystemReady();
// catch INetworkManagementEventObserver during systemReady()
final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
mNetManager.registerObserver(capture(networkObserver));
expectLastCall().atLeastOnce();
replay();
mService.systemReady();
mNetworkObserver = networkObserver.getValue();
// after systemReady(), we should have historical stats loaded again
assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L, 6);
assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
verifyAndReset();
}
Aggregations