mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
use non-blocking channel to prevent dead-lock and add test for server
This commit is contained in:
parent
8b3519c5f7
commit
040c3b50d0
2 changed files with 44 additions and 1 deletions
|
@ -1185,7 +1185,10 @@ func (srv *Server) LogEvent(action, id string) {
|
||||||
jm := utils.JSONMessage{Status: action, ID: id, Time: now}
|
jm := utils.JSONMessage{Status: action, ID: id, Time: now}
|
||||||
srv.events = append(srv.events, jm)
|
srv.events = append(srv.events, jm)
|
||||||
for _, c := range srv.listeners {
|
for _, c := range srv.listeners {
|
||||||
c <- jm
|
select { // non blocking channel
|
||||||
|
case c <- jm:
|
||||||
|
default:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package docker
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/dotcloud/docker/utils"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestContainerTagImageDelete(t *testing.T) {
|
func TestContainerTagImageDelete(t *testing.T) {
|
||||||
|
@ -163,3 +165,41 @@ func TestRunWithTooLowMemoryLimit(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLogEvent(t *testing.T) {
|
||||||
|
runtime := mkRuntime(t)
|
||||||
|
srv := &Server{
|
||||||
|
runtime: runtime,
|
||||||
|
events: make([]utils.JSONMessage, 0, 64),
|
||||||
|
listeners: make(map[string]chan utils.JSONMessage),
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.LogEvent("fakeaction", "fakeid")
|
||||||
|
|
||||||
|
listener := make(chan utils.JSONMessage)
|
||||||
|
srv.Lock()
|
||||||
|
srv.listeners["test"] = listener
|
||||||
|
srv.Unlock()
|
||||||
|
|
||||||
|
srv.LogEvent("fakeaction2", "fakeid")
|
||||||
|
|
||||||
|
if len(srv.events) != 2 {
|
||||||
|
t.Fatalf("Expected 2 events, found %d", len(srv.events))
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
srv.LogEvent("fakeaction3", "fakeid")
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
srv.LogEvent("fakeaction4", "fakeid")
|
||||||
|
}()
|
||||||
|
|
||||||
|
setTimeout(t, "Listening for events timed out", 2*time.Second, func() {
|
||||||
|
for i := 2; i < 4; i++ {
|
||||||
|
event := <-listener
|
||||||
|
if event != srv.events[i] {
|
||||||
|
t.Fatalf("Event received it different than expected")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue