Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng committed Feb 3, 2017
2 parents 3886b1f + 70b17d2 commit edc845f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
10 changes: 10 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,13 @@ func (s *Session) String(key string) string {
func (s *Session) Value(key string) interface{} {
return s.data[key]
}

// Retrieve all session state
func (s *Session) State() map[string]interface{} {
return s.data
}

// Restore session state after reconnect
func (s *Session) Restore(data map[string]interface{}) {
s.data = data
}
72 changes: 48 additions & 24 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestNewSession(t *testing.T) {

func TestSession_Bind(t *testing.T) {
s := NewSession(nil)
uids := []int64{100,1000,10000000}
uids := []int64{100, 1000, 10000000}
for i, uid := range uids {
s.Bind(uid)
if s.Uid != uids[i] {
Expand All @@ -24,7 +24,7 @@ func TestSession_HasKey(t *testing.T) {
s := NewSession(nil)
key := "hello"
value := "world"
s.Set(key,value)
s.Set(key, value)
if !s.HasKey(key) {
t.Fail()
}
Expand All @@ -34,7 +34,7 @@ func TestSession_Float32(t *testing.T) {
s := NewSession(nil)
key := "hello"
value := float32(1.2000)
s.Set(key,value)
s.Set(key, value)
if value != s.Float32(key) {
t.Fail()
}
Expand All @@ -44,108 +44,132 @@ func TestSession_Float64(t *testing.T) {
s := NewSession(nil)
key := "hello"
value := 1.2000
s.Set(key,value)
s.Set(key, value)
if value != s.Float64(key) {
t.Fail()
}
}

func TestSession_Int(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := 234
s.Set(key,value)
s.Set(key, value)
if value != s.Int(key) {
t.Fail()
}
}

func TestSession_Int8(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := int8(123)
s.Set(key,value)
s.Set(key, value)
if value != s.Int8(key) {
t.Fail()
}
}

func TestSession_Int16(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := int16(3245)
s.Set(key,value)
s.Set(key, value)
if value != s.Int16(key) {
t.Fail()
}
}

func TestSession_Int32(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := int32(5454)
s.Set(key,value)
s.Set(key, value)
if value != s.Int32(key) {
t.Fail()
}
}

func TestSession_Int64(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := int64(444454)
s.Set(key,value)
s.Set(key, value)
if value != s.Int64(key) {
t.Fail()
}
}

func TestSession_Uint(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := uint(24254)
s.Set(key,value)
s.Set(key, value)
if value != s.Uint(key) {
t.Fail()
}
}

func TestSession_Uint8(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := uint8(34)
s.Set(key,value)
s.Set(key, value)
if value != s.Uint8(key) {
t.Fail()
}
}

func TestSession_Uint16(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := uint16(4645)
s.Set(key,value)
s.Set(key, value)
if value != s.Uint16(key) {
t.Fail()
}
}

func TestSession_Uint32(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := uint32(12365)
s.Set(key,value)
s.Set(key, value)
if value != s.Uint32(key) {
t.Fail()
}
}

func TestSession_Uint64(t *testing.T) {
s := NewSession(nil)
s := NewSession(nil)
key := "testkey"
value := uint64(1000)
s.Set(key,value)
s.Set(key, value)
if value != s.Uint64(key) {
t.Fail()
}
}

func TestSession_State(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := uint64(1000)
s.Set(key, value)
state := s.State()
if value != state[key].(uint64) {
t.Fail()
}
}

func TestSession_Restore(t *testing.T) {
s := NewSession(nil)
s2 := NewSession(nil)
key := "testkey"
value := uint64(1000)
s.Set(key, value)
state := s.State()
s2.Restore(state)
if value != s2.Uint64(key) {
t.Fail()
}
}

0 comments on commit edc845f

Please sign in to comment.