Use sequential new display IDs to preserve sorting on setup page.

This commit is contained in:
Patrick Fairbank
2019-09-14 17:31:36 -07:00
parent ebf9a88c27
commit a39f3a0bd0
5 changed files with 11 additions and 14 deletions

View File

@@ -7,7 +7,6 @@ package field
import (
"fmt"
"math/rand"
"net/url"
"reflect"
"sort"
@@ -18,7 +17,6 @@ import (
const (
minDisplayId = 100
maxDisplayId = 999
)
type DisplayType int
@@ -136,11 +134,12 @@ func (arena *Arena) NextDisplayId() string {
// Loop until we get an ID that isn't already used. This is inefficient if there is a large number of displays, but
// that should never be the case.
candidateId := minDisplayId
for {
candidateId := strconv.Itoa(rand.Intn(maxDisplayId+1-minDisplayId) + minDisplayId)
if _, ok := arena.Displays[candidateId]; !ok {
return candidateId
if _, ok := arena.Displays[strconv.Itoa(candidateId)]; !ok {
return strconv.Itoa(candidateId)
}
candidateId++
}
}
@@ -194,8 +193,7 @@ func (arena *Arena) MarkDisplayDisconnected(display *Display) {
delete(arena.Displays, existingDisplay.Id)
} else {
existingDisplay.ConnectionCount -= 1
arena.DisplayConfigurationNotifier.Notify()
}
arena.DisplayConfigurationNotifier.Notify()
}
}

View File

@@ -59,12 +59,11 @@ func TestDisplayToUrl(t *testing.T) {
func TestNextDisplayId(t *testing.T) {
arena := setupTestArena(t)
assert.Equal(t, "874", arena.NextDisplayId())
assert.Equal(t, "100", arena.NextDisplayId())
// The next random numbers for the test seed are 514 and 653; check that a number is skipped if already used.
display := &Display{Id: "514"}
display := &Display{Id: "100"}
arena.RegisterDisplay(display)
assert.Equal(t, "653", arena.NextDisplayId())
assert.Equal(t, "101", arena.NextDisplayId())
}
func TestDisplayRegisterUnregister(t *testing.T) {