Skip to content

Commit efc49c6

Browse files
authored
fix: Prevent close on a nil channel (#49)
1 parent 4af8c72 commit efc49c6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ func (s *subscription) send(e eventOrComment) bool {
351351
//
352352
// This should be called only from the Server.run() goroutine.
353353
func (s *subscription) close() {
354+
if s.out == nil {
355+
return
356+
}
357+
354358
close(s.out)
355359
s.out = nil
356360
}

server_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"net/http"
77
"net/http/httptest"
8+
"sync"
89
"testing"
910
"time"
1011

@@ -53,6 +54,27 @@ func TestNewServerHandlerRespondsAfterClose(t *testing.T) {
5354
}
5455
}
5556

57+
func TestServerHandlesLoadsOfPendingTasks(t *testing.T) {
58+
channel := "test"
59+
server := NewServer()
60+
httpServer := httptest.NewServer(server.Handler(channel))
61+
defer httpServer.Close()
62+
63+
var wg sync.WaitGroup
64+
wg.Add(1)
65+
66+
go func() {
67+
_, _ = http.Get(httpServer.URL)
68+
wg.Wait()
69+
}()
70+
71+
server.Register(channel, &testServerRepository{})
72+
for i := 0; i < 1000; i++ {
73+
server.PublishComment([]string{channel}, "my comment")
74+
}
75+
wg.Done()
76+
}
77+
5678
func TestServerHandlerReceivesPublishedEvents(t *testing.T) {
5779
channel := "test"
5880
server := NewServer()

0 commit comments

Comments
 (0)