Skip to content

Commit

Permalink
fix: renamed SealGlobalConnCfgs -> FixGlobalConnCfgs
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Dec 3, 2022
1 parent 306862d commit 7c521fc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
22 changes: 11 additions & 11 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type ConnCfg interface {
}

var (
isGlobalConnCfgSealed bool = false
isGlobalConnCfgsFixed bool = false
globalConnCfgMap map[string]ConnCfg = make(map[string]ConnCfg)
globalConnCfgMutex sync.Mutex
)
Expand All @@ -60,20 +60,20 @@ func AddGlobalConnCfg(name string, cfg ConnCfg) {
globalConnCfgMutex.Lock()
defer globalConnCfgMutex.Unlock()

if !isGlobalConnCfgSealed {
if !isGlobalConnCfgsFixed {
globalConnCfgMap[name] = cfg
}
}

// SealGlobalConnCfgs makes unable to register any further global ConnCfg.
func SealGlobalConnCfgs() {
isGlobalConnCfgSealed = true
// FixGlobalConnCfgs makes unable to register any further global ConnCfg.
func FixGlobalConnCfgs() {
isGlobalConnCfgsFixed = true
}

// ConnBase is a structure type which manages multiple Conn and ConnCfg, and
// also work as an implementation of Dax interface.
type ConnBase struct {
isLocalConnCfgSealed bool
isLocalConnCfgsFixed bool
localConnCfgMap map[string]ConnCfg
connMap map[string]Conn
connMutex sync.Mutex
Expand All @@ -82,7 +82,7 @@ type ConnBase struct {
// NewConnBase is a function which creates a new ConnBase.
func NewConnBase() *ConnBase {
return &ConnBase{
isLocalConnCfgSealed: false,
isLocalConnCfgsFixed: false,
localConnCfgMap: make(map[string]ConnCfg),
connMap: make(map[string]Conn),
}
Expand All @@ -94,7 +94,7 @@ func (base *ConnBase) AddLocalConnCfg(name string, cfg ConnCfg) {
base.connMutex.Lock()
defer base.connMutex.Unlock()

if !base.isLocalConnCfgSealed {
if !base.isLocalConnCfgsFixed {
base.localConnCfgMap[name] = cfg
}
}
Expand Down Expand Up @@ -138,8 +138,8 @@ func (base *ConnBase) GetConn(name string) (Conn, Err) {
}

func (base *ConnBase) begin() {
base.isLocalConnCfgSealed = true
isGlobalConnCfgSealed = true
base.isLocalConnCfgsFixed = true
isGlobalConnCfgsFixed = true
}

type namedErr struct {
Expand Down Expand Up @@ -203,5 +203,5 @@ func (base *ConnBase) close() {

wg.Wait()

base.isLocalConnCfgSealed = false
base.isLocalConnCfgsFixed = false
}
56 changes: 28 additions & 28 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type /* error reason */ (
)

func Clear() {
isGlobalConnCfgSealed = false
isGlobalConnCfgsFixed = false
globalConnCfgMap = make(map[string]ConnCfg)

logs.Init()
Expand Down Expand Up @@ -85,47 +85,47 @@ func TestAddGlobalConnCfg(t *testing.T) {
Clear()
defer Clear()

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 0)

AddGlobalConnCfg("foo", FooConnCfg{})

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 1)

AddGlobalConnCfg("bar", &BarConnCfg{})

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 2)
}

func TestSealGlobalConnCfgs(t *testing.T) {
func TestFixGlobalConnCfgs(t *testing.T) {
Clear()
defer Clear()

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 0)

AddGlobalConnCfg("foo", FooConnCfg{})

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 1)

SealGlobalConnCfgs()
FixGlobalConnCfgs()

assert.True(t, isGlobalConnCfgSealed)
assert.True(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 1)

AddGlobalConnCfg("bar", BarConnCfg{})

assert.True(t, isGlobalConnCfgSealed)
assert.True(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 1)

isGlobalConnCfgSealed = false
isGlobalConnCfgsFixed = false

AddGlobalConnCfg("bar", &BarConnCfg{})

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.Equal(t, len(globalConnCfgMap), 2)
}

Expand All @@ -135,7 +135,7 @@ func TestNewConnBase(t *testing.T) {

base := NewConnBase()

assert.False(t, base.isLocalConnCfgSealed)
assert.False(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 0)
assert.Equal(t, len(base.connMap), 0)
}
Expand All @@ -146,19 +146,19 @@ func TestConnBase_AddLocalConnCfg(t *testing.T) {

base := NewConnBase()

assert.False(t, base.isLocalConnCfgSealed)
assert.False(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 0)
assert.Equal(t, len(base.connMap), 0)

base.AddLocalConnCfg("foo", FooConnCfg{})

assert.False(t, base.isLocalConnCfgSealed)
assert.False(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 1)
assert.Equal(t, len(base.connMap), 0)

base.AddLocalConnCfg("bar", &BarConnCfg{})

assert.False(t, base.isLocalConnCfgSealed)
assert.False(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 2)
assert.Equal(t, len(base.connMap), 0)
}
Expand All @@ -169,38 +169,38 @@ func TestConnBase_begin(t *testing.T) {

base := NewConnBase()

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, base.isLocalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.False(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 0)
assert.Equal(t, len(base.connMap), 0)

base.AddLocalConnCfg("foo", FooConnCfg{})

assert.False(t, isGlobalConnCfgSealed)
assert.False(t, base.isLocalConnCfgSealed)
assert.False(t, isGlobalConnCfgsFixed)
assert.False(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 1)
assert.Equal(t, len(base.connMap), 0)

base.begin()

assert.True(t, isGlobalConnCfgSealed)
assert.True(t, base.isLocalConnCfgSealed)
assert.True(t, isGlobalConnCfgsFixed)
assert.True(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 1)
assert.Equal(t, len(base.connMap), 0)

base.AddLocalConnCfg("bar", &BarConnCfg{})

assert.True(t, isGlobalConnCfgSealed)
assert.True(t, base.isLocalConnCfgSealed)
assert.True(t, isGlobalConnCfgsFixed)
assert.True(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 1)
assert.Equal(t, len(base.connMap), 0)

base.isLocalConnCfgSealed = false
base.isLocalConnCfgsFixed = false

base.AddLocalConnCfg("bar", &BarConnCfg{})

assert.True(t, isGlobalConnCfgSealed)
assert.False(t, base.isLocalConnCfgSealed)
assert.True(t, isGlobalConnCfgsFixed)
assert.False(t, base.isLocalConnCfgsFixed)
assert.Equal(t, len(base.localConnCfgMap), 2)
assert.Equal(t, len(base.connMap), 0)
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestConnBase_GetConn_localCfgIsTakenPriorityOfGlobalCfg(t *testing.T) {
}

AddGlobalConnCfg("foo", FooConnCfg{Label: "global"})
SealGlobalConnCfgs()
FixGlobalConnCfgs()

base.AddLocalConnCfg("foo", FooConnCfg{Label: "local"})

Expand Down
4 changes: 2 additions & 2 deletions example_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func ExampleAddGlobalConnCfg() {
sabi.Clear()
}

func ExampleSealGlobalConnCfgs() {
func ExampleFixGlobalConnCfgs() {
sabi.AddGlobalConnCfg("foo", FooConnCfg{})
sabi.SealGlobalConnCfgs()
sabi.FixGlobalConnCfgs()
sabi.AddGlobalConnCfg("bar", BarConnCfg{}) // Bad example

base := sabi.NewConnBase()
Expand Down
2 changes: 1 addition & 1 deletion example_proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (dax BarSetterDax) SetData(data string) {
func ExampleNewProc() {
sabi.AddGlobalConnCfg("foo", FooConnCfg{})
sabi.AddGlobalConnCfg("bar", BarConnCfg{})
sabi.SealGlobalConnCfgs()
sabi.FixGlobalConnCfgs()

base := sabi.NewConnBase()

Expand Down
6 changes: 3 additions & 3 deletions proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestProc_RunTxn(t *testing.T) {
defer sabi.Clear()

sabi.AddGlobalConnCfg("foo", sabi.FooConnCfg{})
sabi.SealGlobalConnCfgs()
sabi.FixGlobalConnCfgs()

store := make(map[string]string)

Expand All @@ -96,7 +96,7 @@ func TestProc_RunTxn_failToGetConn(t *testing.T) {
defer sabi.Clear()

sabi.AddGlobalConnCfg("foo", sabi.FooConnCfg{})
sabi.SealGlobalConnCfgs()
sabi.FixGlobalConnCfgs()

store := make(map[string]string)

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestProc_RunTxn_failToCommitConn(t *testing.T) {
defer sabi.Clear()

sabi.AddGlobalConnCfg("foo", sabi.FooConnCfg{})
sabi.SealGlobalConnCfgs()
sabi.FixGlobalConnCfgs()

store := make(map[string]string)

Expand Down

0 comments on commit 7c521fc

Please sign in to comment.