Clarify the use of gooseMutex to proect goose internal state

Signed-off-by: Brian Meek <brian@hntlabs.com>
This commit is contained in:
Brian Meek 2022-08-02 08:03:08 -07:00
parent 55b3edcaae
commit 2b45bd6781
No known key found for this signature in database
GPG key ID: ACBD71263BF42D00
2 changed files with 13 additions and 15 deletions

View file

@ -48,9 +48,11 @@ type Migration struct {
Down func(ctx context.Context, txn *sql.Tx) error Down func(ctx context.Context, txn *sql.Tx) error
} }
// Gaurs the internal state of Goose from being modified by concurrent tests or goroutines
var gooseMutex sync.Mutex
// Migrator // Migrator
type Migrator struct { type Migrator struct {
gooseMutex sync.Mutex
db *sql.DB db *sql.DB
migrations []Migration migrations []Migration
knownMigrations map[string]struct{} knownMigrations map[string]struct{}
@ -82,8 +84,8 @@ func (m *Migrator) AddMigrations(migrations ...Migration) {
// Up executes all migrations in order they were added. // Up executes all migrations in order they were added.
func (m *Migrator) Up(ctx context.Context) error { func (m *Migrator) Up(ctx context.Context) error {
m.gooseMutex.Lock() gooseMutex.Lock()
defer m.gooseMutex.Unlock() defer gooseMutex.Unlock()
var ( var (
err error err error
dendriteVersion = internal.VersionString() dendriteVersion = internal.VersionString()

View file

@ -128,19 +128,15 @@ func TestRequestPool_updatePresence(t *testing.T) {
go rp.cleanPresence(db, time.Millisecond*50) go rp.cleanPresence(db, time.Millisecond*50)
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
beforeCount := func() int { publisher.lock.Lock()
publisher.lock.Lock() beforeCount := publisher.count
defer publisher.lock.Unlock() publisher.lock.Unlock()
return publisher.count
}()
rp.updatePresence(db, tt.args.presence, tt.args.userID) rp.updatePresence(db, tt.args.presence, tt.args.userID)
func() { publisher.lock.Lock()
publisher.lock.Lock() if tt.wantIncrease && publisher.count <= beforeCount {
defer publisher.lock.Unlock() t.Fatalf("expected count to increase: %d <= %d", publisher.count, beforeCount)
if tt.wantIncrease && publisher.count <= beforeCount { }
t.Fatalf("expected count to increase: %d <= %d", publisher.count, beforeCount) publisher.lock.Unlock()
}
}()
time.Sleep(tt.args.sleep) time.Sleep(tt.args.sleep)
}) })
} }