diff --git a/sse/sse.go b/sse/sse.go index 35ea4e7..6765a9d 100644 --- a/sse/sse.go +++ b/sse/sse.go @@ -91,26 +91,26 @@ func (sse *SseBroker) HasClients() bool { return len(sse.clients) > 0 } -func (broker *SseBroker) listen() { +func (sse *SseBroker) listen() { for { select { - case s := <-broker.newClients: + case s := <-sse.newClients: { - broker.lock.Lock() - broker.clients[s] = true - broker.lock.Unlock() - log.Printf("Client added. %d registered clients", len(broker.clients)) + sse.lock.Lock() + sse.clients[s] = true + sse.lock.Unlock() + log.Printf("Client added. %d registered clients", len(sse.clients)) } - case s := <-broker.closingClients: + case s := <-sse.closingClients: { - broker.lock.Lock() - delete(broker.clients, s) - broker.lock.Unlock() - log.Printf("Removed client. %d registered clients", len(broker.clients)) + sse.lock.Lock() + delete(sse.clients, s) + sse.lock.Unlock() + log.Printf("Removed client. %d registered clients", len(sse.clients)) } - case event := <-broker.notifier: + case event := <-sse.notifier: { - for clientMessageChan := range broker.clients { + for clientMessageChan := range sse.clients { select { case clientMessageChan <- event: case <-time.After(patience): diff --git a/sse/tick.go b/sse/tick.go index f4cd959..917a0ff 100644 --- a/sse/tick.go +++ b/sse/tick.go @@ -1,9 +1,9 @@ package sse import ( + "context" "os" "time" - "context" ) func Tick(broker Broker, notify func(context.Context, Broker), period time.Duration, stop chan os.Signal) { @@ -21,7 +21,6 @@ func Tick(broker Broker, notify func(context.Context, Broker), period time.Durat { cancel() ticker.Stop() - os.Exit(0) } } } diff --git a/sse/tick_test.go b/sse/tick_test.go index 5f90343..97d5d2c 100644 --- a/sse/tick_test.go +++ b/sse/tick_test.go @@ -1,15 +1,15 @@ package sse import ( + "context" + "encoding/json" + . "github.com/aandryashin/matchers" + "io" "net/http" "net/http/httptest" "os" "testing" "time" - . "github.com/aandryashin/matchers" - "encoding/json" - "context" - "io/ioutil" ) type MockBroker struct { @@ -24,7 +24,7 @@ func (mb *MockBroker) Notify(data []byte) { mb.messages <- string(data) } -func (mb *MockBroker) ServeHTTP(rw http.ResponseWriter, req *http.Request) { +func (mb *MockBroker) ServeHTTP(rw http.ResponseWriter, _ *http.Request) { rw.WriteHeader(http.StatusOK) } @@ -33,9 +33,9 @@ func TestTick(t *testing.T) { broker := &MockBroker{messages: make(chan string, 10)} stop := make(chan os.Signal) go Tick(broker, func(ctx context.Context, br Broker) { - req, _ := http.NewRequest("GET", srv.URL + "/status", nil) + req, _ := http.NewRequest("GET", srv.URL+"/status", nil) resp, _ := http.DefaultClient.Do(req) - data, _ := ioutil.ReadAll(resp.Body) + data, _ := io.ReadAll(resp.Body) br.Notify(data) }, 10*time.Millisecond, stop) time.Sleep(50 * time.Millisecond)