<commit_before>package executor

import (
	"os/exec"
	"strings"
	"syscall"
	"testing"
	"time"

	"github.com/intelsdi-x/swan/pkg/executor"
	. "github.com/smartystreets/goconvey/convey"
)

func TestParallel(t *testing.T) {
	SkipConvey("When using Parallel to decorate local executor", t, func() {
		parallel := executor.NewLocalIsolated(executor.NewParallel(5))
		Convey("Process should be executed 5 times", func() {
			task, err := parallel.Execute("sleep inf")

			So(err, ShouldBeNil)
			So(task, ShouldNotBeNil)
			// NOTE: We have to wait a bit for parallel to launch commands, though.
			isStopped := task.Wait(1000 * time.Millisecond)
			So(isStopped, ShouldBeFalse)

			defer task.Stop()
			defer task.Clean()
			defer task.EraseOutput()

			cmd := exec.Command("pgrep", "sleep")
			output, err := cmd.CombinedOutput()
			So(err, ShouldBeNil)

			pids := strings.Split(strings.TrimSpace(string(output)), "\n")
			So(len(pids), ShouldBeGreaterThan, 0)
			Convey("When I stop parallel process", func() {
				err = task.Stop()

				So(err, ShouldBeNil)
				Convey("All the child processes should be stopped", func() {
					isStopped := task.Wait(0)
					So(isStopped, ShouldBeTrue)
					cmd = exec.Command("pgrep", "sleep")
					err = cmd.Run()

					So(err, ShouldNotBeNil)
					So(cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), ShouldEqual, 1)
				})
			})
		})
	})
}<commit_msg>SCE-592: Integration test fails in TestParallel (#306)<commit_after>package executor

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"syscall"
	"testing"
	"time"

	"github.com/intelsdi-x/swan/pkg/executor"
	. "github.com/smartystreets/goconvey/convey"
)

func TestParallel(t *testing.T) {
	file, err := ioutil.TempFile(".", "parallel")
	if err != nil {
		t.Fail()
	}
	defer os.Remove(file.Name())

	Convey("When using Parallel to decorate local executor", t, func() {
		parallel := executor.NewLocalIsolated(executor.NewParallel(5))
		Convey("Process should be executed 5 times", func() {
			cmdStr := fmt.Sprintf("tailf %s", file.Name())
			task, err := parallel.Execute(cmdStr)
			defer task.EraseOutput()
			defer task.Clean()
			defer task.Stop()

			So(err, ShouldBeNil)
			So(task, ShouldNotBeNil)
			// NOTE: We have to wait a bit for parallel to launch commands, though.
			isStopped := task.Wait(1000 * time.Millisecond)
			So(isStopped, ShouldBeFalse)

			cmd := exec.Command("pgrep", "-f", cmdStr)
			output, err := cmd.CombinedOutput()
			So(err, ShouldBeNil)

			pids := strings.Split(strings.TrimSpace(string(output)), "\n")
			So(len(pids), ShouldBeGreaterThan, 0)
			Convey("When I stop parallel process", func() {
				err = task.Stop()

				So(err, ShouldBeNil)
				Convey("All the child processes should be stopped", func() {
					isStopped := task.Wait(0)
					So(isStopped, ShouldBeTrue)
					cmd = exec.Command("pgrep", "-f", cmdStr)
					err = cmd.Run()

					So(err, ShouldNotBeNil)
					So(cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), ShouldEqual, 1)
				})
			})
		})
	})
}
{'fraction_non_alphanumeric': 0.1527501656726309, 'fraction_numerical': 0.007952286282306162, 'mean_word_length': 3.784469096671949, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2359856', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1058, 'n_words': 242}