Skip to content

Commit

Permalink
Fix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanTinianov committed Jan 30, 2025
1 parent 656181e commit d1adf37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions multinode/rpc_client_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func NewRPCClientBase[HEAD Head](
}
}

func (m *RPCClientBase[HEAD]) LenSubs() int {
func (m *RPCClientBase[HEAD]) lenSubs() int {
m.subsMu.RLock()
defer m.subsMu.RUnlock()
return len(m.subs)
}

// RegisterSub adds the sub to the RPCClientBase list and returns a managed sub which is removed on unsubscribe
func (m *RPCClientBase[HEAD]) RegisterSub(sub Subscription, lifeCycleCh chan struct{}) (*ManagedSubscription, error) {
// ensure that the `sub` belongs to current life cycle of the `rpcMultiNodeAdapter` and it should not be killed due to
// ensure that the `sub` belongs to current life cycle of the `RPCClientBase` and it should not be killed due to
// previous `DisconnectAll` call.
select {
case <-lifeCycleCh:
Expand Down Expand Up @@ -200,7 +200,7 @@ func (m *RPCClientBase[HEAD]) OnNewHead(ctx context.Context, requestCh <-chan st
m.highestUserObservations.TotalDifficulty = MaxTotalDifficulty(m.highestUserObservations.TotalDifficulty, totalDifficulty)
}
select {
case <-requestCh: // no need to update latestChainInfo, as rpcMultiNodeAdapter already started new life cycle
case <-requestCh: // no need to update latestChainInfo, as RPCClientBase already started new life cycle
return
default:
m.latestChainInfo.BlockNumber = blockNumber
Expand All @@ -219,7 +219,7 @@ func (m *RPCClientBase[HEAD]) OnNewFinalizedHead(ctx context.Context, requestCh
m.highestUserObservations.FinalizedBlockNumber = max(m.highestUserObservations.FinalizedBlockNumber, head.BlockNumber())
}
select {
case <-requestCh: // no need to update latestChainInfo, as rpcMultiNodeAdapter already started new life cycle
case <-requestCh: // no need to update latestChainInfo, as RPCClientBase already started new life cycle
return
default:
m.latestChainInfo.FinalizedBlockNumber = head.BlockNumber()
Expand Down
22 changes: 11 additions & 11 deletions multinode/rpc_client_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,27 +216,27 @@ func TestAdapter_HeadSubscriptions(t *testing.T) {
rpc := newTestRPC(t)
_, sub1, err := rpc.SubscribeToHeads(tests.Context(t))
require.NoError(t, err)
require.Equal(t, 1, rpc.LenSubs())
require.Equal(t, 1, rpc.lenSubs())
_, sub2, err := rpc.SubscribeToFinalizedHeads(tests.Context(t))
require.NoError(t, err)
require.Equal(t, 2, rpc.LenSubs())
require.Equal(t, 2, rpc.lenSubs())

sub1.Unsubscribe()
require.Equal(t, 1, rpc.LenSubs())
require.Equal(t, 1, rpc.lenSubs())
sub2.Unsubscribe()
require.Equal(t, 0, rpc.LenSubs())
require.Equal(t, 0, rpc.lenSubs())
})

t.Run("Ensure no deadlock on UnsubscribeAll", func(t *testing.T) {
rpc := newTestRPC(t)
_, _, err := rpc.SubscribeToHeads(tests.Context(t))
require.NoError(t, err)
require.Equal(t, 1, rpc.LenSubs())
require.Equal(t, 1, rpc.lenSubs())
_, _, err = rpc.SubscribeToFinalizedHeads(tests.Context(t))
require.NoError(t, err)
require.Equal(t, 2, rpc.LenSubs())
require.Equal(t, 2, rpc.lenSubs())
rpc.UnsubscribeAllExcept()
require.Equal(t, 0, rpc.LenSubs())
require.Equal(t, 0, rpc.lenSubs())
})
}

Expand All @@ -262,7 +262,7 @@ func TestMultiNodeClient_RegisterSubs(t *testing.T) {
sub, err := rpc.RegisterSub(mockSub, make(chan struct{}))
require.NoError(t, err)
require.NotNil(t, sub)
require.Equal(t, 1, rpc.LenSubs())
require.Equal(t, 1, rpc.lenSubs())
rpc.UnsubscribeAllExcept()
})

Expand All @@ -285,16 +285,16 @@ func TestMultiNodeClient_RegisterSubs(t *testing.T) {
require.NoError(t, err)
_, err = rpc.RegisterSub(mockSub2, chStopInFlight)
require.NoError(t, err)
require.Equal(t, 2, rpc.LenSubs())
require.Equal(t, 2, rpc.lenSubs())

// Ensure passed sub is not removed
rpc.UnsubscribeAllExcept(sub1)
require.Equal(t, 1, rpc.LenSubs())
require.Equal(t, 1, rpc.lenSubs())
require.True(t, mockSub2.unsubscribed)
require.False(t, mockSub1.unsubscribed)

rpc.UnsubscribeAllExcept()
require.Equal(t, 0, rpc.LenSubs())
require.Equal(t, 0, rpc.lenSubs())
require.True(t, mockSub1.unsubscribed)
})
}

0 comments on commit d1adf37

Please sign in to comment.