Exemple #1
0
func TestAddSubscriber(t *testing.T) {
	process.LogsDir = TmpFile()
	defer cleanupLogsDir()

	outputLines := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}

	// create and start a process
	pb := process.NewBuilder().
		CmdName("test").
		CmdType("test").
		CmdLine("printf \"" + strings.Join(outputLines, "\n") + "\"")

	// add a new subscriber
	eventsChan := make(chan *rpc.Event)
	pb.FirstSubscriber(process.Subscriber{
		Id:      "test",
		Mask:    process.DefaultMask,
		Channel: eventsChan,
	})

	// start a new process
	if _, err := pb.Start(); err != nil {
		t.Fatal(err)
	}

	// read all the process output events
	done := make(chan bool)
	var received []string
	go func() {
		event := <-eventsChan
		for event.EventType != process.DiedEventType {
			if event.EventType == process.StdoutEventType {
				out := event.Body.(*process.ProcessOutputEventBody)
				received = append(received, out.Text)
			}
			event = <-eventsChan
		}
		done <- true
	}()

	// wait until process is done
	<-done

	if len(outputLines) != len(received) {
		t.Fatalf("Expected the same size but got %d != %d", len(outputLines), len(received))
	}

	for idx, value := range outputLines {
		if value != received[idx] {
			t.Fatalf("Expected %s but got %s", value, received[idx])
		}
	}
}
Exemple #2
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
}