Rename ServerStatus to ServerState internally

This commit is contained in:
Ken Hibino
2021-03-21 06:33:01 -07:00
parent 5af86a0903
commit 105084d6fb
9 changed files with 81 additions and 74 deletions

View File

@@ -215,52 +215,55 @@ type Z struct {
Score int64
}
// ServerStatus represents status of a server.
// ServerStatus methods are concurrency safe.
type ServerStatus struct {
// ServerState represents state of a server.
// ServerState methods are concurrency safe.
type ServerState struct {
mu sync.Mutex
val ServerStatusValue
val ServerStateValue
}
// NewServerStatus returns a new status instance given an initial value.
func NewServerStatus(v ServerStatusValue) *ServerStatus {
return &ServerStatus{val: v}
// NewServerState returns a new state instance.
// Initial state is set to StateNew.
func NewServerState() *ServerState {
return &ServerState{val: StateNew}
}
type ServerStatusValue int
type ServerStateValue int
const (
// StatusIdle indicates the server is in idle state.
StatusIdle ServerStatusValue = iota
// StateNew represents a new server. Server begins in
// this state and then transition to StatusActive when
// Start or Run is callled.
StateNew ServerStateValue = iota
// StatusActive indicates the server is up and active.
StatusActive
// StateActive indicates the server is up and active.
StateActive
// StatusStopped indicates the server is up but no longer processing new tasks.
StatusStopped
// StateStopped indicates the server is up but no longer processing new tasks.
StateStopped
// StatusClosed indicates the server has been shutdown.
StatusClosed
// StateClosed indicates the server has been shutdown.
StateClosed
)
var statuses = []string{
"idle",
var serverStates = []string{
"new",
"active",
"stopped",
"closed",
}
func (s *ServerStatus) String() string {
func (s *ServerState) String() string {
s.mu.Lock()
defer s.mu.Unlock()
if StatusIdle <= s.val && s.val <= StatusClosed {
return statuses[s.val]
if StateNew <= s.val && s.val <= StateClosed {
return serverStates[s.val]
}
return "unknown status"
}
// Get returns the status value.
func (s *ServerStatus) Get() ServerStatusValue {
func (s *ServerState) Get() ServerStateValue {
s.mu.Lock()
v := s.val
s.mu.Unlock()
@@ -268,7 +271,7 @@ func (s *ServerStatus) Get() ServerStatusValue {
}
// Set sets the status value.
func (s *ServerStatus) Set(v ServerStatusValue) {
func (s *ServerState) Set(v ServerStateValue) {
s.mu.Lock()
s.val = v
s.mu.Unlock()

View File

@@ -400,7 +400,7 @@ func TestServerInfoEncoding(t *testing.T) {
Concurrency: 10,
Queues: map[string]int{"default": 1, "critical": 2},
StrictPriority: false,
Status: "running",
Status: "active",
Started: time.Now().Add(-3 * time.Hour),
ActiveWorkerCount: 8,
},
@@ -530,7 +530,7 @@ func TestSchedulerEnqueueEventEncoding(t *testing.T) {
// Test for status being accessed by multiple goroutines.
// Run with -race flag to check for data race.
func TestStatusConcurrentAccess(t *testing.T) {
status := NewServerStatus(StatusIdle)
status := NewServerState()
var wg sync.WaitGroup
@@ -544,7 +544,7 @@ func TestStatusConcurrentAccess(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
status.Set(StatusClosed)
status.Set(StateClosed)
_ = status.String()
}()

View File

@@ -3222,7 +3222,7 @@ func TestListServers(t *testing.T) {
ServerID: "server123",
Concurrency: 10,
Queues: map[string]int{"default": 1},
Status: "running",
Status: "active",
Started: started1,
ActiveWorkerCount: 0,
}

View File

@@ -1475,7 +1475,7 @@ func TestWriteServerState(t *testing.T) {
Queues: map[string]int{"default": 2, "email": 5, "low": 1},
StrictPriority: false,
Started: time.Now().UTC(),
Status: "running",
Status: "active",
ActiveWorkerCount: 0,
}
@@ -1565,7 +1565,7 @@ func TestWriteServerStateWithWorkers(t *testing.T) {
Queues: map[string]int{"default": 2, "email": 5, "low": 1},
StrictPriority: false,
Started: time.Now().Add(-10 * time.Minute).UTC(),
Status: "running",
Status: "active",
ActiveWorkerCount: len(workers),
}
@@ -1667,7 +1667,7 @@ func TestClearServerState(t *testing.T) {
Queues: map[string]int{"default": 2, "email": 5, "low": 1},
StrictPriority: false,
Started: time.Now().Add(-10 * time.Minute),
Status: "running",
Status: "active",
ActiveWorkerCount: len(workers1),
}
@@ -1690,7 +1690,7 @@ func TestClearServerState(t *testing.T) {
Queues: map[string]int{"default": 2, "email": 5, "low": 1},
StrictPriority: false,
Started: time.Now().Add(-15 * time.Minute),
Status: "running",
Status: "active",
ActiveWorkerCount: len(workers2),
}