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

View file

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