From 70b17d29b996495b1533d85df964ee2d8470ac0f Mon Sep 17 00:00:00 2001 From: Chris Lonng Date: Wed, 25 Jan 2017 11:48:06 +0800 Subject: [PATCH] feature: session state store and restore, which used in network disconnect and reconnect --- session/session.go | 10 ++++++ session/session_test.go | 72 +++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/session/session.go b/session/session.go index eb6ce25..577f22a 100644 --- a/session/session.go +++ b/session/session.go @@ -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 +} diff --git a/session/session_test.go b/session/session_test.go index 76638b5..63d6838 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -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] { @@ -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() } @@ -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() } @@ -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() + } } \ No newline at end of file