Browse Source

Add next request id function.

master
Jeff Baskin 4 years ago
parent
commit
c0b334e2f4
  1. 2
      genid.go
  2. 11
      sessionmanager.go
  3. 66
      sessionmanager_test.go

2
genid.go

@ -9,7 +9,7 @@ import (
const idLength = 64
var runeList = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var runeList = []rune("_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func init() {
rand.Seed(time.Now().UnixNano())

11
sessionmanager.go

@ -66,3 +66,14 @@ func (sm *sessionManager) getSession(sessid string) string {
sm.msgOut <- msg
return <-returnChan
}
func (sm *sessionManager) nextRequestID(sessid string) string {
reqid := newID()
msg := sessionMessage{
action: sessionUpdate,
sessionID: sessid,
requestID: reqid,
}
sm.msgOut <- msg
return reqid
}

66
sessionmanager_test.go

@ -11,6 +11,8 @@ type testServer struct {
msgStore chan sessionMessage
}
const smTestLoopCount = 1000
func newTestServer() *testServer {
ts := &testServer{
sm: newSessionManager(),
@ -26,6 +28,16 @@ func newTestServer() *testServer {
return ts
}
func newBenchmarkServer() *sessionManager {
sm := newSessionManager()
go func() {
for msg := range sm.msgOut {
sm.msgIn <- msg
}
}()
return sm
}
func TestGetSessionMessage(t *testing.T) {
sessid := newID()
reqid := newID()
@ -120,6 +132,22 @@ func TestNewSession(t *testing.T) {
}
}
func TestNewSessionRandomID(t *testing.T) {
sm := newBenchmarkServer()
holder := make(map[string]bool)
for i := 0; i < smTestLoopCount; i++ {
sessid, reqid := sm.newSession()
if holder[sessid] {
t.Errorf("Session ID '%s' was repeated.", sessid)
}
if holder[reqid] {
t.Errorf("Request ID '%s' was repeated.", reqid)
}
holder[sessid] = true
holder[reqid] = true
}
}
func TestGetSession(t *testing.T) {
ts := newTestServer()
sessid, reqid := ts.sm.newSession()
@ -128,3 +156,41 @@ func TestGetSession(t *testing.T) {
t.Errorf("For request id: Expected '%s' but got '%s'.", reqid, rreqid)
}
}
func TestNextRequestID(t *testing.T) {
ts := newTestServer()
sessid, reqid := ts.sm.newSession()
nreqid := ts.sm.nextRequestID(sessid)
close(ts.sm.msgOut)
if nreqid == reqid {
t.Errorf("New request id equaled the old one: %s'", reqid)
}
msg := sessionMessage{
action: sessionUpdate,
sessionID: sessid,
requestID: nreqid,
}
found := false
for readMsg := range ts.msgStore {
if readMsg == msg {
found = true
}
}
if !found {
t.Errorf("No update id msg: action: %d; session id: %s; request id: %s", msg.action, msg.sessionID, msg.requestID)
}
}
func TestNextRequestIDisRandom(t *testing.T) {
sm := newBenchmarkServer()
holder := make(map[string]bool)
sessid, reqid := sm.newSession()
holder[reqid] = true
for i := 0; i < smTestLoopCount; i++ {
reqid = sm.nextRequestID(sessid)
if holder[reqid] {
t.Errorf("Request ID '%s' was repeated.", reqid)
}
holder[reqid] = true
}
}
Loading…
Cancel
Save