Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactor #496

Merged
merged 3 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions app/sessions/sessions_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,21 @@ type InstanceState struct {
}

type SessionState struct {
Instances *sync.Map
Instances map[string]InstanceState `json:"instances"`
}

func (s *SessionState) IsReady() bool {
ready := true

if s.Instances == nil {
s.Instances = &sync.Map{}
s.Instances = map[string]InstanceState{}
}

s.Instances.Range(func(key, value interface{}) bool {
state := value.(InstanceState)
if state.Error != nil || state.Instance.Status != instance.Ready {
ready = false
for _, v := range s.Instances {
if v.Error != nil || v.Instance.Status != instance.Ready {
return false
}
return true
})
}

return ready
return true
}

func (s *SessionState) Status() string {
Expand All @@ -148,7 +143,7 @@ func (s *SessionsManager) RequestSession(names []string, duration time.Duration)
var wg sync.WaitGroup

sessionState = &SessionState{
Instances: &sync.Map{},
Instances: map[string]InstanceState{},
}

wg.Add(len(names))
Expand All @@ -158,10 +153,10 @@ func (s *SessionsManager) RequestSession(names []string, duration time.Duration)
defer wg.Done()
state, err := s.requestSessionInstance(name, duration)

sessionState.Instances.Store(name, InstanceState{
sessionState.Instances[name] = InstanceState{
Instance: state,
Error: err,
})
}
}(names[i])
}

Expand Down Expand Up @@ -250,6 +245,7 @@ func (s *SessionsManager) RequestReadySession(ctx context.Context, names []strin

ticker := time.NewTicker(5 * time.Second)
readiness := make(chan *SessionState)
errch := make(chan error)
quit := make(chan struct{})

go func() {
Expand All @@ -258,6 +254,7 @@ func (s *SessionsManager) RequestReadySession(ctx context.Context, names []strin
case <-ticker.C:
session, err := s.RequestSession(names, duration)
if err != nil {
errch <- err
return
}
if session.IsReady() {
Expand All @@ -278,6 +275,9 @@ func (s *SessionsManager) RequestReadySession(ctx context.Context, names []strin
case status := <-readiness:
close(quit)
return status, nil
case err := <-errch:
close(quit)
return nil, err
case <-time.After(timeout):
close(quit)
return nil, fmt.Errorf("session was not ready after %s", timeout.String())
Expand Down Expand Up @@ -318,13 +318,7 @@ func (s *SessionsManager) Stop() {
}

func (s *SessionState) MarshalJSON() ([]byte, error) {
instances := []InstanceState{}

s.Instances.Range(func(key, value interface{}) bool {
state := value.(InstanceState)
instances = append(instances, state)
return true
})
instances := maps.Values(s.Instances)

return json.Marshal(map[string]any{
"instances": instances,
Expand Down
13 changes: 6 additions & 7 deletions app/sessions/sessions_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sessions

import (
"context"
"sync"
"testing"
"time"

Expand All @@ -14,7 +13,7 @@ import (

func TestSessionState_IsReady(t *testing.T) {
type fields struct {
Instances *sync.Map
Instances map[string]InstanceState
Error error
}
tests := []struct {
Expand Down Expand Up @@ -72,17 +71,17 @@ func TestSessionState_IsReady(t *testing.T) {
}
}

func createMap(instances []*instance.State) (store *sync.Map) {
store = &sync.Map{}
func createMap(instances []*instance.State) map[string]InstanceState {
states := make(map[string]InstanceState)

for _, v := range instances {
store.Store(v.Name, InstanceState{
states[v.Name] = InstanceState{
Instance: v,
Error: nil,
})
}
}

return
return states
}

func TestNewSessionsManagerEvents(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion app/types/session.go

This file was deleted.

6 changes: 2 additions & 4 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand Down Expand Up @@ -355,8 +356,6 @@ golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
Expand All @@ -373,6 +372,7 @@ golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down Expand Up @@ -403,8 +403,6 @@ golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
Expand Down
11 changes: 3 additions & 8 deletions internal/api/start_dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,10 @@ func sessionStateToRenderOptionsInstanceState(sessionState *sessions.SessionStat
log.Warnf("sessionStateToRenderOptionsInstanceState: sessionState is nil")
return
}
sessionState.Instances.Range(func(key, value any) bool {
if value != nil {
instances = append(instances, instanceStateToRenderOptionsRequestState(value.(sessions.InstanceState).Instance))
} else {
log.Warnf("sessionStateToRenderOptionsInstanceState: sessionState instance is nil, key: %v", key)
}

return true
})
for _, v := range sessionState.Instances {
instances = append(instances, instanceStateToRenderOptionsRequestState(v.Instance))
}

sort.SliceStable(instances, func(i, j int) bool {
return strings.Compare(instances[i].Name, instances[j].Name) == -1
Expand Down
30 changes: 28 additions & 2 deletions internal/api/start_dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"errors"
"github.com/sablierapp/sablier/app/instance"
"github.com/sablierapp/sablier/app/sessions"
"github.com/tniswong/go.rfcx/rfc7807"
"go.uber.org/mock/gomock"
Expand All @@ -10,6 +11,23 @@ import (
"testing"
)

func session() *sessions.SessionState {
state := instance.ReadyInstanceState("test", 1)
state2 := instance.ReadyInstanceState("test2", 1)
return &sessions.SessionState{
Instances: map[string]sessions.InstanceState{
"test": {
Instance: &state,
Error: nil,
},
"test2": {
Instance: &state2,
Error: nil,
},
},
}
}

func TestStartDynamic(t *testing.T) {
t.Run("StartDynamicInvalidBind", func(t *testing.T) {
app, router, strategy, _ := NewApiTest(t)
Expand All @@ -32,18 +50,26 @@ func TestStartDynamic(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, r.Code)
assert.Equal(t, rfc7807.JSONMediaType, r.Header().Get("Content-Type"))
})
t.Run("StartDynamicThemeNotFound", func(t *testing.T) {
app, router, strategy, m := NewApiTest(t)
StartDynamic(router, strategy)
m.EXPECT().RequestSessionGroup("test", gomock.Any()).Return(session(), nil)
r := PerformRequest(app, "GET", "/api/strategies/dynamic?group=test&theme=invalid")
assert.Equal(t, http.StatusNotFound, r.Code)
assert.Equal(t, rfc7807.JSONMediaType, r.Header().Get("Content-Type"))
})
t.Run("StartDynamicByNames", func(t *testing.T) {
app, router, strategy, m := NewApiTest(t)
StartDynamic(router, strategy)
m.EXPECT().RequestSession([]string{"test"}, gomock.Any()).Return(&sessions.SessionState{}, nil)
m.EXPECT().RequestSession([]string{"test"}, gomock.Any()).Return(session(), nil)
r := PerformRequest(app, "GET", "/api/strategies/dynamic?names=test")
assert.Equal(t, http.StatusOK, r.Code)
assert.Equal(t, SablierStatusReady, r.Header().Get(SablierStatusHeader))
})
t.Run("StartDynamicByGroup", func(t *testing.T) {
app, router, strategy, m := NewApiTest(t)
StartDynamic(router, strategy)
m.EXPECT().RequestSessionGroup("test", gomock.Any()).Return(&sessions.SessionState{}, nil)
m.EXPECT().RequestSessionGroup("test", gomock.Any()).Return(session(), nil)
r := PerformRequest(app, "GET", "/api/strategies/dynamic?group=test")
assert.Equal(t, http.StatusOK, r.Code)
assert.Equal(t, SablierStatusReady, r.Header().Get(SablierStatusHeader))
Expand Down
Loading