Skip to content

Commit

Permalink
event: fix re-adding listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
robbertkl committed Apr 22, 2016
1 parent ba4298e commit 93997c9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (c *Client) eventHijack(startTime int64, eventChan chan *APIEvents, errChan
var event APIEvents
if err = decoder.Decode(&event); err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
if c.eventMonitor.isEnabled() {
if c.eventMonitor.isEnabled() && c.eventMonitor.C == eventChan {
// Signal that we're exiting.
eventChan <- EOFEvent
}
Expand All @@ -321,7 +321,7 @@ func (c *Client) eventHijack(startTime int64, eventChan chan *APIEvents, errChan
if event.Time == 0 {
continue
}
if !c.eventMonitor.isEnabled() {
if !c.eventMonitor.isEnabled() || c.eventMonitor.C != eventChan {
return
}
transformEvent(&event)
Expand Down
33 changes: 33 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,36 @@ func testEventListeners(testName string, t *testing.T, buildServer func(http.Han
}
}
}

func TestEventListenerReAdding(t *testing.T) {
endChan := make(chan bool)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-endChan
}))

client, err := NewClient(server.URL)
if err != nil {
t.Errorf("Failed to create client: %s", err)
}

listener := make(chan *APIEvents, 10)
if err := client.AddEventListener(listener); err != nil {
t.Errorf("Failed to add event listener: %s", err)
}

// Make sure eventHijack() is started with the current eventMonitoringState.
time.Sleep(10 * time.Millisecond)

if err := client.RemoveEventListener(listener); err != nil {
t.Errorf("Failed to remove event listener: %s", err)
}

if err := client.AddEventListener(listener); err != nil {
t.Errorf("Failed to add event listener: %s", err)
}

endChan <- true

// Give the goroutine of the first eventHijack() time to handle the EOF.
time.Sleep(10 * time.Millisecond)
}

0 comments on commit 93997c9

Please sign in to comment.