sidecar: add CurrentState() method to side car negotiator

This methods makes writing tests easier as the test context can
introspect into the negotiator's state.
This commit is contained in:
Olaoluwa Osuntokun 2021-05-21 20:10:23 -07:00
parent 3369357053
commit 36dbabd714
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306

View file

@ -163,6 +163,8 @@ type AutoAcceptorConfig struct {
// provider and recipient of a sidecar channel to complete the manual steps in
// automated manner.
type SidecarNegotiator struct {
currentState uint32
cfg AutoAcceptorConfig
wg sync.WaitGroup
@ -172,7 +174,8 @@ type SidecarNegotiator struct {
// a valid config.
func NewSidecarNegotiator(cfg AutoAcceptorConfig) *SidecarNegotiator {
return &SidecarNegotiator{
cfg: cfg,
cfg: cfg,
currentState: uint32(cfg.StartingPkt.CurrentState),
}
}
@ -291,7 +294,12 @@ func (a *SidecarNegotiator) autoSidecarReceiver(ctx context.Context,
continue
}
currentState = newPktState.CurrentState
// TODO(roasbeef): make into method for easier
// assertions?
atomic.StoreUint32(
&a.currentState, uint32(newPktState.CurrentState),
)
localTicket = newPktState.ReceiverTicket
// If our next target state is the completion state,
@ -489,6 +497,10 @@ func (a *SidecarNegotiator) autoSidecarProvider(ctx context.Context, startingPkt
currentState = newPktState.CurrentState
localTicket = newPktState.ProviderTicket
atomic.StoreUint32(
&a.currentState, uint32(newPktState.CurrentState),
)
switch {
case priorState == currentState:
fallthrough
@ -515,6 +527,12 @@ func (a *SidecarNegotiator) autoSidecarProvider(ctx context.Context, startingPkt
}
}
// CurrentState returns the current state of the sidecar negotiator.
func (a *SidecarNegotiator) CurrentState() sidecar.State {
state := atomic.LoadUint32(&a.currentState)
return sidecar.State(state)
}
// stateStepProvider is the state transition function for the provider of a
// sidecar ticket. It takes the current transcript state, the provider's
// account, and canned bid and returns a new transition to a new ticket state.