Skip to content

Commit

Permalink
Merge 6b28a2b into bd2ed36
Browse files Browse the repository at this point in the history
  • Loading branch information
igm committed Nov 18, 2019
2 parents bd2ed36 + 6b28a2b commit 3dc5492
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
31 changes: 22 additions & 9 deletions sockjs/eventsource_test.go
Expand Up @@ -3,6 +3,7 @@ package sockjs
import (
"net/http"
"net/http/httptest"
"runtime"
"testing"
"time"
)
Expand All @@ -13,14 +14,22 @@ func TestHandler_EventSource(t *testing.T) {
h := newTestHandler()
h.options.ResponseLimit = 1024
go func() {
time.Sleep(1 * time.Millisecond)
h.sessionsMux.Lock()
defer h.sessionsMux.Unlock()
sess := h.sessions["session"]
sess.Lock()
defer sess.Unlock()
recv := sess.recv
recv.close()
var sess *session
for exists := false; !exists; {
runtime.Gosched()
h.sessionsMux.Lock()
sess, exists = h.sessions["session"]
h.sessionsMux.Unlock()
}
for exists := false; !exists; {
runtime.Gosched()
sess.RLock()
exists = sess.recv != nil
sess.RUnlock()
}
sess.RLock()
sess.recv.close()
sess.RUnlock()
}()
h.eventSource(rw, req)
contentType := rw.Header().Get("content-type")
Expand Down Expand Up @@ -65,7 +74,11 @@ func TestHandler_EventSourceConnectionInterrupted(t *testing.T) {
rw := newClosableRecorder()
close(rw.closeNotifCh)
h.eventSource(rw, req)
time.Sleep(1 * time.Millisecond)
select {
case <-sess.closeCh:
case <-time.After(1 * time.Second):
t.Errorf("session close channel should be closed")
}
sess.Lock()
if sess.state != SessionClosed {
t.Errorf("Session should be closed")
Expand Down
11 changes: 11 additions & 0 deletions sockjs/handler_test.go
Expand Up @@ -30,6 +30,11 @@ func TestHandler_Create(t *testing.T) {
resp, err := http.Get(server.URL + "/echo")
if err != nil {
t.Errorf("There should not be any error, got '%s'", err)
t.FailNow()
}
if resp == nil {
t.Errorf("Response should not be nil")
t.FailNow()
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Unexpected status code receiver, got '%d' expected '%d'", resp.StatusCode, http.StatusOK)
Expand All @@ -47,7 +52,13 @@ func TestHandler_RootPrefixInfoHandler(t *testing.T) {
resp, err := http.Get(server.URL + "/info")
if err != nil {
t.Errorf("There should not be any error, got '%s'", err)
t.FailNow()
}
if resp == nil {
t.Errorf("Response should not be nil")
t.FailNow()
}

if resp.StatusCode != http.StatusOK {
t.Errorf("Unexpected status code receiver, got '%d' expected '%d'", resp.StatusCode, http.StatusOK)
}
Expand Down
7 changes: 6 additions & 1 deletion sockjs/httpreceiver_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"net/http/httptest"
"testing"
"time"
)

type testFrameWriter struct {
Expand Down Expand Up @@ -94,7 +95,11 @@ func TestHttpReceiver_ConnectionInterrupt(t *testing.T) {
rw := newClosableRecorder()
recv := newHTTPReceiver(rw, 1024, nil)
rw.closeNotifCh <- true
recv.Lock()
select {
case <-recv.interruptCh:
case <-time.After(1 * time.Second):
t.Errorf("should interrupt")
}
if recv.state != stateHTTPReceiverClosed {
t.Errorf("Unexpected state, got '%d', expected '%d'", recv.state, stateHTTPReceiverClosed)
}
Expand Down
7 changes: 6 additions & 1 deletion sockjs/session_test.go
Expand Up @@ -92,7 +92,12 @@ func TestSession_Timeout(t *testing.T) {
select {
case <-sess.closeCh:
case <-time.After(20 * time.Millisecond):
t.Errorf("sess close notification channel should close")
select {
case <-sess.closeCh:
// still ok
default:
t.Errorf("sess close notification channel should close")
}
}
if sess.GetSessionState() != SessionClosed {
t.Errorf("Session did not timeout")
Expand Down
29 changes: 26 additions & 3 deletions sockjs/websocket_test.go
Expand Up @@ -16,7 +16,15 @@ func TestHandler_WebSocketHandshakeError(t *testing.T) {
defer server.Close()
req, _ := http.NewRequest("GET", server.URL, nil)
req.Header.Set("origin", "https"+server.URL[4:])
resp, _ := http.DefaultClient.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Errorf("There should not be any error, got '%s'", err)
t.FailNow()
}
if resp == nil {
t.Errorf("Response should not be nil")
t.FailNow()
}
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("Unexpected response code, got '%d', expected '%d'", resp.StatusCode, http.StatusBadRequest)
}
Expand All @@ -30,11 +38,17 @@ func TestHandler_WebSocket(t *testing.T) {
var connCh = make(chan Session)
h.handlerFunc = func(conn Session) { connCh <- conn }
conn, resp, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
t.Errorf("Unexpected error '%v'", err)
t.FailNow()
}
if conn == nil {
t.Errorf("Connection should not be nil")
t.FailNow()
}
if err != nil {
t.Errorf("Unexpected error '%v'", err)
if resp == nil {
t.Errorf("Response should not be nil")
t.FailNow()
}
if resp.StatusCode != http.StatusSwitchingProtocols {
t.Errorf("Wrong response code returned, got '%d', expected '%d'", resp.StatusCode, http.StatusSwitchingProtocols)
Expand All @@ -58,6 +72,11 @@ func TestHandler_WebSocketTerminationByServer(t *testing.T) {
conn, _, err := websocket.DefaultDialer.Dial(url, map[string][]string{"Origin": []string{server.URL}})
if err != nil {
t.Fatalf("websocket dial failed: %v", err)
t.FailNow()
}
if conn == nil {
t.Errorf("Connection should not be nil")
t.FailNow()
}
_, msg, err := conn.ReadMessage()
if string(msg) != "o" || err != nil {
Expand Down Expand Up @@ -89,6 +108,10 @@ func TestHandler_WebSocketTerminationByClient(t *testing.T) {
close(done)
}
conn, _, _ := websocket.DefaultDialer.Dial(url, map[string][]string{"Origin": []string{server.URL}})
if conn == nil {
t.Errorf("Connection should not be nil")
t.FailNow()
}
conn.Close()
<-done
}
Expand Down

0 comments on commit 3dc5492

Please sign in to comment.