Browse code

Replace waitAndAssert in config_test.go with poll.WaitOn

This fix replaces waitAndAssert in config_test.go with poll.WaitOn
so that the testing is consistent with all other tests in integration.

Also, config_test.go uses to wait and sleep for 2 * (1 minutes) to get the task
info. This fix combined those two sleep and wait for 1 mins. Think 1 min
is enough for config test.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2019/01/15 12:54:52
Showing 1 changed files
... ...
@@ -16,6 +16,7 @@ import (
16 16
 	"github.com/docker/docker/pkg/stdcopy"
17 17
 	"gotest.tools/assert"
18 18
 	is "gotest.tools/assert/cmp"
19
+	"gotest.tools/poll"
19 20
 	"gotest.tools/skip"
20 21
 )
21 22
 
... ...
@@ -268,18 +269,26 @@ func TestTemplatedConfig(t *testing.T) {
268 268
 	)
269 269
 
270 270
 	var tasks []swarmtypes.Task
271
-	waitAndAssert(t, 60*time.Second, func(t *testing.T) bool {
271
+	getRunningTasks := func(log poll.LogT) poll.Result {
272 272
 		tasks = swarm.GetRunningTasks(t, client, serviceID)
273
-		return len(tasks) > 0
274
-	})
273
+		if len(tasks) > 0 {
274
+			return poll.Success()
275
+		}
276
+		return poll.Continue("task still waiting")
277
+	}
278
+	poll.WaitOn(t, getRunningTasks, swarm.ServicePoll, poll.WithTimeout(1*time.Minute))
275 279
 
276 280
 	task := tasks[0]
277
-	waitAndAssert(t, 60*time.Second, func(t *testing.T) bool {
281
+	getTask := func(log poll.LogT) poll.Result {
278 282
 		if task.NodeID == "" || (task.Status.ContainerStatus == nil || task.Status.ContainerStatus.ContainerID == "") {
279 283
 			task, _, _ = client.TaskInspectWithRaw(context.Background(), task.ID)
280 284
 		}
281
-		return task.NodeID != "" && task.Status.ContainerStatus != nil && task.Status.ContainerStatus.ContainerID != ""
282
-	})
285
+		if task.NodeID != "" && task.Status.ContainerStatus != nil && task.Status.ContainerStatus.ContainerID != "" {
286
+			return poll.Success()
287
+		}
288
+		return poll.Continue("task still waiting")
289
+	}
290
+	poll.WaitOn(t, getTask, swarm.ServicePoll, poll.WithTimeout(1*time.Minute))
283 291
 
284 292
 	attach := swarm.ExecTask(t, d, task, types.ExecConfig{
285 293
 		Cmd:          []string{"/bin/cat", "/" + templatedConfigName},
... ...
@@ -307,22 +316,6 @@ func assertAttachedStream(t *testing.T, attach types.HijackedResponse, expect st
307 307
 	assert.Check(t, is.Contains(buf.String(), expect))
308 308
 }
309 309
 
310
-func waitAndAssert(t *testing.T, timeout time.Duration, f func(*testing.T) bool) {
311
-	t.Helper()
312
-	after := time.After(timeout)
313
-	for {
314
-		select {
315
-		case <-after:
316
-			t.Fatalf("timed out waiting for condition")
317
-		default:
318
-		}
319
-		if f(t) {
320
-			return
321
-		}
322
-		time.Sleep(100 * time.Millisecond)
323
-	}
324
-}
325
-
326 310
 func TestConfigInspect(t *testing.T) {
327 311
 	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
328 312