Browse code

Make events test more deterministic in go1.5

Now scheduler makes order of events pretty random, so I added little
sleeps to make order intact. Also I renamed to test so name better
describes its nature.

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2015/08/20 03:51:14
Showing 1 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 package events
2 2
 
3 3
 import (
4
-	"fmt"
5 4
 	"testing"
6 5
 	"time"
7 6
 
... ...
@@ -81,55 +80,39 @@ func TestEventsLogTimeout(t *testing.T) {
81 81
 	}
82 82
 }
83 83
 
84
-func TestLogEvents(t *testing.T) {
84
+func TestEventsCap(t *testing.T) {
85 85
 	e := New()
86
-
87
-	for i := 0; i < eventsLimit+16; i++ {
88
-		action := fmt.Sprintf("action_%d", i)
89
-		id := fmt.Sprintf("cont_%d", i)
90
-		from := fmt.Sprintf("image_%d", i)
91
-		e.Log(action, id, from)
86
+	for i := 0; i < eventsLimit+1; i++ {
87
+		e.Log("action", "id", "from")
92 88
 	}
93
-	time.Sleep(50 * time.Millisecond)
89
+	// let all events go through
90
+	time.Sleep(1 * time.Second)
91
+
94 92
 	current, l := e.Subscribe()
95
-	for i := 0; i < 10; i++ {
96
-		num := i + eventsLimit + 16
97
-		action := fmt.Sprintf("action_%d", num)
98
-		id := fmt.Sprintf("cont_%d", num)
99
-		from := fmt.Sprintf("image_%d", num)
100
-		e.Log(action, id, from)
93
+	if len(current) != eventsLimit {
94
+		t.Fatalf("Must be %d events, got %d", eventsLimit, len(current))
101 95
 	}
102 96
 	if len(e.events) != eventsLimit {
103 97
 		t.Fatalf("Must be %d events, got %d", eventsLimit, len(e.events))
104 98
 	}
105 99
 
100
+	for i := 0; i < 10; i++ {
101
+		e.Log("action", "id", "from")
102
+	}
103
+	// let all events go through
104
+	time.Sleep(1 * time.Second)
105
+
106 106
 	var msgs []*jsonmessage.JSONMessage
107 107
 	for len(msgs) < 10 {
108
-		m := <-l
109
-		jm, ok := (m).(*jsonmessage.JSONMessage)
110
-		if !ok {
111
-			t.Fatalf("Unexpected type %T", m)
108
+		select {
109
+		case m := <-l:
110
+			jm, ok := (m).(*jsonmessage.JSONMessage)
111
+			if !ok {
112
+				t.Fatalf("Unexpected type %T", m)
113
+			}
114
+			msgs = append(msgs, jm)
115
+		default:
116
+			t.Fatalf("There is no enough events in channel")
112 117
 		}
113
-		msgs = append(msgs, jm)
114
-	}
115
-	if len(current) != eventsLimit {
116
-		t.Fatalf("Must be %d events, got %d", eventsLimit, len(current))
117
-	}
118
-	first := current[0]
119
-	if first.Status != "action_16" {
120
-		t.Fatalf("First action is %s, must be action_16", first.Status)
121
-	}
122
-	last := current[len(current)-1]
123
-	if last.Status != "action_79" {
124
-		t.Fatalf("Last action is %s, must be action_79", last.Status)
125
-	}
126
-
127
-	firstC := msgs[0]
128
-	if firstC.Status != "action_80" {
129
-		t.Fatalf("First action is %s, must be action_80", firstC.Status)
130
-	}
131
-	lastC := msgs[len(msgs)-1]
132
-	if lastC.Status != "action_89" {
133
-		t.Fatalf("Last action is %s, must be action_89", lastC.Status)
134 118
 	}
135 119
 }