Explorar o código

Merge pull request #2848 from pnasrat/713-data-races

Fix data race in TestLogEvent
Victor Vieux %!s(int64=11) %!d(string=hai) anos
pai
achega
d9e54e28e7
Modificáronse 3 ficheiros con 23 adicións e 6 borrados
  1. 1 1
      api.go
  2. 18 2
      server.go
  3. 4 3
      server_unit_test.go

+ 1 - 1
api.go

@@ -254,7 +254,7 @@ func getEvents(srv *Server, version float64, w http.ResponseWriter, r *http.Requ
 	wf.Flush()
 	wf.Flush()
 	if since != 0 {
 	if since != 0 {
 		// If since, send previous events that happened after the timestamp
 		// If since, send previous events that happened after the timestamp
-		for _, event := range srv.events {
+		for _, event := range srv.GetEvents() {
 			if event.Time >= since {
 			if event.Time >= since {
 				err := sendEvent(wf, &event)
 				err := sendEvent(wf, &event)
 				if err != nil && err.Error() == "JSON error" {
 				if err != nil && err.Error() == "JSON error" {

+ 18 - 2
server.go

@@ -959,6 +959,8 @@ func (srv *Server) poolAdd(kind, key string) error {
 }
 }
 
 
 func (srv *Server) poolRemove(kind, key string) error {
 func (srv *Server) poolRemove(kind, key string) error {
+	srv.Lock()
+	defer srv.Unlock()
 	switch kind {
 	switch kind {
 	case "pull":
 	case "pull":
 		delete(srv.pullingPool, key)
 		delete(srv.pullingPool, key)
@@ -1829,6 +1831,8 @@ func NewServer(eng *engine.Engine, config *DaemonConfig) (*Server, error) {
 }
 }
 
 
 func (srv *Server) HTTPRequestFactory(metaHeaders map[string][]string) *utils.HTTPRequestFactory {
 func (srv *Server) HTTPRequestFactory(metaHeaders map[string][]string) *utils.HTTPRequestFactory {
+	srv.Lock()
+	defer srv.Unlock()
 	if srv.reqFactory == nil {
 	if srv.reqFactory == nil {
 		ud := utils.NewHTTPUserAgentDecorator(srv.versionInfos()...)
 		ud := utils.NewHTTPUserAgentDecorator(srv.versionInfos()...)
 		md := &utils.HTTPMetaHeadersDecorator{
 		md := &utils.HTTPMetaHeadersDecorator{
@@ -1843,7 +1847,7 @@ func (srv *Server) HTTPRequestFactory(metaHeaders map[string][]string) *utils.HT
 func (srv *Server) LogEvent(action, id, from string) *utils.JSONMessage {
 func (srv *Server) LogEvent(action, id, from string) *utils.JSONMessage {
 	now := time.Now().Unix()
 	now := time.Now().Unix()
 	jm := utils.JSONMessage{Status: action, ID: id, From: from, Time: now}
 	jm := utils.JSONMessage{Status: action, ID: id, From: from, Time: now}
-	srv.events = append(srv.events, jm)
+	srv.AddEvent(jm)
 	for _, c := range srv.listeners {
 	for _, c := range srv.listeners {
 		select { // non blocking channel
 		select { // non blocking channel
 		case c <- jm:
 		case c <- jm:
@@ -1853,8 +1857,20 @@ func (srv *Server) LogEvent(action, id, from string) *utils.JSONMessage {
 	return &jm
 	return &jm
 }
 }
 
 
+func (srv *Server) AddEvent(jm utils.JSONMessage) {
+	srv.Lock()
+	defer srv.Unlock()
+	srv.events = append(srv.events, jm)
+}
+
+func (srv *Server) GetEvents() []utils.JSONMessage {
+	srv.RLock()
+	defer srv.RUnlock()
+	return srv.events
+}
+
 type Server struct {
 type Server struct {
-	sync.Mutex
+	sync.RWMutex
 	runtime     *Runtime
 	runtime     *Runtime
 	pullingPool map[string]struct{}
 	pullingPool map[string]struct{}
 	pushingPool map[string]struct{}
 	pushingPool map[string]struct{}

+ 4 - 3
server_unit_test.go

@@ -70,8 +70,9 @@ func TestLogEvent(t *testing.T) {
 
 
 	srv.LogEvent("fakeaction2", "fakeid", "fakeimage")
 	srv.LogEvent("fakeaction2", "fakeid", "fakeimage")
 
 
-	if len(srv.events) != 2 {
-		t.Fatalf("Expected 2 events, found %d", len(srv.events))
+	numEvents := len(srv.GetEvents())
+	if numEvents != 2 {
+		t.Fatalf("Expected 2 events, found %d", numEvents)
 	}
 	}
 	go func() {
 	go func() {
 		time.Sleep(200 * time.Millisecond)
 		time.Sleep(200 * time.Millisecond)
@@ -83,7 +84,7 @@ func TestLogEvent(t *testing.T) {
 	setTimeout(t, "Listening for events timed out", 2*time.Second, func() {
 	setTimeout(t, "Listening for events timed out", 2*time.Second, func() {
 		for i := 2; i < 4; i++ {
 		for i := 2; i < 4; i++ {
 			event := <-listener
 			event := <-listener
-			if event != srv.events[i] {
+			if event != srv.GetEvents()[i] {
 				t.Fatalf("Event received it different than expected")
 				t.Fatalf("Event received it different than expected")
 			}
 			}
 		}
 		}