Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
refactor(BUX-222): making tests more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Sep 29, 2023
1 parent bddd544 commit af60eb5
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 129 deletions.
181 changes: 98 additions & 83 deletions chainstate/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,50 @@ func Test_doesErrorContain(t *testing.T) {
})
}

// TestClient_Broadcast_Success will test the method Broadcast()
func TestClient_Broadcast_Success(t *testing.T) {
// TestClient_Broadcast will test the method Broadcast()
func TestClient_Broadcast(t *testing.T) {
t.Parallel()

t.Run("error - missing tx id", func(t *testing.T) {
// given
c := NewTestClient(context.Background(), t)

// when
provider, err := c.Broadcast(
context.Background(), "", onChainExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidTransactionID)
assert.Empty(t, provider)
})

t.Run("error - missing tx hex", func(t *testing.T) {
// given
c := NewTestClient(context.Background(), t)

// when
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, "", defaultBroadcastTimeOut,
)

// then
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidTransactionHex)
assert.Empty(t, provider)
})
}

// TestClient_Broadcast_MAPI will test the method Broadcast() with MAPI
func TestClient_Broadcast_MAPI(t *testing.T) {
t.Parallel()

t.Run("broadcast - success (mAPI)", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockSuccess).
Build()
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftBroadcastSuccess{}),
WithBroadcastClient(bc),
)

// when
Expand All @@ -59,19 +90,22 @@ func TestClient_Broadcast_Success(t *testing.T) {
minercraft.MinerMempool,
minercraft.MinerGorillaPool,
minercraft.MinerMatterpool,
ProviderBroadcastClient,
))
})
}

t.Run("broadcast - success (mAPI timeouts)", func(t *testing.T) {
// TestClient_Broadcast_BroadcastClient will test the method Broadcast() with BroadcastClient
func TestClient_Broadcast_BroadcastClient(t *testing.T) {
t.Parallel()

t.Run("broadcast - success (broadcast-client)", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockSuccess).
Build()
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftBroadcastTimeout{}), // Timeout
WithBroadcastClient(bc), // Success
WithBroadcastClient(bc),
)

// when
Expand All @@ -83,24 +117,26 @@ func TestClient_Broadcast_Success(t *testing.T) {
require.NoError(t, err)
miners := strings.Split(providers, ",")
assert.GreaterOrEqual(t, len(miners), 1)
assert.True(t, containsAtLeastOneElement(miners, ProviderBroadcastClient))
assert.NotContains(t, miners, minercraft.MinerTaal)
assert.NotContains(t, miners, minercraft.MinerMempool)
assert.NotContains(t, miners, minercraft.MinerGorillaPool)
assert.NotContains(t, miners, minercraft.MinerMatterpool)
assert.True(t, containsAtLeastOneElement(
miners,
ProviderBroadcastClient,
))
})
}

