Skip to content

Commit

Permalink
fix unit test conflicts and thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Jul 1, 2024
1 parent 3c1f245 commit ca9142d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 52 deletions.
8 changes: 8 additions & 0 deletions zetaclient/orchestrator/chain_activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (oc *Orchestrator) ActivateDeactivateChains() {
oc.logger.Std.Info().Msgf("ActivateDeactivateChains: deactivating chain %d", chainID)

observer.Stop()

// remove signer and observer from maps
oc.mu.Lock()
defer oc.mu.Unlock()
delete(oc.signerMap, chainID)
delete(oc.observerMap, chainID)
}
Expand All @@ -71,6 +75,10 @@ func (oc *Orchestrator) ActivateDeactivateChains() {
}

observer.Start()

// add signer and observer to maps
oc.mu.Lock()
defer oc.mu.Unlock()
oc.signerMap[chainID] = newSignerMap[chainID]
oc.observerMap[chainID] = observer
}
Expand Down
4 changes: 4 additions & 0 deletions zetaclient/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ func (oc *Orchestrator) Stop() {

// GetUpdatedSigner returns signer with updated chain parameters
func (oc *Orchestrator) GetUpdatedSigner(chainID int64) (interfaces.ChainSigner, error) {
oc.mu.Lock()
signer, found := oc.signerMap[chainID]
oc.mu.Unlock()
if !found {
return nil, fmt.Errorf("signer not found for chainID %d", chainID)
}
Expand Down Expand Up @@ -215,7 +217,9 @@ func (oc *Orchestrator) GetUpdatedSigner(chainID int64) (interfaces.ChainSigner,

// GetUpdatedChainObserver returns chain observer with updated chain parameters
func (oc *Orchestrator) GetUpdatedChainObserver(chainID int64) (interfaces.ChainObserver, error) {
oc.mu.Lock()
observer, found := oc.observerMap[chainID]
oc.mu.Unlock()
if !found {
return nil, fmt.Errorf("chain observer not found for chainID %d", chainID)
}
Expand Down
53 changes: 1 addition & 52 deletions zetaclient/orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ func MockOrchestrator(
return orchestrator
}

<<<<<<< HEAD
// CreateTestAppContext creates a test app context for orchestrator testing
func CreateTestAppContext(
=======
func CreateAppContext(
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
evmChain, btcChain chains.Chain,
evmChainParams, btcChainParams *observertypes.ChainParams,
) *context.AppContext {
Expand All @@ -72,42 +69,24 @@ func CreateAppContext(
cfg.BitcoinConfig = config.BTCConfig{
RPCHost: "localhost",
}
<<<<<<< HEAD
// new app context
appContext := context.NewAppContext(cfg)
chainParamsMap := make(map[int64]*observertypes.ChainParams)
chainParamsMap[evmChain.ChainId] = evmChainParams
chainParamsMap[btcChain.ChainId] = btcChainParams
=======
// new zetacore context
appContext := context.New(cfg, zerolog.Nop())
evmChainParamsMap := make(map[int64]*observertypes.ChainParams)
evmChainParamsMap[evmChain.ChainId] = evmChainParams
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
ccFlags := sample.CrosschainFlags()
verificationFlags := sample.HeaderSupportedChains()

// feed chain params
appContext.Update(
<<<<<<< HEAD
observertypes.Keygen{},
=======
&observertypes.Keygen{},
[]chains.Chain{evmChain, btcChain},
evmChainParamsMap,
btcChainParams,
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
"",
[]chains.Chain{evmChain, btcChain},
chainParamsMap,
&chaincfg.RegressionNetParams,
*ccFlags,
verificationFlags,
<<<<<<< HEAD
zerolog.Logger{},
=======
true,
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
)
return appContext
}
Expand All @@ -131,26 +110,16 @@ func Test_GetUpdatedSigner(t *testing.T) {
}

t.Run("signer should not be found", func(t *testing.T) {
<<<<<<< HEAD
appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams)
=======
orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams)
context := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
// BSC signer should not be found
_, err := orchestrator.GetUpdatedSigner(chains.BscMainnet.ChainId)
require.ErrorContains(t, err, "signer not found")
})
t.Run("should be able to update connector and erc20 custody address", func(t *testing.T) {
<<<<<<< HEAD
appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams)

=======
orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams)
context := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
// update signer with new connector and erc20 custody address
signer, err := orchestrator.GetUpdatedSigner(evmChain.ChainId)
require.NoError(t, err)
Expand Down Expand Up @@ -207,55 +176,35 @@ func Test_GetUpdatedChainObserver(t *testing.T) {
}

t.Run("evm chain observer should not be found", func(t *testing.T) {
<<<<<<< HEAD
appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams)

=======
orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams)
coreContext := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
// BSC chain observer should not be found
_, err := orchestrator.GetUpdatedChainObserver(chains.BscMainnet.ChainId)
require.ErrorContains(t, err, "chain observer not found")
})
t.Run("chain params in evm chain observer should be updated successfully", func(t *testing.T) {
<<<<<<< HEAD
appCtx := CreateTestAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams)

=======
orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams)
coreContext := CreateAppContext(evmChain, btcChain, evmChainParamsNew, btcChainParams)
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
// update evm chain observer with new chain params
chainOb, err := orchestrator.GetUpdatedChainObserver(evmChain.ChainId)
require.NoError(t, err)
require.NotNil(t, chainOb)
require.True(t, observertypes.ChainParamsEqual(*evmChainParamsNew, chainOb.GetChainParams()))
})
t.Run("btc chain observer should not be found", func(t *testing.T) {
<<<<<<< HEAD
appCtx := CreateTestAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew)
orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams)

=======
orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams)
coreContext := CreateAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew)
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
// BTC testnet chain observer should not be found
_, err := orchestrator.GetUpdatedChainObserver(chains.BitcoinTestnet.ChainId)
require.ErrorContains(t, err, "chain observer not found")
})
t.Run("chain params in btc chain observer should be updated successfully", func(t *testing.T) {
<<<<<<< HEAD
appCtx := CreateTestAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew)
orchestrator := MockOrchestrator(t, appCtx, nil, evmChain, btcChain, evmChainParams, btcChainParams)

=======
orchestrator := MockOrchestrator(t, nil, evmChain, btcChain, evmChainParams, btcChainParams)
coreContext := CreateAppContext(btcChain, btcChain, evmChainParams, btcChainParamsNew)
>>>>>>> 2d5519f4d64bb05b64e6f2c7ec9ef6a87e97610f
// update btc chain observer with new chain params
chainOb, err := orchestrator.GetUpdatedChainObserver(btcChain.ChainId)
require.NoError(t, err)
Expand Down

0 comments on commit ca9142d

Please sign in to comment.