Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tld committed Jul 31, 2024
1 parent a0ff8ce commit 0ff3c95
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ coverage:
status:
project:
default:
target: 40%
target: 60%
if_not_found: success
2 changes: 1 addition & 1 deletion eventcenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (center *EventCenter) HandleWebSocket(w http.ResponseWriter, r *http.Reques

msg := &Message{}
eventCh := make(chan eventbus.Event)
handleFinished := make(chan bool)
handleFinished := make(chan bool, 1)
for {
err := conn.ReadJSON(msg)
if err != nil {
Expand Down
82 changes: 72 additions & 10 deletions eventcenter_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package eventcenter

import (
"fmt"
"sync"
"testing"

"net/http"
"net/http/httptest"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/gorilla/websocket"
. "github.com/smartystreets/goconvey/convey"
"github.com/tianluoding/eventcenter/eventbus"
)
Expand All @@ -17,21 +18,82 @@ func TestEventCenter(t *testing.T) {
center := NewEventCenter(eb)

Convey("When publishing an event", func() {
patches := gomonkey.ApplyFunc(eb.Publish, func(event eventbus.Event) {})
var called bool
patches := gomonkey.ApplyMethod(eb, "Publish", func(eb *eventbus.EventBus, event eventbus.Event) {
called = true
})
defer patches.Reset()

center.Publish(eventbus.Event{})
So(called, ShouldBeTrue)
})

Convey("When handling WebSocket connections", func() {
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
Convey("When listening and writing to WebSocket", func() {
fakeConn := &websocket.Conn{}

patches := gomonkey.ApplyFunc(eb.Subscribe, func(id string, name string, ch chan eventbus.Event) {})
patches.ApplyFunc(eb.Unsubscribe, func(id string, name string) {})
eventCh := make(chan eventbus.Event)
handleFinished := make(chan bool)
msg := &Message{ID: "testID", MsgType: "subscription"}
patches := gomonkey.ApplyMethodReturn(fakeConn, "WriteJSON", nil)
defer patches.Reset()

center.HandleWebSocket(w, r)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
center.listenAndWrite(eventCh, fakeConn, msg, handleFinished)
}()

// Simulate receiving an event
eventCh <- eventbus.Event{}
time.Sleep(10 * time.Millisecond) // Allow time for the goroutine to process the event

close(eventCh)
wg.Wait()
})

Convey("When listening and writing to WebSocket, handleFinished", func() {
fakeConn := &websocket.Conn{}

eventCh := make(chan eventbus.Event)
handleFinished := make(chan bool)
msg := &Message{ID: "testID", MsgType: "subscription"}
patches := gomonkey.ApplyMethodReturn(fakeConn, "WriteJSON", nil)
defer patches.Reset()

wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
center.listenAndWrite(eventCh, fakeConn, msg, handleFinished)
}()

// Simulate receiving an event
eventCh <- eventbus.Event{}
time.Sleep(10 * time.Millisecond) // Allow time for the goroutine to process the event

handleFinished <- true
wg.Wait()
})

Convey("When listening and writing to WebSocket, ReadJSON err", func() {
fakeConn := &websocket.Conn{}

patches := gomonkey.
ApplyMethodReturn(&upgrader, "Upgrade", fakeConn, nil).
ApplyMethodReturn(fakeConn, "Close", nil).
ApplyMethodReturn(fakeConn, "ReadJSON", fmt.Errorf("read error"))

defer patches.Reset()

wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
center.HandleWebSocket(nil, nil)
}()

wg.Wait()
})
})
}

0 comments on commit 0ff3c95

Please sign in to comment.