Skip to content

Commit

Permalink
feat(varlogtest): create new instance of admin and log
Browse files Browse the repository at this point in the history
This PR makes the varlogtest return a new instance of admin client and log client when
`pkg/varlogtest.(*VarlogTest).Admin` and `pkg/varlogtest.(*VarlogTest).Log` are invoked.
Previously, those returned the same instances so callers shared them. However, it wasn't right
decision because the callers had to share the clients regardless their needs.
  • Loading branch information
ijsong committed Sep 25, 2023
1 parent 6b37408 commit 2b31e74
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
7 changes: 4 additions & 3 deletions pkg/varlogtest/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
)

type testAdmin struct {
vt *VarlogTest
vt *VarlogTest
closed bool
}

var _ varlog.Admin = (*testAdmin)(nil)

func (c *testAdmin) lock() error {
c.vt.cond.L.Lock()
if c.vt.adminClientClosed {
if c.closed {
c.vt.cond.L.Unlock()
return verrors.ErrClosed
}
Expand Down Expand Up @@ -534,6 +535,6 @@ func (c *testAdmin) RemoveMRPeer(ctx context.Context, raftURL string, opts ...va
func (c *testAdmin) Close() error {
c.vt.cond.L.Lock()
defer c.vt.cond.L.Unlock()
c.vt.adminClientClosed = true
c.closed = true
return nil
}
9 changes: 5 additions & 4 deletions pkg/varlogtest/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
)

type testLog struct {
vt *VarlogTest
vt *VarlogTest
closed bool
}

var _ varlog.Log = (*testLog)(nil)

func (c *testLog) lock() error {
c.vt.cond.L.Lock()
if c.vt.varlogClientClosed {
if c.closed {
c.vt.cond.L.Unlock()
return verrors.ErrClosed
}
Expand All @@ -35,7 +36,7 @@ func (c *testLog) unlock() {
func (c *testLog) Close() error {
c.vt.cond.L.Lock()
defer c.vt.cond.L.Unlock()
c.vt.varlogClientClosed = true
c.closed = true
c.vt.cond.Broadcast()
return nil
}
Expand Down Expand Up @@ -247,7 +248,7 @@ func (c *testLog) SubscribeTo(ctx context.Context, topicID types.TopicID, logStr
return logEntries
}
s.vt.closedClient = func() bool {
return c.vt.varlogClientClosed
return c.closed
}

s.wg.Add(1)
Expand Down
12 changes: 2 additions & 10 deletions pkg/varlogtest/varlogtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
type VarlogTest struct {
config

admin *testAdmin
vlg *testLog

rng *rand.Rand

mu sync.Mutex
Expand All @@ -38,9 +35,6 @@ type VarlogTest struct {
nextTopicID types.TopicID
nextStorageNodeID types.StorageNodeID
nextLogStreamID types.LogStreamID

adminClientClosed bool
varlogClientClosed bool
}

func New(opts ...Option) (*VarlogTest, error) {
Expand All @@ -62,8 +56,6 @@ func New(opts ...Option) (*VarlogTest, error) {
leaderMR: types.InvalidNodeID,
}
vt.cond = sync.NewCond(&vt.mu)
vt.admin = &testAdmin{vt: vt}
vt.vlg = &testLog{vt: vt}

for _, mrn := range vt.initialMRNodes {
if vt.leaderMR == types.InvalidNodeID {
Expand All @@ -77,11 +69,11 @@ func New(opts ...Option) (*VarlogTest, error) {
}

func (vt *VarlogTest) Admin() varlog.Admin {
return vt.admin
return &testAdmin{vt: vt}
}

func (vt *VarlogTest) Log() varlog.Log {
return vt.vlg
return &testLog{vt: vt}
}

func (vt *VarlogTest) generateTopicID() types.TopicID {
Expand Down

0 comments on commit 2b31e74

Please sign in to comment.