Skip to content

Commit

Permalink
Improve test feedback on event test
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed Jul 26, 2017
1 parent 57f271a commit 78dd980
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func TestEventListeners(t *testing.T) {
t.Parallel()
testEventListeners("TestEventListeners", t, httptest.NewServer, NewClient)
}

func TestTLSEventListeners(t *testing.T) {
t.Parallel()
testEventListeners("TestTLSEventListeners", t, func(handler http.Handler) *httptest.Server {
server := httptest.NewUnstartedServer(handler)

Expand Down Expand Up @@ -51,7 +54,6 @@ func TestTLSEventListeners(t *testing.T) {
}

func testEventListeners(testName string, t *testing.T, buildServer func(http.Handler) *httptest.Server, buildClient func(string) (*Client, error)) {
t.Parallel()
response := `{"action":"pull","type":"image","actor":{"id":"busybox:latest","attributes":{}},"time":1442421700,"timeNano":1442421700598988358}
{"action":"create","type":"container","actor":{"id":"5745704abe9caa5","attributes":{"image":"busybox"}},"time":1442421716,"timeNano":1442421716853979870}
{"action":"attach","type":"container","actor":{"id":"5745704abe9caa5","attributes":{"image":"busybox"}},"time":1442421716,"timeNano":1442421716894759198}
Expand All @@ -62,7 +64,17 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han
{"status":"destroy","id":"dfdf82bd3881","from":"base:latest","time":1374067970}
{"Action":"create","Actor":{"Attributes":{"HAProxyMode":"http","HealthCheck":"HttpGet","HealthCheckArgs":"http://127.0.0.1:39051/status/check","ServicePort_8080":"17801","image":"datanerd.us/siteeng/sample-app-go:latest","name":"sample-app-client-go-69818c1223ddb5"},"ID":"a925eaf4084d5c3bcf337b2abb05f566ebb94276dff34f6effb00d8ecd380e16"},"Type":"container","from":"datanerd.us/siteeng/sample-app-go:latest","id":"a925eaf4084d5c3bcf337b2abb05f566ebb94276dff34f6effb00d8ecd380e16","status":"create","time":1459133932,"timeNano":1459133932961735842}`

wantResponse := []*APIEvents{
server := buildServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rsc := bufio.NewScanner(strings.NewReader(response))
for rsc.Scan() {
w.Write(rsc.Bytes())
w.(http.Flusher).Flush()
time.Sleep(10 * time.Millisecond)
}
}))
defer server.Close()

wantedEvents := []APIEvents{
{
Action: "pull",
Type: "image",
Expand Down Expand Up @@ -214,15 +226,6 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han
},
},
}
server := buildServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rsc := bufio.NewScanner(strings.NewReader(response))
for rsc.Scan() {
w.Write([]byte(rsc.Text()))
w.(http.Flusher).Flush()
time.Sleep(10 * time.Millisecond)
}
}))
defer server.Close()

client, err := buildClient(server.URL)
if err != nil {
Expand All @@ -232,7 +235,6 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han

listener := make(chan *APIEvents, 10)
defer func() {
time.Sleep(10 * time.Millisecond)
if err = client.RemoveEventListener(listener); err != nil {
t.Error(err)
}
Expand All @@ -244,18 +246,24 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han
}

timeout := time.After(1 * time.Second)
events := make([]APIEvents, 0, len(wantedEvents))

for i := 0; i < 9; i++ {
for range wantedEvents {
select {
case msg := <-listener:
t.Logf("%d: Received: %v", i, msg)
if !reflect.DeepEqual(msg, wantResponse[i]) {
t.Fatalf("%d: wanted: %#v\n got: %#v", i, wantResponse[i], msg)
case msg, ok := <-listener:
if ok {
events = append(events, *msg)
}
case <-timeout:
t.Fatalf("%s timed out waiting on events", testName)
}
}
cmpr := cmp.Comparer(func(e1, e2 APIEvents) bool {
return e1.Action == e2.Action && e1.Actor.ID == e2.Actor.ID
})
if dff := cmp.Diff(events, wantedEvents, cmpr); dff != "" {
t.Errorf("wrong events:\n%s", dff)
}
}

func TestEventListenerReAdding(t *testing.T) {
Expand Down

0 comments on commit 78dd980

Please sign in to comment.