Search in sources :

Example 1 with FakeNuProcess

use of com.facebook.buck.util.FakeNuProcess in project buck by facebook.

the class ProcessTrackerTest method testExternalProcessInfo.

@Test
public void testExternalProcessInfo() throws Exception {
    BlockingQueue<ProcessResourceConsumptionEvent> events = new LinkedBlockingQueue<>();
    try (ProcessTrackerForTest processTracker = createProcessTracker(events)) {
        FakeNuProcess proc1 = new FakeNuProcess(111);
        ProcessResourceConsumption res1 = createConsumption(42, 3, 0);
        processHelper.setProcessResourceConsumption(111, res1);
        ProcessTracker.ExternalProcessInfo info1 = processTracker.new ExternalProcessInfo(111, proc1, createParams("proc1"), CONTEXT);
        assertFalse(info1.hasProcessFinished());
        testProcessInfo(info1, 111, "proc1", Optional.of(createParams("proc1")), res1, events);
        FakeNuProcess proc2 = new FakeNuProcess(222);
        ProcessTracker.ExternalProcessInfo info2 = processTracker.new ExternalProcessInfo(222, proc2, createParams("proc2"), CONTEXT);
        assertFalse(info2.hasProcessFinished());
        testProcessInfo(info2, 222, "proc2", Optional.of(createParams("proc2")), null, events);
        proc1.finish(-3);
        assertTrue(info1.hasProcessFinished());
    }
}
Also used : ProcessResourceConsumptionEvent(com.facebook.buck.util.perf.ProcessTracker.ProcessResourceConsumptionEvent) ProcessResourceConsumption(com.facebook.buck.util.ProcessResourceConsumption) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) FakeNuProcess(com.facebook.buck.util.FakeNuProcess) Test(org.junit.Test)

Example 2 with FakeNuProcess

use of com.facebook.buck.util.FakeNuProcess in project buck by facebook.

the class ProcessTrackerTest method testInteraction.

@Test
public void testInteraction() throws Exception {
    BlockingQueue<ProcessResourceConsumptionEvent> events = new LinkedBlockingQueue<>();
    try (ProcessTrackerForTest processTracker = createProcessTracker(events)) {
        processTracker.explicitStartUp();
        processTracker.verifyThisProcessInfo(PID, "<buck-process>");
        assertEquals(1, processTracker.processesInfo.size());
        verifyEvents(ImmutableMap.of(), null, events);
        // Verify that ProcessTracker subscribes to ProcessRegistry and that calling
        // registerProcess causes ProcessTracker to start tracking the process.
        FakeNuProcess proc41 = new FakeNuProcess(41);
        processTracker.verifyNoProcessInfo(41);
        assertEquals(1, processTracker.processesInfo.size());
        processRegistry.registerProcess(proc41, createParams("proc41"), CONTEXT);
        processTracker.verifyExternalProcessInfo(41, proc41, createParams("proc41"));
        assertEquals(2, processTracker.processesInfo.size());
        verifyEvents(ImmutableMap.of(), null, events);
        // Verify that after registering a new process, both are being tracked
        FakeNuProcess proc42 = new FakeNuProcess(42);
        processRegistry.registerProcess(proc42, createParams("proc42"), CONTEXT);
        processTracker.verifyExternalProcessInfo(41, proc41, createParams("proc41"));
        processTracker.verifyExternalProcessInfo(42, proc42, createParams("proc42"));
        assertEquals(3, processTracker.processesInfo.size());
        verifyEvents(ImmutableMap.of(), null, events);
        // Verify that after registering a process with an already tracked pid,
        // the old process info gets discarded.
        FakeNuProcess proc41b = new FakeNuProcess(41);
        processRegistry.registerProcess(proc41b, createParams("proc41b"), CONTEXT);
        processTracker.verifyExternalProcessInfo(42, proc42, createParams("proc42"));
        processTracker.verifyExternalProcessInfo(41, proc41b, createParams("proc41b"));
        assertEquals(3, processTracker.processesInfo.size());
        // Verify an event has been posted to the bus on remove
        verifyEvents(ImmutableMap.of("proc41", Optional.of(createParams("proc41"))), null, events);
        // Verify that processes whose pid cannot be obtained are ignored
        processRegistry.registerProcess(new FakeProcess(0), createParams("proc0"), CONTEXT);
        processTracker.verifyExternalProcessInfo(42, proc42, createParams("proc42"));
        processTracker.verifyExternalProcessInfo(41, proc41b, createParams("proc41b"));
        assertEquals(3, processTracker.processesInfo.size());
        verifyEvents(ImmutableMap.of(), null, events);
        // Verify that ongoing processes are kept after refresh
        processTracker.explicitRunOneIteration();
        processTracker.verifyExternalProcessInfo(42, proc42, createParams("proc42"));
        processTracker.verifyExternalProcessInfo(41, proc41b, createParams("proc41b"));
        assertEquals(3, processTracker.processesInfo.size());
        verifyEvents(ImmutableMap.of(), null, events);
        // Verify that finished processes are removed after refresh
        proc42.finish(0);
        processTracker.explicitRunOneIteration();
        processTracker.verifyExternalProcessInfo(41, proc41b, createParams("proc41b"));
        processTracker.verifyNoProcessInfo(42);
        assertEquals(2, processTracker.processesInfo.size());
        // Verify an event has been posted to the bus on remove
        verifyEvents(ImmutableMap.of("proc42", Optional.of(createParams("proc42"))), null, events);
        processTracker.explicitShutDown();
        processTracker.verifyNoProcessInfo(41);
        processTracker.verifyNoProcessInfo(PID);
        assertEquals(0, processTracker.processesInfo.size());
        // Verify events for the existing processes have been posted to the bus on shut down
        verifyEvents(ImmutableMap.of("proc41b", Optional.of(createParams("proc41b")), "<buck-process>", Optional.empty()), null, events);
    }
    // verify no events are sent after closing ProcessTracker
    processRegistry.registerProcess(new FakeNuProcess(43), createParams("proc43"), CONTEXT);
    verifyEvents(ImmutableMap.of(), null, events);
}
Also used : ProcessResourceConsumptionEvent(com.facebook.buck.util.perf.ProcessTracker.ProcessResourceConsumptionEvent) FakeProcess(com.facebook.buck.util.FakeProcess) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) FakeNuProcess(com.facebook.buck.util.FakeNuProcess) Test(org.junit.Test)

Aggregations

FakeNuProcess (com.facebook.buck.util.FakeNuProcess)2 ProcessResourceConsumptionEvent (com.facebook.buck.util.perf.ProcessTracker.ProcessResourceConsumptionEvent)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Test (org.junit.Test)2 FakeProcess (com.facebook.buck.util.FakeProcess)1 ProcessResourceConsumption (com.facebook.buck.util.ProcessResourceConsumption)1