Multiple meeting lobbies and beta-gated features (3.5.2)
- /meeting add-lobby, remove-lobby, lobbies: up to 10 lobbies per server, capped in the database - Deleted voice channels are dropped from the lobbies - Beta features: registered only in the beta server and usable only with the Beta Tester role (BETA_GUILD_ID, BETA_ROLE_ID); fails closed - /beta (bot owner only) shows how beta access is set up Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
136377d81a
commit
a364435bab
15 files changed
+789
-52
No files matched your search
@@ -0,0 +1,119 @@
|
||||
//go:build integration
|
||||
|
||||
// Runs against a real Postgres with the dashboard API's migrations applied
|
||||
// (the lobby limit is a trigger from migration 000020):
|
||||
//
|
||||
// TEST_DB=postgres://... go test -tags integration ./internal/db/repos/settingsrepo
|
||||
package settingsrepo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
func testRepo(t *testing.T) (*Repo, int64) {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("TEST_DB")
|
||||
if dsn == "" {
|
||||
t.Skip("TEST_DB not set")
|
||||
}
|
||||
db, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { db.Close() })
|
||||
const guild = 4242
|
||||
if _, err := db.Exec(`DELETE FROM meeting_lobbies WHERE guild_id IN ($1, $2)`, guild, guild+1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return NewRepo(db), guild
|
||||
}
|
||||
|
||||
func TestMeetingLobbies(t *testing.T) {
|
||||
r, g := testRepo(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if ids, err := r.ListMeetingLobbies(ctx, g); err != nil || len(ids) != 0 {
|
||||
t.Fatalf("a fresh server has no lobbies: %v %v", ids, err)
|
||||
}
|
||||
|
||||
for ch := int64(1); ch <= MaxMeetingLobbies; ch++ {
|
||||
added, err := r.AddMeetingLobby(ctx, g, ch)
|
||||
if err != nil || !added {
|
||||
t.Fatalf("adding lobby %d: added=%v err=%v", ch, added, err)
|
||||
}
|
||||
}
|
||||
if _, err := r.AddMeetingLobby(ctx, g, 99); !errors.Is(err, ErrMeetingLobbyLimit) {
|
||||
t.Errorf("an 11th lobby must hit the limit, got %v", err)
|
||||
}
|
||||
if added, err := r.AddMeetingLobby(ctx, g, 3); err != nil || added {
|
||||
t.Errorf("adding an existing lobby is a no-op even at the limit: added=%v err=%v", added, err)
|
||||
}
|
||||
if added, err := r.AddMeetingLobby(ctx, g+1, 99); err != nil || !added {
|
||||
t.Errorf("the limit is per server: added=%v err=%v", added, err)
|
||||
}
|
||||
|
||||
if ok, _ := r.IsMeetingLobby(ctx, g, 7); !ok {
|
||||
t.Error("7 is a lobby")
|
||||
}
|
||||
if ok, _ := r.IsMeetingLobby(ctx, g, 99); ok {
|
||||
t.Error("99 isn't a lobby of this server (it is one of the other server's)")
|
||||
}
|
||||
|
||||
if removed, err := r.RemoveMeetingLobby(ctx, g, 7); err != nil || !removed {
|
||||
t.Errorf("removing 7: removed=%v err=%v", removed, err)
|
||||
}
|
||||
if removed, _ := r.RemoveMeetingLobby(ctx, g, 7); removed {
|
||||
t.Error("7 was already removed")
|
||||
}
|
||||
if ok, _ := r.IsMeetingLobby(ctx, g, 7); ok {
|
||||
t.Error("7 is no longer a lobby")
|
||||
}
|
||||
if added, err := r.AddMeetingLobby(ctx, g, 99); err != nil || !added {
|
||||
t.Errorf("removing one makes room: added=%v err=%v", added, err)
|
||||
}
|
||||
|
||||
ids, err := r.ListMeetingLobbies(ctx, g)
|
||||
if err != nil || len(ids) != MaxMeetingLobbies || ids[len(ids)-1] != 99 {
|
||||
t.Errorf("oldest first, newest last: %v %v", ids, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Two people adding a lobby at the same moment must not both fit under the limit.
|
||||
func TestMeetingLobbiesConcurrent(t *testing.T) {
|
||||
r, g := testRepo(t)
|
||||
ctx := context.Background()
|
||||
for ch := int64(1); ch < MaxMeetingLobbies; ch++ {
|
||||
if _, err := r.AddMeetingLobby(ctx, g, ch); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
succeeded := 0
|
||||
for i := 0; i < 8; i++ {
|
||||
wg.Add(1)
|
||||
go func(ch int64) {
|
||||
defer wg.Done()
|
||||
if added, err := r.AddMeetingLobby(ctx, g, ch); err == nil && added {
|
||||
mu.Lock()
|
||||
succeeded++
|
||||
mu.Unlock()
|
||||
}
|
||||
}(int64(100 + i))
|
||||
}
|
||||
wg.Wait()
|
||||
if succeeded != 1 {
|
||||
t.Errorf("exactly one of the concurrent adds fits, %d did", succeeded)
|
||||
}
|
||||
if ids, _ := r.ListMeetingLobbies(ctx, g); len(ids) != MaxMeetingLobbies {
|
||||
t.Errorf("have %d lobbies, want %d", len(ids), MaxMeetingLobbies)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user