From: Clément Bénier <clement.benier@iot.bzh>
Date: Tue, 14 Aug 2018 14:44:47 +0000 (+0200)
Subject: test exec: waiting for events and timeout while building
X-Git-Tag: 5.99.5~2^2~13
X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=330d6849a51ea6b47d90e9cd6f6ee43a7c2fb10e;p=src%2Fxds%2Fxds-server.git

test exec: waiting for events and timeout while building

Change-Id: I4caf0dd3e59f920f602131638c58e11beb9f3e08
Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
---

diff --git a/test/exec_test.go b/test/exec_test.go
index abe23b5..5d4c106 100644
--- a/test/exec_test.go
+++ b/test/exec_test.go
@@ -28,6 +28,17 @@ import (
 	"github.com/stretchr/testify/assert"
 )
 
+/*flush channel with timeout*/
+func flushChannelExec(channel chan xsapiv1.ExecOutMsg, ms time.Duration) {
+	timeoutB := false
+	for !timeoutB {
+		select {
+		case <-channel:
+		case <-time.After(ms * time.Millisecond):
+			timeoutB = true
+		}
+	}
+}
 func TestExec(t *testing.T) {
 	cloneRepo := "https://github.com/iotbzh/helloworld-service.git"
 	cloneDir := path.Join(os.Getenv(envRootCfgDir), "testExec")
@@ -72,6 +83,12 @@ func TestExec(t *testing.T) {
 	assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
 	assert.NotNil(t, cfg)
 
+	chExec := make(chan xsapiv1.ExecOutMsg)
+	defer close(chExec)
+	sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
+		chExec <- ev
+	})
+
 	cmd := "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
 	cmd = cmd + " && "
 	cmd = cmd + "cd " + fPrj.ClientPath
@@ -88,7 +105,7 @@ func TestExec(t *testing.T) {
 	}
 	var execRes xsapiv1.ExecArgs
 	assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
-	time.Sleep(3 * time.Second) //maybe waiting for an event would be better
+	flushChannelExec(chExec, 1000)
 
 	cmd = "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
 	cmd = cmd + "&&"
@@ -99,7 +116,7 @@ func TestExec(t *testing.T) {
 	cmd = cmd + "make"
 	exec.Cmd = cmd
 	assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
-	time.Sleep(3 * time.Second) //maybe waiting for an event would be better
+	flushChannelExec(chExec, 1000)
 
 	/*check afb-helloworld.so exists*/
 	_, err = os.Stat(path.Join(fPrj.ClientPath, "build/helloworld-afb/afb-helloworld.so"))