// TestClient_Broadcast_MultipleClients will test the method Broadcast() with multiple clients
func TestClient_Broadcast_MultipleClients(t *testing.T) {
t.Parallel()

t.Run("broadcast - success (broadcastClient timeouts)", func(t *testing.T) {
t.Run("broadcast - success from multiple clients", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockTimeout).
WithMockArc(broadcast_client_mock.MockSuccess).
Build()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c := NewTestClient(
ctx, t,
WithMinercraft(&minerCraftBroadcastSuccess{}), // Success
WithBroadcastClient(bc), // Timeout
context.Background(), t,
WithMinercraft(&minerCraftBroadcastSuccess{}),
WithBroadcastClient(bc),
)

// when
Expand All @@ -112,93 +148,72 @@ func TestClient_Broadcast_Success(t *testing.T) {
require.NoError(t, err)
miners := strings.Split(providers, ",")
assert.GreaterOrEqual(t, len(miners), 1)
assert.True(t, containsAtLeastOneElement(
miners,
assert.True(t, containsAtLeastOneElement(miners,
ProviderBroadcastClient,
minercraft.MinerTaal,
minercraft.MinerMempool,
minercraft.MinerGorillaPool,
minercraft.MinerMatterpool,
minercraft.MinerGorillaPool,
minercraft.MinerMempool,
))
assert.NotContains(t, miners, ProviderBroadcastClient)
})
}

// TestClient_Broadcast_OnChain will test the method Broadcast()
func TestClient_Broadcast_OnChain(t *testing.T) {
t.Parallel()

t.Run("broadcast - tx already on-chain (mAPI)", func(t *testing.T) {
t.Run("broadcast - success from broadcastClient (mAPI timeouts)", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockSuccess).
Build()
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftTxOnChain{}),
WithMinercraft(&minerCraftBroadcastTimeout{}), // Timeout
WithBroadcastClient(bc), // Success
)

// when
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.NoError(t, err)
assert.NotEmpty(t, provider)
miners := strings.Split(providers, ",")
assert.GreaterOrEqual(t, len(miners), 1)
assert.True(t, containsAtLeastOneElement(miners, ProviderBroadcastClient))
assert.NotContains(t, miners, minercraft.MinerTaal)
assert.NotContains(t, miners, minercraft.MinerMempool)
assert.NotContains(t, miners, minercraft.MinerGorillaPool)
assert.NotContains(t, miners, minercraft.MinerMatterpool)
})
}

// TestClient_Broadcast_InMempool will test the method Broadcast()
func TestClient_Broadcast_InMempool(t *testing.T) {
t.Parallel()

t.Run("broadcast - in mempool (mAPI)", func(t *testing.T) {
t.Run("broadcast - success from mAPI (broadcastClient timeouts)", func(t *testing.T) {
// given
bc := broadcast_client_mock.Builder().
WithMockArc(broadcast_client_mock.MockTimeout).
Build()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftInMempool{}),
ctx, t,
WithBroadcastClient(bc), // Timeout
WithMinercraft(&minerCraftBroadcastSuccess{}), // Success
)

// when
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.NoError(t, err)
assert.NotEmpty(t, provider)
})
}

// TestClient_Broadcast will test the method Broadcast()
func TestClient_Broadcast(t *testing.T) {
t.Parallel()

t.Run("error - missing tx id", func(t *testing.T) {
// given
c := NewTestClient(context.Background(), t)

// when
provider, err := c.Broadcast(
context.Background(), "", onChainExample1TxHex, defaultBroadcastTimeOut,
)

// then
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidTransactionID)
assert.Empty(t, provider)
})

t.Run("error - missing tx hex", func(t *testing.T) {
// given
c := NewTestClient(context.Background(), t)

// when
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, "", defaultBroadcastTimeOut,
)

// then
require.Error(t, err)
assert.ErrorIs(t, err, ErrInvalidTransactionHex)
assert.Empty(t, provider)
miners := strings.Split(providers, ",")
assert.GreaterOrEqual(t, len(miners), 1)
assert.True(t, containsAtLeastOneElement(
miners,
minercraft.MinerTaal,
minercraft.MinerMempool,
minercraft.MinerGorillaPool,
minercraft.MinerMatterpool,
))
assert.NotContains(t, miners, ProviderBroadcastClient)
})

t.Run("broadcast - all providers fail", func(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion chainstate/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestNewClient(t *testing.T) {
c, err := NewClient(
context.Background(),
WithBroadcastClient(customClient),
WithMinercraft(&MinerCraftBase{}),
)
require.NoError(t, err)
require.NotNil(t, c)
Expand Down
8 changes: 4 additions & 4 deletions chainstate/mock_minercraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,11 +778,11 @@ func (m *minerCraftUnreachble) FeeQuote(context.Context, *minercraft.Miner) (*mi
return nil, errors.New("minercraft is unreachable")
}

/*type minerCraftBroadcastTimeout struct {
type minerCraftBroadcastTimeout struct {
MinerCraftBase
}*/
}

/*func (m *minerCraftBroadcastTimeout) SubmitTransaction(_ context.Context, miner *minercraft.Miner,
func (m *minerCraftBroadcastTimeout) SubmitTransaction(_ context.Context, miner *minercraft.Miner,
_ *minercraft.Transaction,
) (*minercraft.SubmitTransactionResponse, error) {
time.Sleep(defaultBroadcastTimeOut * 2)
Expand All @@ -808,4 +808,4 @@ func (m *minerCraftUnreachble) FeeQuote(context.Context, *minercraft.Miner) (*mi
TxID: "",
},
}, nil
}*/
}
Loading

0 comments on commit af60eb5

Please sign in to comment.