func TestCleansOnlyUnusedProcesses(t *testing.T) {
	p1 := startAndWaitTestProcess(testCmd, t)
	p2 := startAndWaitTestProcess(testCmd, t)

	time.Sleep(500 * time.Millisecond)

	// use one of the processes, so it is used now
	process.Get(p1.Pid)

	// cleanup immediately
	(&process.Cleaner{CleanupPeriod: 0, CleanupThreshold: 500 * time.Millisecond}).CleanOnce()

	_, err1 := process.Get(p1.Pid)
	_, err2 := process.Get(p2.Pid)

	// process 1 must be cleaned
	if err1 != nil {
		t.Fatalf("Expected process 2 to exist, but got an error: %s", err1.Error())
	}

	// process 2 must exist
	if _, ok := err2.(*process.NoProcessError); !ok {
		t.Fatal("Expected process 2 to be cleaned")
	}
}
func TestCleanWithZeroThreshold(t *testing.T) {
	p := startAndWaitTestProcess(testCmd, t)
	defer cleanupLogsDir()

	process.NewCleaner(0, 0).CleanOnce()

	_, err := process.Get(p.Pid)
	if err == nil {
		t.Fatal("Must not exist")
	}
	if _, ok := err.(*process.NoProcessError); !ok {
		t.Fatal(err)
	}
}
Beispiel #3
0
func startAndWaitTestProcess(cmd string, t *testing.T) process.MachineProcess {
	process.LogsDir = TmpFile()
	events := make(chan *rpc.Event)
	done := make(chan bool)

	// Create and start process
	pb := process.NewBuilder().
		CmdName("test").
		CmdType("test").
		CmdLine(cmd).
		FirstSubscriber(process.Subscriber{
			Id:      "test",
			Mask:    process.DefaultMask,
			Channel: events,
		})

	go func() {
		statusReceived := false
		timeoutReached := false
		for !statusReceived && !timeoutReached {
			select {
			case event := <-events:
				if event.EventType == process.DiedEventType {
					statusReceived = true
				}
			case <-time.After(time.Second):
				timeoutReached = true
			}
		}
		done <- true
	}()

	p, err := pb.Start()
	if err != nil {
		t.Fatal(err)
	}

	// Wait until process is finished or timeout is reached
	if ok := <-done; !ok {
		t.Fatalf("Expected to receive %s process event", process.DiedEventType)
	}

	// Check process state after it is finished
	result, err := process.Get(p.Pid)
	if err != nil {
		t.Fatal(err)
	}
	return result
}