Skip to content

Commit

Permalink
Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Feb 8, 2023
1 parent 786e81f commit cb2034b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
26 changes: 13 additions & 13 deletions sse/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions sse/tick.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -21,7 +21,6 @@ func Tick(broker Broker, notify func(context.Context, Broker), period time.Durat
{
cancel()
ticker.Stop()
os.Exit(0)
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions sse/tick_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}

Expand All @@ -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)
Expand Down

0 comments on commit cb2034b

Please sign in to comment.