github.com/projectatomic/docker@v1.8.2/daemon/events/events_test.go (about)

     1  package events
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/docker/pkg/jsonmessage"
     9  )
    10  
    11  func TestEventsLog(t *testing.T) {
    12  	e := New()
    13  	_, l1 := e.Subscribe()
    14  	_, l2 := e.Subscribe()
    15  	defer e.Evict(l1)
    16  	defer e.Evict(l2)
    17  	count := e.SubscribersCount()
    18  	if count != 2 {
    19  		t.Fatalf("Must be 2 subscribers, got %d", count)
    20  	}
    21  	e.Log("test", "cont", "image")
    22  	select {
    23  	case msg := <-l1:
    24  		jmsg, ok := msg.(*jsonmessage.JSONMessage)
    25  		if !ok {
    26  			t.Fatalf("Unexpected type %T", msg)
    27  		}
    28  		if len(e.events) != 1 {
    29  			t.Fatalf("Must be only one event, got %d", len(e.events))
    30  		}
    31  		if jmsg.Status != "test" {
    32  			t.Fatalf("Status should be test, got %s", jmsg.Status)
    33  		}
    34  		if jmsg.ID != "cont" {
    35  			t.Fatalf("ID should be cont, got %s", jmsg.ID)
    36  		}
    37  		if jmsg.From != "image" {
    38  			t.Fatalf("From should be image, got %s", jmsg.From)
    39  		}
    40  	case <-time.After(1 * time.Second):
    41  		t.Fatal("Timeout waiting for broadcasted message")
    42  	}
    43  	select {
    44  	case msg := <-l2:
    45  		jmsg, ok := msg.(*jsonmessage.JSONMessage)
    46  		if !ok {
    47  			t.Fatalf("Unexpected type %T", msg)
    48  		}
    49  		if len(e.events) != 1 {
    50  			t.Fatalf("Must be only one event, got %d", len(e.events))
    51  		}
    52  		if jmsg.Status != "test" {
    53  			t.Fatalf("Status should be test, got %s", jmsg.Status)
    54  		}
    55  		if jmsg.ID != "cont" {
    56  			t.Fatalf("ID should be cont, got %s", jmsg.ID)
    57  		}
    58  		if jmsg.From != "image" {
    59  			t.Fatalf("From should be image, got %s", jmsg.From)
    60  		}
    61  	case <-time.After(1 * time.Second):
    62  		t.Fatal("Timeout waiting for broadcasted message")
    63  	}
    64  }
    65  
    66  func TestEventsLogTimeout(t *testing.T) {
    67  	e := New()
    68  	_, l := e.Subscribe()
    69  	defer e.Evict(l)
    70  
    71  	c := make(chan struct{})
    72  	go func() {
    73  		e.Log("test", "cont", "image")
    74  		close(c)
    75  	}()
    76  
    77  	select {
    78  	case <-c:
    79  	case <-time.After(time.Second):
    80  		t.Fatal("Timeout publishing message")
    81  	}
    82  }
    83  
    84  func TestLogEvents(t *testing.T) {
    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)
    92  	}
    93  	time.Sleep(50 * time.Millisecond)
    94  	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)
   101  	}
   102  	if len(e.events) != eventsLimit {
   103  		t.Fatalf("Must be %d events, got %d", eventsLimit, len(e.events))
   104  	}
   105  
   106  	var msgs []*jsonmessage.JSONMessage
   107  	for len(msgs) < 10 {
   108  		m := <-l
   109  		jm, ok := (m).(*jsonmessage.JSONMessage)
   110  		if !ok {
   111  			t.Fatalf("Unexpected type %T", m)
   112  		}
   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  	}
   135  }