MemoryServiceProxy extends ServiceProxy { public void trackAllocations(TrackAllocationsRequest request, StreamObserver<TrackAllocationsResponse> responseObserver) { long sessionId = request.getSession().getSessionId(); long timestamp = request.getRequestTime(); TrackAllocationsResponse response; if (myUseLegacyTracking) { response = request.getEnabled() ? enableAllocations(timestamp, sessionId) : disableAllocations(timestamp, sessionId); } else { response = myServiceStub.trackAllocations(request); } responseObserver.onNext(response); responseObserver.onCompleted(); } MemoryServiceProxy(@NotNull IDevice device,
                            @NotNull ManagedChannel channel,
                            @NotNull Executor fetchExecutor,
                            @NotNull BiFunction<IDevice, Integer, LegacyAllocationTracker> legacyTrackerSupplier,
                            Map<String, ByteString> proxyBytesCache); void startMonitoringApp(MemoryProfiler.MemoryStartRequest request,
                                 StreamObserver<MemoryProfiler.MemoryStartResponse> responseObserver); void stopMonitoringApp(MemoryProfiler.MemoryStopRequest request,
                                StreamObserver<MemoryProfiler.MemoryStopResponse> responseObserver); void getData(MemoryProfiler.MemoryRequest request, StreamObserver<MemoryProfiler.MemoryData> responseObserver); void trackAllocations(TrackAllocationsRequest request,
                               StreamObserver<TrackAllocationsResponse> responseObserver); void forceGarbageCollection(ForceGarbageCollectionRequest request, StreamObserver<ForceGarbageCollectionResponse> observer); @Override ServerServiceDefinition getServiceDefinition();  }
@Test public void testLegacyAllocationTrackingWorkflow() throws Exception { int time1 = 5; int time2 = 10; TrackAllocationsResponse expected1 = TrackAllocationsResponse.newBuilder() .setStatus(TrackStatus.newBuilder().setStatus(TrackStatus.Status.SUCCESS).setStartTime(time1)) .setInfo( AllocationsInfo.newBuilder().setStartTime(time1).setEndTime(Long.MAX_VALUE).setLegacy(true).build()).build(); StreamObserver<TrackAllocationsResponse> observer1 = mock(StreamObserver.class); myProxy.trackAllocations( TrackAllocationsRequest.newBuilder().setSession(SESSION1).setEnabled(true).setRequestTime(time1).build(), observer1); assertThat(myTracker.getTrackingState()).isTrue(); verify(observer1, times(1)).onNext(expected1); verify(observer1, times(1)).onCompleted(); assertThat(myProxyBytesCache.containsKey(Integer.toString(time1))).isFalse(); TrackAllocationsResponse expected2 = TrackAllocationsResponse.newBuilder() .setStatus(TrackStatus.newBuilder().setStatus(TrackStatus.Status.SUCCESS).setStartTime(time1)) .setInfo(AllocationsInfo.newBuilder().setStartTime(time1).setEndTime(time2).setSuccess(true).setLegacy(true).build()) .build(); StreamObserver<TrackAllocationsResponse> observer2 = mock(StreamObserver.class); myProxy.trackAllocations( TrackAllocationsRequest.newBuilder().setSession(SESSION1).setEnabled(false).setRequestTime(time2).build(), observer2); verify(observer2, times(1)).onNext(expected2); verify(observer2, times(1)).onCompleted(); myTracker.getParsingWaitLatch().countDown(); myTracker.getParsingDoneLatch().await(); assertThat(myProxyBytesCache.get(Integer.toString(time1))) .isEqualTo(ByteString.copyFrom(FakeLegacyAllocationTracker.Companion.getRAW_DATA())); }
@Test public void testLegacyAllocationTrackingReturningNullData() throws Exception { int time1 = 5; int time2 = 10; myProxy.trackAllocations( TrackAllocationsRequest.newBuilder().setSession(SESSION1).setEnabled(true).setRequestTime(time1).build(), mock(StreamObserver.class)); myProxy.trackAllocations( TrackAllocationsRequest.newBuilder().setSession(SESSION1).setEnabled(false).setRequestTime(time2).build(), mock(StreamObserver.class)); myTracker.setReturnNullTrackingData(true); myTracker.getParsingWaitLatch().countDown(); myTracker.getParsingDoneLatch().await(); assertThat(myProxyBytesCache.get(Integer.toString(time1))).isEqualTo(ByteString.EMPTY); }