MemoryServiceProxy extends ServiceProxy { public void forceGarbageCollection(ForceGarbageCollectionRequest request, StreamObserver<ForceGarbageCollectionResponse> observer) { if (myDevice.isOnline()) { int processId = request.getSession().getPid(); for (Client client : myDevice.getClients()) { if (processId == client.getClientData().getPid()) { client.executeGarbageCollector(); break; } } } observer.onNext(ForceGarbageCollectionResponse.newBuilder().build()); observer.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 testForceGarbageCollection() { Client client = mock(Client.class); ClientData clientData = mock(ClientData.class); when(client.getClientData()).thenReturn(clientData); when(clientData.getPid()).thenReturn(SESSION1.getPid()); when(myDevice.getClients()).thenReturn(new Client[]{client}); myProxy.forceGarbageCollection(ForceGarbageCollectionRequest.newBuilder().setSession(SESSION1).build(), mock(StreamObserver.class)); verify(client, times(1)).executeGarbageCollector(); }
@Test public void testForceGarbageCollectionWhenDeviceOffline() { when(myDevice.isOnline()).thenReturn(false); Client client = mock(Client.class); ClientData clientData = mock(ClientData.class); when(client.getClientData()).thenReturn(clientData); when(myDevice.getClients()).thenReturn(new Client[]{client}); myProxy.forceGarbageCollection(ForceGarbageCollectionRequest.newBuilder().setSession(SESSION1).build(), mock(StreamObserver.class)); verify(client, never()).executeGarbageCollector(); }
@Test public void testForceGarbageCollectionWhenClientIsNull() { Client client = mock(Client.class); ClientData clientData = mock(ClientData.class); when(client.getClientData()).thenReturn(clientData); when(clientData.getPid()).thenReturn(SESSION1.getPid()); when(myDevice.getClients()).thenReturn(new Client[0]); myProxy.forceGarbageCollection(ForceGarbageCollectionRequest.newBuilder().setSession(SESSION1).build(), mock(StreamObserver.class)); verify(client, never()).executeGarbageCollector(); }