Skip to content

Commit

Permalink
Changes to v0 raiqub/data updates
Browse files Browse the repository at this point in the history
  • Loading branch information
skarllot committed Jan 22, 2016
1 parent f23fcd1 commit e7af930
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
6 changes: 0 additions & 6 deletions jsonerrorbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ func NewJSONError() JSONErrorBuilder {
}}
}

func (b *jsonErrorBuilder) Basic(status int, msg string) JSONErrorBuilder {
b.instance.Status = status
b.instance.Message = msg
return b
}

func (b *jsonErrorBuilder) Build() JSONError {
return b.instance
}
Expand Down
14 changes: 8 additions & 6 deletions sessioncache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package http

import (
"github.com/raiqub/data"
"github.com/skarllot/raiqub/crypt"
"gopkg.in/raiqub/data.v0"
"gopkg.in/raiqub/dot.v1"
)

// A SessionCache provides a temporary token to uniquely identify an user
Expand Down Expand Up @@ -62,12 +63,13 @@ func (s *SessionCache) Count() (int, error) {
//
// Errors:
// InvalidTokenError when requested token could not be found.
func (s *SessionCache) Get(token string) (interface{}, error) {
v, err := s.cache.Get(token)
if err != nil {
return nil, InvalidTokenError(token)
func (s *SessionCache) Get(token string, ref interface{}) error {
err := s.cache.Get(token, ref)
if _, ok := err.(dot.InvalidKeyError); ok {
return InvalidTokenError(token)
}
return v, err

return err
}

// Add creates a new unique token and stores it into current SessionCache
Expand Down
36 changes: 23 additions & 13 deletions sessioncache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ import (
"testing"
"time"

"github.com/raiqub/data"
"gopkg.in/raiqub/data.v0/memstore"
)

const TokenSalt = "CvoTVwDw685Ve0qjGn//zmHGKvoCcslYNQT4AQ9FygSk9t6NuzBHuohyO" +
"Hhqb/1omn6c"

func TestSessionLifetime(t *testing.T) {
store := data.NewCacheStore(time.Millisecond * 10)
store := memstore.New(time.Millisecond*10, false)
ts := NewSessionCache(store, TokenSalt)

t1 := ts.Add()
t2 := ts.Add()

if _, err := ts.Get(t1); err != nil {
if err := ts.Get(t1, nil); err != nil {
t.Error("The session t1 was not stored")
}
if _, err := ts.Get(t2); err != nil {
if err := ts.Get(t2, nil); err != nil {
t.Error("The session t2 was not stored")
}

time.Sleep(time.Millisecond * 20)

if _, err := ts.Get(t1); err == nil {
if err := ts.Get(t1, nil); err == nil {
t.Error("The session t1 was not expired")
}
if _, err := ts.Get(t2); err == nil {
if err := ts.Get(t2, nil); err == nil {
t.Error("The session t2 was not expired")
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func TestSessionHandling(t *testing.T) {
9: 4099,
}

store := data.NewCacheStore(time.Millisecond * 100)
store := memstore.New(time.Millisecond*100, false)
ts := NewSessionCache(store, TokenSalt)
if _, err := ts.Count(); err != nil {
t.Fatal("The Count() method should be supported by MemStore")
Expand Down Expand Up @@ -119,8 +119,8 @@ func TestSessionHandling(t *testing.T) {
}

for _, i := range testValues {
v, err := ts.Get(i.token)
if err != nil {
var v int
if err := ts.Get(i.token, &v); err != nil {
t.Errorf("The session %s could not be read", i.ref)
}
if v != i.value {
Expand All @@ -132,7 +132,7 @@ func TestSessionHandling(t *testing.T) {
if err := ts.Delete(rmTestKey.token); err != nil {
t.Errorf("The session %s could not be removed", rmTestKey.ref)
}
if _, err := ts.Get(rmTestKey.token); err == nil {
if err := ts.Get(rmTestKey.token, nil); err == nil {
t.Errorf("The removed session %s should not be retrieved", rmTestKey.ref)
}
if count, _ := ts.Count(); count == len(testValues) {
Expand All @@ -148,8 +148,8 @@ func TestSessionHandling(t *testing.T) {
}
for k, v := range changeValues {
item := testValues[k]
v2, err := ts.Get(item.token)
if err != nil {
var v2 int
if err := ts.Get(item.token, &v2); err != nil {
t.Errorf("The session %s could not be read", item.ref)
}
if v2 != v {
Expand All @@ -159,11 +159,21 @@ func TestSessionHandling(t *testing.T) {
}

func BenchmarkSessionCreation(b *testing.B) {
store := data.NewCacheStore(time.Millisecond)
store := memstore.New(time.Millisecond, false)
ts := NewSessionCache(store, TokenSalt)
b.ResetTimer()

for i := 0; i < b.N; i++ {
ts.Add()
}
}

func BenchmarkSessionCreationFast(b *testing.B) {
store := memstore.New(time.Millisecond, false)
ts := NewSessionCacheFast(store, TokenSalt)
b.ResetTimer()

for i := 0; i < b.N; i++ {
ts.Add()
}
}

0 comments on commit e7af930

Please sign in to comment.