github.com/vlifesystems/rulehunter@v0.0.0-20180501090014-673078aa4a83/cmd/serve_unix_test.go (about)

     1  // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
     2  
     3  package cmd
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/vlifesystems/rulehunter/internal/testhelpers"
    12  	"github.com/vlifesystems/rulehunter/quitter"
    13  )
    14  
    15  func TestRunServe_interrupt(t *testing.T) {
    16  	cfgDir := testhelpers.BuildConfigDirs(t, false)
    17  	cfgFilename := filepath.Join(cfgDir, "config.yaml")
    18  	defer os.RemoveAll(cfgDir)
    19  	if testing.Short() {
    20  		testhelpers.MustWriteConfig(t, cfgDir, 3000)
    21  	} else {
    22  		testhelpers.MustWriteConfig(t, cfgDir, 20000)
    23  	}
    24  	l := testhelpers.NewLogger()
    25  	q := quitter.New()
    26  	defer q.Quit()
    27  
    28  	go func() {
    29  		if err := runServe(l, q, cfgFilename); err != nil {
    30  			t.Errorf("runServe: %s", err)
    31  		}
    32  	}()
    33  
    34  	if !testing.Short() {
    35  		time.Sleep(2 * time.Second)
    36  	}
    37  
    38  	experimentFiles := []string{
    39  		"debt.json",
    40  		"debt.yaml",
    41  		"0debt_broken.yaml",
    42  		"debt2.json",
    43  		"debt.jso",
    44  	}
    45  	for _, f := range experimentFiles {
    46  		testhelpers.CopyFile(
    47  			t,
    48  			filepath.Join("fixtures", f),
    49  			filepath.Join(cfgDir, "experiments"),
    50  		)
    51  	}
    52  
    53  	hasInterrupted := false
    54  	tickerC := time.NewTicker(400 * time.Millisecond).C
    55  	timeoutC := time.NewTimer(20 * time.Second).C
    56  	for !hasInterrupted {
    57  		select {
    58  		case <-tickerC:
    59  			gotReportFiles := testhelpers.GetFilesInDir(
    60  				t,
    61  				filepath.Join(cfgDir, "build", "reports"),
    62  			)
    63  			if len(gotReportFiles) >= 1 {
    64  				interruptProcess(t)
    65  				hasInterrupted = true
    66  				break
    67  			}
    68  		case <-timeoutC:
    69  			t.Fatal("runServe hasn't stopped")
    70  		}
    71  	}
    72  
    73  	time.Sleep(4 * time.Second)
    74  
    75  	gotReportFiles := testhelpers.GetFilesInDir(
    76  		t,
    77  		filepath.Join(cfgDir, "build", "reports"),
    78  	)
    79  	if len(gotReportFiles) >= 3 {
    80  		t.Errorf("runServe - gotReportFiles: %v, want: len(gotReportFiles) < 3",
    81  			gotReportFiles)
    82  	}
    83  }