Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GetSessionState method to sockjs.Session #31

Merged
merged 2 commits into from
Aug 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions sockjs/eventsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ func TestHandler_EventSourceMultipleConnections(t *testing.T) {
func TestHandler_EventSourceConnectionInterrupted(t *testing.T) {
h := newTestHandler()
sess := newTestSession()
sess.state = sessionActive
sess.state = SessionActive
h.sessions["session"] = sess
req, _ := http.NewRequest("POST", "/server/session/eventsource", nil)
rw := newClosableRecorder()
close(rw.closeNotifCh)
h.eventSource(rw, req)
time.Sleep(1 * time.Millisecond)
sess.Lock()
if sess.state != sessionClosed {
if sess.state != SessionClosed {
t.Errorf("Session should be closed")
}
}
28 changes: 15 additions & 13 deletions sockjs/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type sessionState uint32

const (
// brand new session, need to send "h" to receiver
sessionOpening sessionState = iota
SessionOpening sessionState = iota
// active session
sessionActive
SessionActive
// session being closed, sending "closeFrame" to receivers
sessionClosing
SessionClosing
// closed session, no activity at all, should be removed from handler completely and not reused
sessionClosed
SessionClosed
)

var (
Expand Down Expand Up @@ -92,7 +92,7 @@ func newSession(req *http.Request, sessionID string, sessionTimeoutInterval, hea
func (s *session) sendMessage(msg string) error {
s.Lock()
defer s.Unlock()
if s.state > sessionActive {
if s.state > SessionActive {
return ErrSessionNotOpen
}
s.sendBuffer = append(s.sendBuffer, msg)
Expand Down Expand Up @@ -120,14 +120,14 @@ func (s *session) attachReceiver(recv receiver) error {
}
}(recv)

if s.state == sessionClosing {
if s.state == SessionClosing {
s.recv.sendFrame(s.closeFrame)
s.recv.close()
return nil
}
if s.state == sessionOpening {
if s.state == SessionOpening {
s.recv.sendFrame("o")
s.state = sessionActive
s.state = SessionActive
}
s.recv.sendBulk(s.sendBuffer...)
s.sendBuffer = nil
Expand Down Expand Up @@ -166,10 +166,10 @@ func (s *session) accept(messages ...string) error {
func (s *session) closing() {
s.Lock()
defer s.Unlock()
if s.state < sessionClosing {
if s.state < SessionClosing {
s.msgReader.Close()
s.msgWriter.Close()
s.state = sessionClosing
s.state = SessionClosing
if s.recv != nil {
s.recv.sendFrame(s.closeFrame)
s.recv.close()
Expand All @@ -182,8 +182,8 @@ func (s *session) close() {
s.closing()
s.Lock()
defer s.Unlock()
if s.state < sessionClosed {
s.state = sessionClosed
if s.state < SessionClosed {
s.state = SessionClosed
s.timer.Stop()
close(s.closeCh)
}
Expand All @@ -194,7 +194,7 @@ func (s *session) closedNotify() <-chan struct{} { return s.closeCh }
// Conn interface implementation
func (s *session) Close(status uint32, reason string) error {
s.Lock()
if s.state < sessionClosing {
if s.state < SessionClosing {
s.closeFrame = closeFrame(status, reason)
s.Unlock()
s.closing()
Expand All @@ -219,6 +219,8 @@ func (s *session) Send(msg string) error {

func (s *session) ID() string { return s.id }

func (s *session) GetSessionState() sessionState { return s.state }

func (s *session) Request() *http.Request {
return s.req
}
20 changes: 10 additions & 10 deletions sockjs/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package sockjs

import (
"io"
"net/http"
"runtime"
"strings"
"sync"
"testing"
"time"
"net/http"
"strings"
)

func newTestSession() *session {
Expand All @@ -25,8 +25,8 @@ func TestSession_Create(t *testing.T) {
if len(session.sendBuffer) != 2 {
t.Errorf("Session send buffer should contain 2 messages")
}
if session.state != sessionOpening {
t.Errorf("Session in wrong state %v, should be %v", session.state, sessionOpening)
if session.GetSessionState() != SessionOpening {
t.Errorf("Session in wrong state %v, should be %v", session.GetSessionState(), SessionOpening)
}
}

Expand Down Expand Up @@ -73,8 +73,8 @@ func TestSession_AttachReceiver(t *testing.T) {
if err := session.attachReceiver(recv); err != nil {
t.Errorf("Should not return error")
}
if session.state != sessionActive {
t.Errorf("Session in wrong state after receiver attached %d, should be %d", session.state, sessionActive)
if session.GetSessionState() != SessionActive {
t.Errorf("Session in wrong state after receiver attached %d, should be %d", session.GetSessionState(), SessionActive)
}
session.detachReceiver()
// recv = &mockRecv{
Expand All @@ -96,7 +96,7 @@ func TestSession_Timeout(t *testing.T) {
t.Errorf("sess close notification channel should close")
}
sess.Lock()
if sess.state != sessionClosed {
if sess.GetSessionState() != SessionClosed {
t.Errorf("Session did not timeout")
}
sess.Unlock()
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestSession_SessionSend(t *testing.T) {

func TestSession_SessionClose(t *testing.T) {
s := newTestSession()
s.state = sessionActive
s.state = SessionActive
recv := newTestReceiver()
s.attachReceiver(recv)
err := s.Close(1, "some reason")
Expand All @@ -275,8 +275,8 @@ func TestSession_SessionClose(t *testing.T) {
if s.closeFrame != "c[1,\"some reason\"]" {
t.Errorf("Incorrect closeFrame, got '%s'", s.closeFrame)
}
if s.state != sessionClosing {
t.Errorf("Incorrect session state, expected 'sessionClosing', got '%v'", s.state)
if s.GetSessionState() != SessionClosing {
t.Errorf("Incorrect session state, expected 'sessionClosing', got '%v'", s.GetSessionState())
}
// all the consequent receivers trying to attach shoult get the same close frame
var i = 100
Expand Down
2 changes: 2 additions & 0 deletions sockjs/sockjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ type Session interface {
Send(string) error
// Close closes the session with provided code and reason.
Close(status uint32, reason string) error
//Gets the state of the session. SessionOpening/SessionActive/SessionClosing/SessionClosed;
GetSessionState() sessionState
}
4 changes: 2 additions & 2 deletions sockjs/xhr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ func TestHandler_XhrPoll(t *testing.T) {
func TestHandler_XhrPollConnectionInterrupted(t *testing.T) {
h := newTestHandler()
sess := newTestSession()
sess.state = sessionActive
sess.state = SessionActive
h.sessions["session"] = sess
req, _ := http.NewRequest("POST", "/server/session/xhr", nil)
rw := newClosableRecorder()
close(rw.closeNotifCh)
h.xhrPoll(rw, req)
time.Sleep(1 * time.Millisecond)
sess.Lock()
if sess.state != sessionClosed {
if sess.state != SessionClosed {
t.Errorf("Session should be closed")
}
}
Expand Down