Skip to content

Commit

Permalink
fix(gmm): add atomic bool to check if has been built
Browse files Browse the repository at this point in the history
  • Loading branch information
hedon954 committed Nov 26, 2024
1 parent 9c02797 commit f6e062a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions gmm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"reflect"
"strconv"
"sync/atomic"

"github.com/dolthub/go-mysql-server/server"
"github.com/google/uuid"
Expand All @@ -18,12 +19,13 @@ import (

// GMMBuilder struct for building and managing the mock MySQL server
type GMMBuilder struct {
dbName string
port int
server *server.Server
sqlDB *sql.DB
gormDB *gorm.DB
err error
dbName string
port int
server *server.Server
sqlDB *sql.DB
gormDB *gorm.DB
err error
started atomic.Bool

tables []schema.Tabler
models []schema.Tabler
Expand All @@ -39,6 +41,7 @@ func Builder(db ...string) *GMMBuilder {
models: make([]schema.Tabler, 0),
sqlStmts: make([]string, 0),
sqlFiles: make([]string, 0),
started: atomic.Bool{},
}
dbName := "gmm-test-db-" + uuid.NewString()[:6]
if len(db) > 0 {
Expand All @@ -61,6 +64,10 @@ func (b *GMMBuilder) Build() (sDB *sql.DB, gDB *gorm.DB, shutdown func(), err er
return nil, nil, nil, b.err
}

if !b.started.CompareAndSwap(false, true) {
return nil, nil, nil, errors.New("gmm server already started")
}

// If not specify port, get an unused one form local machine.
//
// NOTE: The `getFreePort` method indeed has the limitation
Expand Down

0 comments on commit f6e062a

Please sign in to comment.