Skip to content

Commit

Permalink
fix: 🐛 unmarshalling json error
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskaransarkaria committed Jul 4, 2020
1 parent 4f70368 commit 886d5eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .kubernetes/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/proxy-connect-timeout: "7200"
nginx.ingress.kubernetes.io/proxy-send-timeout: "7200"
nginx.ingress.kubernetes.io/proxy-read-timeout: "7200"
nginx.ingress.kubernetes.io/cors-allow-methods: "*"
nginx.ingress.kubernetes.io/cors-allow-origin: "*"
nginx.ingress.kubernetes.io/use-regex: "true"
Expand Down
2 changes: 1 addition & 1 deletion http-routes/http-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
}

func updateSessionEndpoint(w http.ResponseWriter, r *http.Request) {
var sessionToUpdate session.Session
var sessionToUpdate session.UpdateRequest
var requestBody = r.Body
enableCors(&w)
err := json.NewDecoder(requestBody).Decode(&sessionToUpdate)
Expand Down
27 changes: 19 additions & 8 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ type ExistingSessionReq struct {
JoinSessionID string `json:"joinSession"`
}

// UpdateRequest .. Incoming timer update from client (current driver)
type UpdateRequest struct {
SessionID string `json:"sessionId"`
UpdatedDuration int64 `json:"updatedDuration,omitempty"`
}

// Sessions is a collection of all current sessions
var Sessions []Session

// UpdateTimerChannel reads updates as they come in via updateSessionEndpoint
var UpdateTimerChannel = make(chan Session)
var UpdateTimerChannel = make(chan UpdateRequest)

// CreateNewUserAndSession creates new users and sessions
func CreateNewUserAndSession(
Expand Down Expand Up @@ -97,8 +103,8 @@ func JoinExistingSession(joinExistingSessionData ExistingSessionReq, newUser Use
}

// HandleUpdateSession when a timer finishes
func HandleUpdateSession(sessionToUpdate Session) {
updatedSessionIdx, updateErr := sessionToUpdate.handleTimerEnd()
func HandleUpdateSession(sessionToUpdate UpdateRequest) {
updatedSessionIdx, updateErr := handleTimerEnd(sessionToUpdate)
if updateErr != nil {
log.Println("updateError", updateErr)
return
Expand Down Expand Up @@ -137,14 +143,19 @@ func RemoveSession(sessionID string) error {
return nil
}

func (session *Session) handleTimerEnd() (int, error) {
updatedSessionIdx, err := getExistingSession(session.SessionID)
// Map the incoming session request to an in-memory session
func handleTimerEnd(session UpdateRequest) (int, error) {
mappedSessionIdx, err := getExistingSession(session.SessionID)
if err != nil {
return -1, err
}
Sessions[updatedSessionIdx].changeDriver()
Sessions[updatedSessionIdx].resetTimer()
return updatedSessionIdx, nil
// update duration
if session.UpdatedDuration > 0 {
Sessions[mappedSessionIdx].Duration = session.UpdatedDuration
}
Sessions[mappedSessionIdx].changeDriver()
Sessions[mappedSessionIdx].resetTimer()
return mappedSessionIdx, nil
}

func getExistingSession(desiredSessionID string) (int, error) {
Expand Down
6 changes: 5 additions & 1 deletion session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ func TestHandleUpdateSession(t *testing.T) {
JoinSessionID: sessionID,
}
testSession, _ := JoinExistingSession(sessionToJoin, newUser)
mockUpdateRequest := UpdateRequest{
SessionID: testSession.SessionID,
UpdatedDuration: testSession.Duration,
}
// mock broadcast to all sessionUsers
mockConnInitUser.On("WriteJSON", &Sessions[sessionIndex]).Return(nil)
mockConnJoiningUser.On("WriteJSON", &Sessions[sessionIndex]).Return(nil)
// fire handle time end (changes driver and resets the timer)
HandleUpdateSession(testSession)
HandleUpdateSession(mockUpdateRequest)

if Sessions[sessionIndex].CurrentDriver.UUID != newUser.UUID {
t.Errorf("The Driver has not been correctly changed")
Expand Down

0 comments on commit 886d5eb

Please sign in to comment.