MemoryServiceProxy extends ServiceProxy { public void getData(MemoryProfiler.MemoryRequest request, StreamObserver<MemoryProfiler.MemoryData> responseObserver) { try { MemoryProfiler.MemoryData data = myServiceStub.getData(request); if (myUseLegacyTracking) { synchronized (myUpdatingDataLock) { Long2ObjectMap<AllocationsInfo> infos = myTrackingInfos.get(request.getSession().getSessionId()); MemoryProfiler.MemoryData.Builder rebuilder = data.toBuilder(); long requestStartTime = request.getStartTime(); long requestEndTime = request.getEndTime(); List<AllocationsInfo> infosToReturn = new ArrayList<>(); for (AllocationsInfo info : infos.values()) { if (info.getStartTime() <= requestEndTime && info.getEndTime() > requestStartTime) { infosToReturn.add(info); } } infosToReturn.sort(Comparator.comparingLong(AllocationsInfo::getStartTime)); for (AllocationsInfo info : infosToReturn) { rebuilder.addAllocationsInfo(info); long infoMax = info.getEndTime() == Long.MAX_VALUE ? info.getStartTime() : info.getEndTime(); rebuilder.setEndTimestamp(Math.max(rebuilder.getEndTimestamp(), infoMax)); } data = rebuilder.build(); } } responseObserver.onNext(data); responseObserver.onCompleted(); } catch (RuntimeException e) { responseObserver.onError(e); } } 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 testGetDataWithoutAllocationsInfo() throws Exception { MemoryData.MemorySample memSample = MemoryData.MemorySample.newBuilder().setTimestamp(1) .setMemoryUsage(Memory.MemoryUsageData.newBuilder().setJavaMem(1)).build(); MemoryData memData = MemoryProfiler.MemoryData.newBuilder().addMemSamples(memSample).build(); myService.setMemoryData(memData); StreamObserver<MemoryData> observer = mock(StreamObserver.class); myProxy.getData(MemoryRequest.newBuilder().setSession(SESSION1).build(), observer); verify(observer, times(1)).onNext(memData); verify(observer, times(1)).onCompleted(); }