From e48f8c46fb6f141d0169609278352cbf872e37dd Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Fri, 26 May 2017 14:24:57 +1000 Subject: [PATCH] session test with id naming as procedure --- chatSession.go | 38 ++++++++++++++++++----------------- chatSession_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 chatSession_test.go diff --git a/chatSession.go b/chatSession.go index 0d7eddd..2028c6c 100644 --- a/chatSession.go +++ b/chatSession.go @@ -9,32 +9,34 @@ import ( "time" ) -//a state is a alphanumerical string lessthan 128 bytes. +//a Procedure is a alphanumerical string lessthan 128 bytes. -//when messages comes in we need to differentiate what state is this in. -//default state is where command can happen +//when messages comes in we need to differentiate what Procedure is this in. +//default Procedure is where command can happen -//where the state is stored, +//where the Procedure is stored, // in sessions/openID.json -//if there is no such file, we are in default state. +//if there is no such file, we are in default Procedure. -//First state is to record user's name, email, and phone number. -// for individual state -// state/statename/openID.json +//First Procedure is to record user's name, email, and phone number. +// for individual Procedure +// Procedure/Procedurename/openID.json var sessionDir = "sessions" -var stateDir = "state" + +//where to find the procedure state information +var ProcedureDir = "Procedure" //openIDSession openID user's session type openIDSession struct { - State string `json:"State"` //name of the current state alphanumerical string - CreateAt int32 `json:"CreateAt"` //when is this session created - UpdateAt int32 `json:"UpdateAt"` //when is this session updated - Expire int32 `json:"Expire"` //unix timestamp of when this state expires + Procedure string `json:"Procedure"` //name of the current current process, alphanumerical + CreateAt int32 `json:"CreateAt"` //when is this session created + UpdateAt int32 `json:"UpdateAt"` //when is this session updated + Expire int32 `json:"Expire"` //unix timestamp of when this Procedure expires } -//read sessions/openID.json to check whether its a state for ongoing dialog +//read sessions/openID.json to check whether its a Procedure for ongoing dialog func getCurrentSesssion(openID string) (result openIDSession, err error) { path := getSessionPath(openID) log.Printf("read session from %s\r\n", path) @@ -73,14 +75,14 @@ func writeSession(openid string, ss openIDSession) (err error) { } //create if not available -func setSessionState(openID, state string, expireAfter int32) (updatedSession openIDSession, err error) { +func setSessionProcedure(openID, procedure string, expireAfter int32) (updatedSession openIDSession, err error) { s, err := getCurrentSesssion(openID) now := int32(time.Now().Unix()) if s.CreateAt == 0 { s.CreateAt = now } s.UpdateAt = now - s.State = state + s.Procedure = procedure s.Expire = now + expireAfter err = writeSession(openID, s) return s, err @@ -97,8 +99,8 @@ func getSessionPath(openID string) (path string) { return path } -func getStatePath(openID, stateName string) (path string) { - path = stateDir + string(os.PathSeparator) + stateName + string(os.PathSeparator) + openID + ".json" +func getProcedurePath(openID, ProcedureName string) (path string) { + path = ProcedureDir + string(os.PathSeparator) + ProcedureName + string(os.PathSeparator) + openID + ".json" ensurePathExist(path) return } diff --git a/chatSession_test.go b/chatSession_test.go new file mode 100644 index 0000000..f276fc8 --- /dev/null +++ b/chatSession_test.go @@ -0,0 +1,48 @@ +package main + +import ( + "log" + "testing" + "time" +) + +func TestSession(t *testing.T) { + id := "testopenid" + procedure := "test procedure" + + //delete any existing + deleteSession(id) + path := getSessionPath(id) + AssertEqual(t, isFileExist(path), false, "Session file should not exist") + + //create new + setSessionProcedure(id, procedure, 100) + + //wait 1 sec + log.Print("wait for 1 sec for testing timestamp ......") + time.Sleep(1 * time.Second) + log.Print("......waiting done") + + //read back , check state + s, _ := getCurrentSesssion(id) + AssertEqual(t, s.Procedure, procedure, "") + now := int32(time.Now().Unix()) + + //check timing + AssertEqual(t, s.Expire > now, true, "Expire should be in future") + AssertEqual(t, s.UpdateAt < now, true, "Update should be in pass") + AssertEqual(t, s.CreateAt == s.UpdateAt, true, "Update should be equal to create") + AssertEqual(t, isExpired(now), false, "current time should not be expired") + + //update existing session + s, _ = setSessionProcedure(id, procedure, 100) + //timeing should be exactly 1s diff + AssertEqual(t, s.CreateAt+1 == s.UpdateAt, true, "second Update should be bigger create") + n, _ := getCurrentSesssion(id) + AssertEqual(t, s, n, "session should be equal") + + //delete session, for clean up + deleteSession(id) + path = getSessionPath(id) + AssertEqual(t, isFileExist(path), false, "Session file should not exist") +}